diff --git a/.upgradenotes/MDL-82490-2024080509320820.yml b/.upgradenotes/MDL-82490-2024080509320820.yml new file mode 100644 index 00000000000..2070b225e51 --- /dev/null +++ b/.upgradenotes/MDL-82490-2024080509320820.yml @@ -0,0 +1,7 @@ +issueNumber: MDL-82490 +notes: + core: + - message: >- + Add set_disabled_option method to url_select to enable or disable an option + from its url (the key for the option). + type: improved diff --git a/lib/classes/output/url_select.php b/lib/classes/output/url_select.php index 3068f443ae9..5d4228c23b1 100644 --- a/lib/classes/output/url_select.php +++ b/lib/classes/output/url_select.php @@ -93,6 +93,11 @@ class url_select implements renderable, templatable { */ public $showbutton = null; + /** + * @var array $disabledoptions array of disabled options + */ + public $disabledoptions = []; + /** * Constructor * @param array $urls list of options @@ -108,6 +113,17 @@ class url_select implements renderable, templatable { $this->nothing = $nothing; $this->formid = $formid; $this->showbutton = $showbutton; + $this->disabledoptions = []; + } + + /** + * Disable the option(url). + * + * @param string $urlkey + * @param bool $disabled + */ + public function set_option_disabled(string $urlkey, bool $disabled = true) { + $this->disabledoptions[$urlkey] = $disabled; } /** @@ -189,6 +205,7 @@ class url_select implements renderable, templatable { 'name' => $optoption, 'value' => $cleanedvalue, 'selected' => $this->selected == $optvalue, + 'disabled' => $this->disabledoptions[$optvalue] ?? false, ]; } } @@ -198,6 +215,7 @@ class url_select implements renderable, templatable { 'name' => $option, 'value' => $cleanedvalue, 'selected' => $this->selected == $value, + 'disabled' => $this->disabledoptions[$value] ?? false, ]; } } diff --git a/lib/templates/url_select.mustache b/lib/templates/url_select.mustache index d9c9992ac40..92d21a7d937 100644 --- a/lib/templates/url_select.mustache +++ b/lib/templates/url_select.mustache @@ -26,13 +26,18 @@ "name": "Group 1", "isgroup": true, "options": [ {"name": "Item 1", "isgroup": false, "value": "1"}, - {"name": "Item 2", "isgroup": false, "value": "2"} + {"name": "Item 2", "isgroup": false, "value": "2"}, + {"name": "Item 1", "isgroup": false, "value": "3", "disabled": true} ]}, {"name": "Group 2", "isgroup": true, "options": [ {"name": "Item 3", "isgroup": false, "value": "3"}, {"name": "Item 4", "isgroup": false, "value": "4"} - ]}], + ]}, + {"name": "Group 3", "isgroup": false, "value":"1"}, + {"name": "Group 4", "isgroup": false, "value":"1", "disabled": true} + ], + "disabled": false, "title": "Some cool title" } @@ -54,12 +59,12 @@ {{#isgroup}} {{#options}} - + {{/options}} {{/isgroup}} {{^isgroup}} - + {{/isgroup}} {{/options}} diff --git a/lib/tests/outputcomponents_test.php b/lib/tests/outputcomponents_test.php index 15af70b24c9..94d40347ae7 100644 --- a/lib/tests/outputcomponents_test.php +++ b/lib/tests/outputcomponents_test.php @@ -744,6 +744,38 @@ EOF; $this->assertTrue(in_array(['name' => 'style', 'value' => $labelstyle], $data->labelattributes)); } + /** + * Test for checking the template context data for the url_select element. + * @covers \url_select::disable_option + * @covers \url_select::enable_option + */ + public function test_url_select_disabled_options(): void { + global $PAGE; + $url1 = new \moodle_url("/#a"); + $url2 = new \moodle_url("/#b"); + $url3 = new \moodle_url("/#c"); + + $urls = [ + $url1->out() => 'A', + $url2->out() => 'B', + $url3->out() => 'C', + ]; + $urlselect = new url_select($urls, + null, + null, + 'someformid', + null); + $renderer = $PAGE->get_renderer('core'); + $urlselect->set_option_disabled($url2->out(), true); + $data = $urlselect->export_for_template($renderer); + $this->assertFalse($data->options[0]['disabled']); + $this->assertTrue($data->options[1]['disabled']); + $urlselect->set_option_disabled($url2->out(), false); + $data = $urlselect->export_for_template($renderer); + $this->assertFalse($data->options[0]['disabled']); + $this->assertFalse($data->options[1]['disabled']); + } + /** * Data provider for test_block_contents_is_fake(). *