Browse Source

Improve templateVars support for radios and add tests.

Allow the top-level templatevars to be used for the wrapper, while each
input can use the complex form to set its own templateVars. Individual
radio buttons will also inherit template vars from the top level key.
Mark Story 10 years ago
parent
commit
bcc2d93d65
2 changed files with 40 additions and 5 deletions
  1. 9 5
      src/View/Widget/RadioWidget.php
  2. 31 0
      tests/TestCase/View/Widget/RadioWidgetTest.php

+ 9 - 5
src/View/Widget/RadioWidget.php

@@ -151,24 +151,27 @@ class RadioWidget implements WidgetInterface
         $escape = $data['escape'];
         if (is_int($val) && isset($text['text'], $text['value'])) {
             $radio = $text;
-            $text = $radio['text'];
         } else {
             $radio = ['value' => $val, 'text' => $text];
         }
         $radio['name'] = $data['name'];
 
+        if (!isset($radio['templateVars'])) {
+            $radio['templateVars'] = [];
+        }
+        if (!empty($data['templateVars'])) {
+            $radio['templateVars'] = array_merge($data['templateVars'], $radio['templateVars']);
+        }
+
         if (empty($radio['id'])) {
             $radio['id'] = $this->_id($radio['name'], $radio['value']);
         }
-
         if (isset($data['val']) && is_bool($data['val'])) {
             $data['val'] = $data['val'] ? 1 : 0;
         }
-
         if (isset($data['val']) && strval($data['val']) === strval($radio['value'])) {
             $radio['checked'] = true;
         }
-
         if ($this->_isDisabled($radio, $data['disabled'])) {
             $radio['disabled'] = true;
         }
@@ -182,7 +185,7 @@ class RadioWidget implements WidgetInterface
         $input = $this->_templates->format('radio', [
             'name' => $radio['name'],
             'value' => $escape ? h($radio['value']) : $radio['value'],
-            'templateVars' => $data['templateVars'],
+            'templateVars' => $radio['templateVars'],
             'attrs' => $this->_templates->formatAttributes($radio, ['name', 'value', 'text']),
         ]);
 
@@ -230,6 +233,7 @@ class RadioWidget implements WidgetInterface
             'for' => $radio['id'],
             'escape' => $escape,
             'text' => $radio['text'],
+            'templateVars' => $radio['templateVars'],
             'input' => $input,
         ];
         return $this->_label->render($labelAttrs, $context);

+ 31 - 0
tests/TestCase/View/Widget/RadioWidgetTest.php

@@ -657,4 +657,35 @@ class RadioWidgetTest extends TestCase
             $result
         );
     }
+
+    /**
+     * Ensure that template vars work.
+     *
+     * @return void
+     */
+    public function testRenderTemplateVars()
+    {
+        $this->templates->add([
+            'radioWrapper' => '<div class="radio" data-var="{{wrapperVar}}">{{label}}</div>',
+            'radio' => '<input type="radio" data-i="{{inputVar}}" name="{{name}}" value="{{value}}"{{attrs}}>',
+            'nestingLabel' => '<label{{attrs}}>{{input}}{{text}} {{labelVar}} {{wrapperVar}}</label>',
+        ]);
+        $label = new NestingLabelWidget($this->templates);
+        $radio = new RadioWidget($this->templates, $label);
+        $data = [
+            'name' => 'Versions[ver]',
+            'options' => [
+                ['value' => '1x', 'text' => 'one x', 'templateVars' => ['labelVar' => 'l-var', 'inputVar' => 'i-var']],
+                '2' => 'two',
+            ],
+            'templateVars' => [
+                'wrapperVar' => 'wrap-var',
+            ]
+        ];
+        $result = $radio->render($data, $this->context);
+        $this->assertContains('data-var="wrap-var"><label', $result);
+        $this->assertContains('type="radio" data-i="i-var"', $result);
+        $this->assertContains('one x l-var wrap-var</label>', $result);
+        $this->assertContains('two  wrap-var</label>', $result);
+    }
 }