MDL-56577 core_form: Let other option attrs to be set in select elements
authorJun Pataleta <jun@moodle.com>
Tue, 25 Oct 2016 06:23:21 +0000 (14:23 +0800)
committerJun Pataleta <jun@moodle.com>
Mon, 31 Oct 2016 04:19:21 +0000 (12:19 +0800)
lib/form/select.php
lib/form/selectgroups.php
lib/form/selectwithlink.php
theme/boost/templates/core_form/element-select-inline.mustache
theme/boost/templates/core_form/element-select.mustache
theme/boost/templates/core_form/element-selectgroups-inline.mustache
theme/boost/templates/core_form/element-selectgroups.mustache
theme/boost/templates/core_form/element-selectwithlink.mustache

index d0a03dc..e0f8cf9 100644 (file)
@@ -200,6 +200,8 @@ class MoodleQuickForm_select extends HTML_QuickForm_select implements templatabl
         $context = $this->export_for_template_base($output);
 
         $options = [];
+        // Standard option attributes.
+        $standardoptionattributes = ['text', 'value', 'selected', 'disabled'];
         foreach ($this->_options as $option) {
             if (is_array($this->_values) && in_array( (string) $option['attr']['value'], $this->_values)) {
                 $this->_updateAttrArray($option['attr'], ['selected' => 'selected']);
@@ -207,8 +209,17 @@ class MoodleQuickForm_select extends HTML_QuickForm_select implements templatabl
             $o = [
                 'text' => $option['text'],
                 'value' => $option['attr']['value'],
-                'selected' => !empty($option['attr']['selected'])
+                'selected' => !empty($option['attr']['selected']),
+                'disabled' => !empty($option['attr']['disabled']),
             ];
+            // Set other attributes.
+            $otheroptionattributes = [];
+            foreach ($option['attr'] as $attr => $value) {
+                if (!in_array($attr, $standardoptionattributes) && $attr != 'class' && !is_object($value)) {
+                    $otheroptionattributes[] = $attr . '="' . s($value) . '"';
+                }
+            }
+            $o['optionattributes'] = implode(' ', $otheroptionattributes);
             $options[] = $o;
         }
         $context['options'] = $options;
index ab547cf..9ed4d05 100644 (file)
@@ -522,6 +522,8 @@ class MoodleQuickForm_selectgroups extends HTML_QuickForm_element implements tem
             ];
         }
 
+        // Standard option attributes.
+        $standardoptionattributes = ['text', 'value', 'selected', 'disabled'];
         foreach ($this->_optGroups as $group) {
             $options = [];
 
@@ -533,6 +535,15 @@ class MoodleQuickForm_selectgroups extends HTML_QuickForm_element implements tem
                     $o['selected'] = false;
                 }
                 $o['text'] = $option['text'];
+                $o['disabled'] = !empty($option['attr']['disabled']);
+                // Set other attributes.
+                $otheroptionattributes = [];
+                foreach ($option['attr'] as $attr => $value) {
+                    if (!in_array($attr, $standardoptionattributes) && $attr != 'class' && !is_object($value)) {
+                        $otheroptionattributes[] = $attr . '="' . s($value) . '"';
+                    }
+                }
+                $o['optionattributes'] = implode(' ', $otheroptionattributes);
                 $options[] = $o;
             }
 
index 1643e6f..fc4a915 100644 (file)
@@ -231,6 +231,8 @@ class MoodleQuickForm_selectwithlink extends HTML_QuickForm_select implements te
         $context = $this->export_for_template_base($output);
 
         $options = [];
+        // Standard option attributes.
+        $standardoptionattributes = ['text', 'value', 'selected', 'disabled'];
         foreach ($this->_options as $option) {
             if (is_array($this->_values) && in_array( (string) $option['attr']['value'], $this->_values)) {
                 $this->_updateAttrArray($option['attr'], ['selected' => 'selected']);
@@ -238,8 +240,17 @@ class MoodleQuickForm_selectwithlink extends HTML_QuickForm_select implements te
             $o = [
                 'text' => $option['text'],
                 'value' => $option['attr']['value'],
-                'selected' => !empty($option['attr']['selected'])
+                'selected' => !empty($option['attr']['selected']),
+                'disabled' => !empty($option['attr']['disabled']),
             ];
+            // Set other attributes.
+            $otheroptionattributes = [];
+            foreach ($option['attr'] as $attr => $value) {
+                if (!in_array($attr, $standardoptionattributes) && $attr != 'class' && !is_object($value)) {
+                    $otheroptionattributes[] = $attr . '="' . s($value) . '"';
+                }
+            }
+            $o['optionattributes'] = implode(' ', $otheroptionattributes);
             $options[] = $o;
         }
         $context['options'] = $options;
index 155f101..6362a7a 100644 (file)
@@ -9,7 +9,7 @@
         {{/error}}
         {{element.attributes}} >
         {{#element.options}}
-        <option value="{{value}}" {{#selected}}selected{{/selected}}>{{text}}</option>
+        <option value="{{value}}" {{#selected}}selected{{/selected}} {{#disabled}}disabled{{/disabled}} {{{optionattributes}}}>{{text}}</option>
         {{/element.options}}
     </select>
         {{/element.frozen}}
index 4ac6560..6947361 100644 (file)
@@ -9,7 +9,7 @@
             {{/error}}
             {{element.attributes}} >
             {{#element.options}}
-            <option value="{{value}}" {{#selected}}selected{{/selected}}>{{text}}</option>
+            <option value="{{value}}" {{#selected}}selected{{/selected}} {{#disabled}}disabled{{/disabled}} {{{optionattributes}}}>{{text}}</option>
             {{/element.options}}
         </select>
         {{/element.frozen}}
index 3889a87..72e767a 100644 (file)
@@ -13,7 +13,7 @@
         <optgroup label="{{text}}">
         {{/text}}
         {{#options}}
-        <option value="{{value}}" {{#selected}}selected{{/selected}}>{{{text}}}</option>
+        <option value="{{value}}" {{#selected}}selected{{/selected}} {{#disabled}}disabled{{/disabled}} {{{optionattributes}}}>{{{text}}}</option>
         {{/options}}
         {{#text}}
         </optgroup>
index 99b93e2..88fef74 100644 (file)
@@ -13,7 +13,7 @@
             <optgroup label="{{text}}">
             {{/text}}
             {{#options}}
-            <option value="{{value}}" {{#selected}}selected{{/selected}}>{{{text}}}</option>
+            <option value="{{value}}" {{#selected}}selected{{/selected}} {{#disabled}}disabled{{/disabled}} {{{optionattributes}}}>{{{text}}}</option>
             {{/options}}
             {{/element.optiongroups}}
             {{#text}}
index f561e5a..66cbe9c 100644 (file)
@@ -9,7 +9,7 @@
             {{/error}}
             {{element.attributes}} >
             {{#element.options}}
-            <option value="{{value}}" {{#selected}}selected{{/selected}}>{{text}}</option>
+            <option value="{{value}}" {{#selected}}selected{{/selected}} {{#disabled}}disabled{{/disabled}} {{{optionattributes}}}>{{text}}</option>
             {{/element.options}}
         </select>
         {{/element.frozen}}