MDL-82490 core_output: Add disabled option for url_select
* Add a way to disable (and enable later) option in the url select selector.
This commit is contained in:
@@ -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
|
||||
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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}}
|
||||
<optgroup label="{{name}}">
|
||||
{{#options}}
|
||||
<option value="{{value}}" {{#selected}}selected{{/selected}}>{{{name}}}</option>
|
||||
<option value="{{value}}" {{#selected}}selected{{/selected}} {{#disabled}}disabled{{/disabled}}>{{{name}}}</option>
|
||||
{{/options}}
|
||||
</optgroup>
|
||||
{{/isgroup}}
|
||||
{{^isgroup}}
|
||||
<option value="{{value}}" {{#selected}}selected{{/selected}}>{{{name}}}</option>
|
||||
<option value="{{value}}" {{#selected}}selected{{/selected}} {{#disabled}}disabled{{/disabled}}>{{{name}}}</option>
|
||||
{{/isgroup}}
|
||||
{{/options}}
|
||||
</select>
|
||||
|
||||
@@ -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().
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user