Skip to content
Snippets Groups Projects
Commit c839a4dc authored by Frank Nägler's avatar Frank Nägler Committed by Anja Leichsenring
Browse files

[FEATURE] Add generic data-* attribute

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: default avatarWouter Wolters <typo3@wouterwolters.nl>
Tested-by: default avatarWouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: default avatarMartin Sonnenholzer <martin.sonnenholzer@googlemail.com>
Reviewed-by: default avatarAnja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: default avatarAnja Leichsenring <aleichsenring@ab-softlab.de>
parent e6a68819
No related merge requests found
......@@ -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] !== '') {
......
......@@ -64,6 +64,21 @@ class AbstractTagBasedViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
$this->viewHelper->initialize();
}
/**
* @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
*/
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment