TYPO3 API  SVNRelease
AbstractTagBasedViewHelper.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script belongs to the FLOW3 package "Fluid".                      *
00005  *                                                                        *
00006  * It is free software; you can redistribute it and/or modify it under    *
00007  * the terms of the GNU Lesser General Public License as published by the *
00008  * Free Software Foundation, either version 3 of the License, or (at your *
00009  * option) any later version.                                             *
00010  *                                                                        *
00011  * This script is distributed in the hope that it will be useful, but     *
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00013  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
00014  * General Public License for more details.                               *
00015  *                                                                        *
00016  * You should have received a copy of the GNU Lesser General Public       *
00017  * License along with the script.                                         *
00018  * If not, see http://www.gnu.org/licenses/lgpl.html                      *
00019  *                                                                        *
00020  * The TYPO3 project - inspiring people to share!                         *
00021  *                                                                        */
00022 
00023 /**
00024  * Tag based view helper.
00025  * Sould be used as the base class for all view helpers which output simple tags, as it provides some
00026  * convenience methods to register default attributes, ...
00027  *
00028  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00029  * @api
00030  */
00031 abstract class Tx_Fluid_Core_ViewHelper_AbstractTagBasedViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
00032 
00033     /**
00034      * Names of all registered tag attributes
00035      * @var array
00036      */
00037     protected $tagAttributes = array();
00038 
00039     /**
00040      * Tag builder instance
00041      *
00042      * @var Tx_Fluid_Core_ViewHelper_TagBuilder
00043      * @api
00044      */
00045     protected $tag = NULL;
00046 
00047     /**
00048      * name of the tag to be created by this view helper
00049      *
00050      * @var string
00051      * @api
00052      */
00053     protected $tagName = 'div';
00054 
00055     /**
00056      * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager
00057      * @return void
00058      */
00059     public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
00060         $this->objectManager = $objectManager;
00061         $this->tag = $this->objectManager->create('Tx_Fluid_Core_ViewHelper_TagBuilder');
00062     }
00063 
00064     /**
00065      * Constructor
00066      *
00067      * @author Sebastian Kurfürst <sebastian@typo3.org>
00068      * @api
00069      */
00070     public function __construct() {
00071         $this->registerArgument('additionalAttributes', 'array', 'Additional tag attributes. They will be added directly to the resulting HTML tag.', FALSE);
00072     }
00073 
00074     /**
00075      * Sets the tag name to $this->tagName.
00076      * Additionally, sets all tag attributes which were registered in
00077      * $this->tagAttributes and additionalArguments.
00078      *
00079      * Will be invoked just before the render method.
00080      *
00081      * @return void
00082      * @author Bastian Waidelich <bastian@typo3.org>
00083      * @api
00084      */
00085     public function initialize() {
00086         parent::initialize();
00087         $this->tag->reset();
00088         $this->tag->setTagName($this->tagName);
00089         if (is_array($this->arguments['additionalAttributes'])) {
00090             $this->tag->addAttributes($this->arguments['additionalAttributes']);
00091         }
00092         foreach ($this->tagAttributes as $attributeName) {
00093             if ($this->arguments->hasArgument($attributeName) && $this->arguments[$attributeName] !== '') {
00094                 $this->tag->addAttribute($attributeName, $this->arguments[$attributeName]);
00095             }
00096         }
00097     }
00098 
00099     /**
00100      * Register a new tag attribute. Tag attributes are all arguments which will be directly appended to a tag if you call $this->initializeTag()
00101      *
00102      * @param string $name Name of tag attribute
00103      * @param string $type Type of the tag attribute
00104      * @param string $description Description of tag attribute
00105      * @param boolean $required set to TRUE if tag attribute is required. Defaults to FALSE.
00106      * @return void
00107      * @author Sebastian Kurfürst <sebastian@typo3.org>
00108      * @api
00109      */
00110     protected function registerTagAttribute($name, $type, $description, $required = FALSE) {
00111         $this->registerArgument($name, $type, $description, $required, NULL);
00112         $this->tagAttributes[] = $name;
00113     }
00114 
00115     /**
00116      * Registers all standard HTML universal attributes.
00117      * Should be used inside registerArguments();
00118      *
00119      * @return void
00120      * @author Sebastian Kurfürst <sebastian@typo3.org>
00121      * @api
00122      */
00123     protected function registerUniversalTagAttributes() {
00124         $this->registerTagAttribute('class', 'string', 'CSS class(es) for this element');
00125         $this->registerTagAttribute('dir', 'string', 'Text direction for this HTML element. Allowed strings: "ltr" (left to right), "rtl" (right to left)');
00126         $this->registerTagAttribute('id', 'string', 'Unique (in this file) identifier for this HTML element.');
00127         $this->registerTagAttribute('lang', 'string', 'Language for this element. Use short names specified in RFC 1766');
00128         $this->registerTagAttribute('style', 'string', 'Individual CSS styles for this element');
00129         $this->registerTagAttribute('title', 'string', 'Tooltip text of element');
00130         $this->registerTagAttribute('accesskey', 'string', 'Keyboard shortcut to access this element');
00131         $this->registerTagAttribute('tabindex', 'integer', 'Specifies the tab order of this element');
00132         $this->registerTagAttribute('onclick', 'string', 'JavaScript evaluated for the onclick event');
00133     }
00134 }
00135 ?>