[BUGFIX] Wording 78169-IntroduceTranslationSourceFieldForTt_content.rst
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / Core / ViewHelper / AbstractTagBasedViewHelper.php
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
17 /**
18 * Tag based view helper.
19 * Sould be used as the base class for all view helpers which output simple tags, as it provides some
20 * convenience methods to register default attributes, ...
21 *
22 * @api
23 */
24 abstract class AbstractTagBasedViewHelper extends AbstractViewHelper
25 {
26 /**
27 * Names of all registered tag attributes
28 *
29 * @var array
30 */
31 private static $tagAttributes = [];
32
33 /**
34 * name of the tag to be created by this view helper
35 *
36 * @var string
37 * @api
38 */
39 protected $tagName = 'div';
40
41 /**
42 * @var \TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder
43 */
44 protected $tag;
45
46 /**
47 * @var bool
48 */
49 protected $escapeOutput = false;
50
51 /**
52 * Constructor
53 */
54 public function __construct()
55 {
56 $this->tag = new \TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder();
57 }
58
59 /**
60 * Constructor
61 *
62 * @api
63 */
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
71 /**
72 * Sets the tag name to $this->tagName.
73 * Additionally, sets all tag attributes which were registered in
74 * $this->tagAttributes and additionalArguments.
75 *
76 * Will be invoked just before the render method.
77 *
78 * @api
79 */
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[static::class])) {
96 foreach (self::$tagAttributes[static::class] as $attributeName) {
97 if ($this->hasArgument($attributeName) && $this->arguments[$attributeName] !== '') {
98 $this->tag->addAttribute($attributeName, $this->arguments[$attributeName]);
99 }
100 }
101 }
102 }
103
104 /**
105 * Register a new tag attribute. Tag attributes are all arguments which will be directly appended to a tag if you call $this->initializeTag()
106 *
107 * @param string $name Name of tag attribute
108 * @param string $type Type of the tag attribute
109 * @param string $description Description of tag attribute
110 * @param bool $required set to TRUE if tag attribute is required. Defaults to FALSE.
111 * @param string $default Default value
112 * @api
113 */
114 protected function registerTagAttribute($name, $type, $description, $required = false, $default = null)
115 {
116 $this->registerArgument($name, $type, $description, $required, $default);
117 self::$tagAttributes[static::class][$name] = $name;
118 }
119
120 /**
121 * Registers all standard HTML universal attributes.
122 * Should be used inside registerArguments();
123 *
124 * @api
125 */
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 }