[FEATURE] Add generic data-* attribute 90/32590/3
authorFrank Nägler <typo3@naegler.net>
Thu, 4 Sep 2014 21:18:03 +0000 (23:18 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Sat, 6 Sep 2014 18:27:48 +0000 (20:27 +0200)
All elements in HTML5 can have any number of data-* attributes.
This additional array attribute on tag bases viewhelpers makes
it easier to add several data-* attributes to them:
  <f:form.textfield data="{foo: 'bar', baz: 'foos'}" />

Will render:
  <input data-foo="bar" data-baz="foos" ...

This is a backport from TYPO3.Fluid

Resolves: #61351
Related: #35748
Releases: 6.3
Change-Id: I3a6483e3293a7e18a3ee7fb5b04bf28bdb19d63d
Reviewed-on: http://review.typo3.org/32590
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Martin Sonnenholzer <martin.sonnenholzer@googlemail.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/fluid/Classes/Core/ViewHelper/AbstractTagBasedViewHelper.php
typo3/sysext/fluid/Tests/Unit/Core/ViewHelper/AbstractTagBasedViewHelperTest.php

index af31364..7a1fb72 100644 (file)
@@ -51,6 +51,7 @@ abstract class AbstractTagBasedViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelp
         */
        public function __construct() {
                $this->registerArgument('additionalAttributes', 'array', 'Additional tag attributes. They will be added directly to the resulting HTML tag.', FALSE);
+               $this->registerArgument('data', 'array', 'Additional data-* attributes. They will each be added with a "data-" prefix.', FALSE);
        }
 
        /**
@@ -71,6 +72,12 @@ abstract class AbstractTagBasedViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelp
                        $this->tag->addAttributes($this->arguments['additionalAttributes']);
                }
 
+               if ($this->hasArgument('data') && is_array($this->arguments['data'])) {
+                       foreach ($this->arguments['data'] as $dataAttributeKey => $dataAttributeValue) {
+                               $this->tag->addAttribute('data-' . $dataAttributeKey, $dataAttributeValue);
+                       }
+               }
+
                if (isset(self::$tagAttributes[get_class($this)])) {
                        foreach (self::$tagAttributes[get_class($this)] as $attributeName) {
                                if ($this->hasArgument($attributeName) && $this->arguments[$attributeName] !== '') {
index cc1d061..e3de50f 100644 (file)
@@ -67,6 +67,21 @@ class AbstractTagBasedViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
        /**
         * @test
         */
+       public function dataAttributesAreRenderedCorrectly() {
+               $mockTagBuilder = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\ViewHelper\\TagBuilder', array('addAttribute'), array(), '', FALSE);
+               $mockTagBuilder->expects($this->at(0))->method('addAttribute')->with('data-foo', 'bar');
+               $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('data-baz', 'foos');
+               $this->viewHelper->_set('tag', $mockTagBuilder);
+
+               $arguments = array('data' => array('foo' => 'bar', 'baz' => 'foos'));
+               $this->viewHelper->setArguments($arguments);
+               $this->viewHelper->initializeArguments();
+               $this->viewHelper->initialize();
+       }
+
+       /**
+        * @test
+        */
        public function standardTagAttributesAreRegistered() {
                $mockTagBuilder = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\ViewHelper\\TagBuilder', array('addAttribute'), array(), '', FALSE);
                $mockTagBuilder->expects($this->at(0))->method('addAttribute')->with('class', 'classAttribute');