[TASK] Sync CMS Fluid with Flow Fluid 1.1 (part1)
[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 * @api
35 */
36 protected $tag = NULL;
37
38 /**
39 * name of the tag to be created by this view helper
40 *
41 * @var string
42 * @api
43 */
44 protected $tagName = 'div';
45
46 /**
47 * Inject a TagBuilder
48 *
49 * @param \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder $tagBuilder Tag builder
50 * @return void
51 */
52 public function injectTagBuilder(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder $tagBuilder) {
53 $this->tag = $tagBuilder;
54 }
55
56 /**
57 * Constructor
58 *
59 * @api
60 */
61 public function __construct() {
62 $this->registerArgument('additionalAttributes', 'array', 'Additional tag attributes. They will be added directly to the resulting HTML tag.', FALSE);
63 }
64
65 /**
66 * Sets the tag name to $this->tagName.
67 * Additionally, sets all tag attributes which were registered in
68 * $this->tagAttributes and additionalArguments.
69 *
70 * Will be invoked just before the render method.
71 *
72 * @return void
73 * @api
74 */
75 public function initialize() {
76 parent::initialize();
77 $this->tag->reset();
78 $this->tag->setTagName($this->tagName);
79 if ($this->hasArgument('additionalAttributes') && is_array($this->arguments['additionalAttributes'])) {
80 $this->tag->addAttributes($this->arguments['additionalAttributes']);
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
92 /**
93 * Register a new tag attribute. Tag attributes are all arguments which will be directly appended to a tag if you call $this->initializeTag()
94 *
95 * @param string $name Name of tag attribute
96 * @param string $type Type of the tag attribute
97 * @param string $description Description of tag attribute
98 * @param boolean $required set to TRUE if tag attribute is required. Defaults to FALSE.
99 * @param string $default Default value
100 * @return void
101 * @api
102 */
103 protected function registerTagAttribute($name, $type, $description, $required = FALSE, $default = NULL) {
104 $this->registerArgument($name, $type, $description, $required, $default);
105 self::$tagAttributes[get_class($this)][$name] = $name;
106 }
107
108 /**
109 * Registers all standard HTML universal attributes.
110 * Should be used inside registerArguments();
111 *
112 * @return void
113 * @api
114 */
115 protected function registerUniversalTagAttributes() {
116 $this->registerTagAttribute('class', 'string', 'CSS class(es) for this element');
117 $this->registerTagAttribute('dir', 'string', 'Text direction for this HTML element. Allowed strings: "ltr" (left to right), "rtl" (right to left)');
118 $this->registerTagAttribute('id', 'string', 'Unique (in this file) identifier for this HTML element.');
119 $this->registerTagAttribute('lang', 'string', 'Language for this element. Use short names specified in RFC 1766');
120 $this->registerTagAttribute('style', 'string', 'Individual CSS styles for this element');
121 $this->registerTagAttribute('title', 'string', 'Tooltip text of element');
122 $this->registerTagAttribute('accesskey', 'string', 'Keyboard shortcut to access this element');
123 $this->registerTagAttribute('tabindex', 'integer', 'Specifies the tab order of this element');
124 $this->registerTagAttribute('onclick', 'string', 'JavaScript evaluated for the onclick event');
125 }
126 }
127
128 ?>