Browse Source

Make selectbox template vars cascade as one would expect.

Expecting people to always use expanded option forms is not going to end
well. Given that I can already see the argument for supporting this
use-case I can add it now.
Mark Story 10 years ago
parent
commit
f7d6dfc856

+ 17 - 6
src/View/Widget/SelectBoxWidget.php

@@ -161,7 +161,8 @@ class SelectBoxWidget extends BasicWidget
         if (isset($data['disabled']) && is_array($data['disabled'])) {
             $disabled = $data['disabled'];
         }
-        return $this->_renderOptions($options, $disabled, $selected, $data['escape']);
+        $templateVars = $data['templateVars'];
+        return $this->_renderOptions($options, $disabled, $selected, $templateVars, $data['escape']);
     }
 
     /**
@@ -171,10 +172,11 @@ class SelectBoxWidget extends BasicWidget
      * @param array $optgroup The opt group data.
      * @param array|null $disabled The options to disable.
      * @param array|string|null $selected The options to select.
+     * @param array $templateVars Additional template variables.
      * @param bool $escape Toggle HTML escaping
      * @return string Formatted template string
      */
-    protected function _renderOptgroup($label, $optgroup, $disabled, $selected, $escape)
+    protected function _renderOptgroup($label, $optgroup, $disabled, $selected, $templateVars, $escape)
     {
         $opts = $optgroup;
         $attrs = [];
@@ -183,11 +185,12 @@ class SelectBoxWidget extends BasicWidget
             $label = $optgroup['text'];
             $attrs = $optgroup;
         }
-        $groupOptions = $this->_renderOptions($opts, $disabled, $selected, $escape);
+        $groupOptions = $this->_renderOptions($opts, $disabled, $selected, $templateVars, $escape);
 
         return $this->_templates->format('optgroup', [
             'label' => $escape ? h($label) : $label,
             'content' => implode('', $groupOptions),
+            'templateVars' => $templateVars,
             'attrs' => $this->_templates->formatAttributes($attrs, ['text', 'options']),
         ]);
     }
@@ -200,10 +203,11 @@ class SelectBoxWidget extends BasicWidget
      * @param array $options The options to render.
      * @param array|null $disabled The options to disable.
      * @param array|string|null $selected The options to select.
+     * @param array $templateVars Additional template variables.
      * @param bool $escape Toggle HTML escaping.
      * @return array Option elements.
      */
-    protected function _renderOptions($options, $disabled, $selected, $escape)
+    protected function _renderOptions($options, $disabled, $selected, $templateVars, $escape)
     {
         $out = [];
         foreach ($options as $key => $val) {
@@ -212,7 +216,7 @@ class SelectBoxWidget extends BasicWidget
             if ((!is_int($key) && $arrayVal) ||
                 (is_int($key) && $arrayVal && isset($val['options']))
             ) {
-                $out[] = $this->_renderOptgroup($key, $val, $disabled, $selected, $escape);
+                $out[] = $this->_renderOptgroup($key, $val, $disabled, $selected, $templateVars, $escape);
                 continue;
             }
 
@@ -220,23 +224,30 @@ class SelectBoxWidget extends BasicWidget
             $optAttrs = [
                 'value' => $key,
                 'text' => $val,
+                'templateVars' => [],
             ];
             if (is_array($val) && isset($optAttrs['text'], $optAttrs['value'])) {
                 $optAttrs = $val;
                 $key = $optAttrs['value'];
             }
+            if (!isset($optAttrs['templateVars'])) {
+                $optAttrs['templateVars'] = [];
+            }
             if ($this->_isSelected($key, $selected)) {
                 $optAttrs['selected'] = true;
             }
             if ($this->_isDisabled($key, $disabled)) {
                 $optAttrs['disabled'] = true;
             }
+            if (!empty($templateVars)) {
+                $optAttrs['templateVars'] = array_merge($templateVars, $optAttrs['templateVars']);
+            }
             $optAttrs['escape'] = $escape;
 
             $out[] = $this->_templates->format('option', [
                 'value' => $escape ? h($optAttrs['value']) : $optAttrs['value'],
                 'text' => $escape ? h($optAttrs['text']) : $optAttrs['text'],
-                'templateVars' => isset($optAttrs['templateVars']) ? $optAttrs['templateVars'] : [],
+                'templateVars' => $optAttrs['templateVars'],
                 'attrs' => $this->_templates->formatAttributes($optAttrs, ['text', 'value']),
             ]);
         }

+ 2 - 2
tests/TestCase/View/Widget/SelectBoxWidgetTest.php

@@ -812,10 +812,10 @@ class SelectBoxWidgetTest extends TestCase
             ['option' => ['value' => 'a', 'opt' => 'opt-1']],
             'Albatross',
             '/option',
-            ['option' => ['value' => 'b', 'opt' => '']],
+            ['option' => ['value' => 'b', 'opt' => 'option']],
             'Budgie',
             '/option',
-            ['option' => ['value' => 'c', 'opt' => '']],
+            ['option' => ['value' => 'c', 'opt' => 'option']],
             'Canary',
             '/option',
             '/select'