TYPO3 CMS  TYPO3_8-7
AbstractTagBasedViewHelper.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
25 {
31  private static $tagAttributes = [];
32 
39  protected $tagName = 'div';
40 
44  protected $tag;
45 
49  protected $escapeOutput = false;
50 
54  public function __construct()
55  {
56  $this->tag = new \TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder();
57  }
58 
64  public function initializeArguments()
65  {
66  $this->registerArgument('additionalAttributes', 'array', 'Additional tag attributes. They will be added directly to the resulting HTML tag.', false);
67  $this->registerArgument('data', 'array', 'Additional data-* attributes. They will each be added with a "data-" prefix.', false);
68  parent::initializeArguments();
69  }
70 
80  public function initialize()
81  {
82  parent::initialize();
83  $this->tag->reset();
84  $this->tag->setTagName($this->tagName);
85  if ($this->hasArgument('additionalAttributes') && is_array($this->arguments['additionalAttributes'])) {
86  $this->tag->addAttributes($this->arguments['additionalAttributes']);
87  }
88 
89  if ($this->hasArgument('data') && is_array($this->arguments['data'])) {
90  foreach ($this->arguments['data'] as $dataAttributeKey => $dataAttributeValue) {
91  $this->tag->addAttribute('data-' . $dataAttributeKey, $dataAttributeValue);
92  }
93  }
94 
95  if (isset(self::$tagAttributes[get_class($this)])) {
96  foreach (self::$tagAttributes[get_class($this)] as $attributeName) {
97  if ($this->hasArgument($attributeName) && $this->arguments[$attributeName] !== '') {
98  $this->tag->addAttribute($attributeName, $this->arguments[$attributeName]);
99  }
100  }
101  }
102  }
103 
114  protected function registerTagAttribute($name, $type, $description, $required = false, $default = null)
115  {
116  $this->registerArgument($name, $type, $description, $required, $default);
117  self::$tagAttributes[get_class($this)][$name] = $name;
118  }
119 
126  protected function registerUniversalTagAttributes()
127  {
128  $this->registerTagAttribute('class', 'string', 'CSS class(es) for this element');
129  $this->registerTagAttribute('dir', 'string', 'Text direction for this HTML element. Allowed strings: "ltr" (left to right), "rtl" (right to left)');
130  $this->registerTagAttribute('id', 'string', 'Unique (in this file) identifier for this HTML element.');
131  $this->registerTagAttribute('lang', 'string', 'Language for this element. Use short names specified in RFC 1766');
132  $this->registerTagAttribute('style', 'string', 'Individual CSS styles for this element');
133  $this->registerTagAttribute('title', 'string', 'Tooltip text of element');
134  $this->registerTagAttribute('accesskey', 'string', 'Keyboard shortcut to access this element');
135  $this->registerTagAttribute('tabindex', 'integer', 'Specifies the tab order of this element');
136  $this->registerTagAttribute('onclick', 'string', 'JavaScript evaluated for the onclick event');
137  }
138 }
registerTagAttribute($name, $type, $description, $required=false, $default=null)