TYPO3 CMS  TYPO3_7-6
AbstractTagBasedViewHelper.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
13 
22 {
28  private static $tagAttributes = [];
29 
37  protected $tag = null;
38 
45  protected $tagName = 'div';
46 
52  public function __construct()
53  {
54  $this->registerArgument('additionalAttributes', 'array', 'Additional tag attributes. They will be added directly to the resulting HTML tag.', false);
55  $this->registerArgument('data', 'array', 'Additional data-* attributes. They will each be added with a "data-" prefix.', false);
56  }
57 
68  public function initialize()
69  {
70  parent::initialize();
71  $this->tag->reset();
72  $this->tag->setTagName($this->tagName);
73  if ($this->hasArgument('additionalAttributes') && is_array($this->arguments['additionalAttributes'])) {
74  $this->tag->addAttributes($this->arguments['additionalAttributes']);
75  }
76 
77  if ($this->hasArgument('data') && is_array($this->arguments['data'])) {
78  foreach ($this->arguments['data'] as $dataAttributeKey => $dataAttributeValue) {
79  $this->tag->addAttribute('data-' . $dataAttributeKey, $dataAttributeValue);
80  }
81  }
82 
83  if (isset(self::$tagAttributes[get_class($this)])) {
84  foreach (self::$tagAttributes[get_class($this)] as $attributeName) {
85  if ($this->hasArgument($attributeName) && $this->arguments[$attributeName] !== '') {
86  $this->tag->addAttribute($attributeName, $this->arguments[$attributeName]);
87  }
88  }
89  }
90  }
91 
103  protected function registerTagAttribute($name, $type, $description, $required = false, $default = null)
104  {
105  $this->registerArgument($name, $type, $description, $required, $default);
106  self::$tagAttributes[get_class($this)][$name] = $name;
107  }
108 
116  protected function registerUniversalTagAttributes()
117  {
118  $this->registerTagAttribute('class', 'string', 'CSS class(es) for this element');
119  $this->registerTagAttribute('dir', 'string', 'Text direction for this HTML element. Allowed strings: "ltr" (left to right), "rtl" (right to left)');
120  $this->registerTagAttribute('id', 'string', 'Unique (in this file) identifier for this HTML element.');
121  $this->registerTagAttribute('lang', 'string', 'Language for this element. Use short names specified in RFC 1766');
122  $this->registerTagAttribute('style', 'string', 'Individual CSS styles for this element');
123  $this->registerTagAttribute('title', 'string', 'Tooltip text of element');
124  $this->registerTagAttribute('accesskey', 'string', 'Keyboard shortcut to access this element');
125  $this->registerTagAttribute('tabindex', 'integer', 'Specifies the tab order of this element');
126  $this->registerTagAttribute('onclick', 'string', 'JavaScript evaluated for the onclick event');
127  }
128 }
registerArgument($name, $type, $description, $required=false, $defaultValue=null)
registerTagAttribute($name, $type, $description, $required=false, $default=null)