TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
AbstractTagBasedViewHelper.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\Core\ViewHelper;
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 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 
81  public function initialize()
82  {
83  parent::initialize();
84  $this->tag->reset();
85  $this->tag->setTagName($this->tagName);
86  if ($this->hasArgument('additionalAttributes') && is_array($this->arguments['additionalAttributes'])) {
87  $this->tag->addAttributes($this->arguments['additionalAttributes']);
88  }
89 
90  if ($this->hasArgument('data') && is_array($this->arguments['data'])) {
91  foreach ($this->arguments['data'] as $dataAttributeKey => $dataAttributeValue) {
92  $this->tag->addAttribute('data-' . $dataAttributeKey, $dataAttributeValue);
93  }
94  }
95 
96  if (isset(self::$tagAttributes[get_class($this)])) {
97  foreach (self::$tagAttributes[get_class($this)] as $attributeName) {
98  if ($this->hasArgument($attributeName) && $this->arguments[$attributeName] !== '') {
99  $this->tag->addAttribute($attributeName, $this->arguments[$attributeName]);
100  }
101  }
102  }
103  }
104 
116  protected function registerTagAttribute($name, $type, $description, $required = false, $default = null)
117  {
118  $this->registerArgument($name, $type, $description, $required, $default);
119  self::$tagAttributes[get_class($this)][$name] = $name;
120  }
121 
129  protected function registerUniversalTagAttributes()
130  {
131  $this->registerTagAttribute('class', 'string', 'CSS class(es) for this element');
132  $this->registerTagAttribute('dir', 'string', 'Text direction for this HTML element. Allowed strings: "ltr" (left to right), "rtl" (right to left)');
133  $this->registerTagAttribute('id', 'string', 'Unique (in this file) identifier for this HTML element.');
134  $this->registerTagAttribute('lang', 'string', 'Language for this element. Use short names specified in RFC 1766');
135  $this->registerTagAttribute('style', 'string', 'Individual CSS styles for this element');
136  $this->registerTagAttribute('title', 'string', 'Tooltip text of element');
137  $this->registerTagAttribute('accesskey', 'string', 'Keyboard shortcut to access this element');
138  $this->registerTagAttribute('tabindex', 'integer', 'Specifies the tab order of this element');
139  $this->registerTagAttribute('onclick', 'string', 'JavaScript evaluated for the onclick event');
140  }
141 }
registerTagAttribute($name, $type, $description, $required=false, $default=null)