[TASK] Make \Be\Menus\ActionMenuItemViewHelper compilable
[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 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
14 /**
15 * Tag based view helper.
16 * Sould be used as the base class for all view helpers which output simple tags, as it provides some
17 * convenience methods to register default attributes, ...
18 *
19 * @api
20 */
21 abstract class AbstractTagBasedViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
22
23 /**
24 * Names of all registered tag attributes
25 *
26 * @var array
27 */
28 static private $tagAttributes = array();
29
30 /**
31 * Tag builder instance
32 *
33 * @var \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder
34 * @inject
35 * @api
36 */
37 protected $tag = NULL;
38
39 /**
40 * name of the tag to be created by this view helper
41 *
42 * @var string
43 * @api
44 */
45 protected $tagName = 'div';
46
47 /**
48 * Constructor
49 *
50 * @api
51 */
52 public function __construct() {
53 $this->registerArgument('additionalAttributes', 'array', 'Additional tag attributes. They will be added directly to the resulting HTML tag.', FALSE);
54 $this->registerArgument('data', 'array', 'Additional data-* attributes. They will each be added with a "data-" prefix.', FALSE);
55 }
56
57 /**
58 * Sets the tag name to $this->tagName.
59 * Additionally, sets all tag attributes which were registered in
60 * $this->tagAttributes and additionalArguments.
61 *
62 * Will be invoked just before the render method.
63 *
64 * @return void
65 * @api
66 */
67 public function initialize() {
68 parent::initialize();
69 $this->tag->reset();
70 $this->tag->setTagName($this->tagName);
71 if ($this->hasArgument('additionalAttributes') && is_array($this->arguments['additionalAttributes'])) {
72 $this->tag->addAttributes($this->arguments['additionalAttributes']);
73 }
74
75 if ($this->hasArgument('data') && is_array($this->arguments['data'])) {
76 foreach ($this->arguments['data'] as $dataAttributeKey => $dataAttributeValue) {
77 $this->tag->addAttribute('data-' . $dataAttributeKey, $dataAttributeValue);
78 }
79 }
80
81 if (isset(self::$tagAttributes[get_class($this)])) {
82 foreach (self::$tagAttributes[get_class($this)] as $attributeName) {
83 if ($this->hasArgument($attributeName) && $this->arguments[$attributeName] !== '') {
84 $this->tag->addAttribute($attributeName, $this->arguments[$attributeName]);
85 }
86 }
87 }
88 }
89
90 /**
91 * Register a new tag attribute. Tag attributes are all arguments which will be directly appended to a tag if you call $this->initializeTag()
92 *
93 * @param string $name Name of tag attribute
94 * @param string $type Type of the tag attribute
95 * @param string $description Description of tag attribute
96 * @param bool $required set to TRUE if tag attribute is required. Defaults to FALSE.
97 * @param string $default Default value
98 * @return void
99 * @api
100 */
101 protected function registerTagAttribute($name, $type, $description, $required = FALSE, $default = NULL) {
102 $this->registerArgument($name, $type, $description, $required, $default);
103 self::$tagAttributes[get_class($this)][$name] = $name;
104 }
105
106 /**
107 * Registers all standard HTML universal attributes.
108 * Should be used inside registerArguments();
109 *
110 * @return void
111 * @api
112 */
113 protected function registerUniversalTagAttributes() {
114 $this->registerTagAttribute('class', 'string', 'CSS class(es) for this element');
115 $this->registerTagAttribute('dir', 'string', 'Text direction for this HTML element. Allowed strings: "ltr" (left to right), "rtl" (right to left)');
116 $this->registerTagAttribute('id', 'string', 'Unique (in this file) identifier for this HTML element.');
117 $this->registerTagAttribute('lang', 'string', 'Language for this element. Use short names specified in RFC 1766');
118 $this->registerTagAttribute('style', 'string', 'Individual CSS styles for this element');
119 $this->registerTagAttribute('title', 'string', 'Tooltip text of element');
120 $this->registerTagAttribute('accesskey', 'string', 'Keyboard shortcut to access this element');
121 $this->registerTagAttribute('tabindex', 'integer', 'Specifies the tab order of this element');
122 $this->registerTagAttribute('onclick', 'string', 'JavaScript evaluated for the onclick event');
123 }
124
125 /**
126 * Get the TagBuilder for renderStatic function
127 *
128 * @return TagBuilder
129 */
130 protected static function getTagBuilder(){
131 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(TagBuilder::class);
132 }
133
134 }