MDL-65081 core_output: Set single_select and url_select attributes

This commit is contained in:
Jun Pataleta 2019-03-15 10:41:55 +08:00
parent 3271c39c74
commit 5f30600f43
3 changed files with 31 additions and 8 deletions

View File

@ -1020,7 +1020,7 @@ class single_select implements renderable, templatable {
var $formid = null;
/**
* @var array List of attached actions
* @var help_icon The help icon for this element.
*/
var $helpicon = null;
@ -1111,7 +1111,19 @@ class single_select implements renderable, templatable {
$data->title = $this->tooltip;
$data->formid = !empty($this->formid) ? $this->formid : html_writer::random_id('single_select_f');
$data->id = !empty($attributes['id']) ? $attributes['id'] : html_writer::random_id('single_select');
// Select element attributes.
// Unset attributes that are already predefined in the template.
unset($attributes['id']);
unset($attributes['class']);
unset($attributes['name']);
unset($attributes['title']);
unset($attributes['disabled']);
// Map the attributes.
$data->attributes = array_map(function($key) use ($attributes) {
return ['name' => $key, 'value' => $attributes[$key]];
}, array_keys($attributes));
// Form parameters.
$params = $this->url->params();
@ -1185,8 +1197,11 @@ class single_select implements renderable, templatable {
// Label attributes.
$data->labelattributes = [];
// Unset label attributes that are already in the template.
unset($this->labelattributes['for']);
// Map the label attributes.
foreach ($this->labelattributes as $key => $value) {
$data->labelattributes = ['name' => $key, 'value' => $value];
$data->labelattributes[] = ['name' => $key, 'value' => $value];
}
// Help icon.
@ -1260,7 +1275,7 @@ class url_select implements renderable, templatable {
var $formid = null;
/**
* @var array List of attached actions
* @var help_icon The help icon for this element.
*/
var $helpicon = null;
@ -1423,6 +1438,7 @@ class url_select implements renderable, templatable {
unset($attributes['id']);
unset($attributes['name']);
unset($attributes['title']);
unset($attributes['disabled']);
$data->showbutton = $this->showbutton;
@ -1442,6 +1458,9 @@ class url_select implements renderable, templatable {
// Label attributes.
$data->labelattributes = [];
// Unset label attributes that are already in the template.
unset($this->labelattributes['for']);
// Map the label attributes.
foreach ($this->labelattributes as $key => $value) {
$data->labelattributes[] = ['name' => $key, 'value' => $value];
}
@ -1451,8 +1470,8 @@ class url_select implements renderable, templatable {
// Finally all the remaining attributes.
$data->attributes = [];
foreach ($this->attributes as $key => $value) {
$data->attributes = ['name' => $key, 'value' => $value];
foreach ($attributes as $key => $value) {
$data->attributes[] = ['name' => $key, 'value' => $value];
}
return $data;

View File

@ -95,7 +95,8 @@
{{#helpicon}}
{{>core/help_icon}}
{{/helpicon}}
<select {{#attributes}}{{name}}="{{value}}" {{/attributes}} id="{{id}}" class="custom-select {{classes}}" name="{{name}}">
<select {{#attributes}}{{name}}="{{value}}" {{/attributes}} id="{{id}}" class="custom-select {{classes}}" name="{{name}}"
{{#title}}title="{{.}}"{{/title}} {{#disabled}}disabled{{/disabled}}>
{{#options}}
{{#optgroup}}
<optgroup label="{{name}}">

View File

@ -32,7 +32,9 @@
[
{"name": "Item 3", "isgroup": false, "value": "3"},
{"name": "Item 4", "isgroup": false, "value": "4"}
]}]
]}],
"disabled": false,
"title": "Some cool title"
}
}}
<div class="{{classes}}">
@ -46,7 +48,8 @@
{{#helpicon}}
{{>core/help_icon}}
{{/helpicon}}
<select {{#attributes}}{{name}}="{{value}}" {{/attributes}} id="{{id}}" class="custom-select {{classes}}" name="jump">
<select {{#attributes}}{{name}}="{{value}}" {{/attributes}} id="{{id}}" class="custom-select {{classes}}" name="jump"
{{#title}}title="{{.}}"{{/title}} {{#disabled}}disabled{{/disabled}}>
{{#options}}
{{#isgroup}}
<optgroup label="{{name}}">