[TYPO3-mvc] Re: LabelViewHelper

Nils Blattner nb at cabag.ch
Fri Feb 19 10:30:52 CET 2010


This is basically a copy of <insert random form ViewHelper here> with 
the addition of the *-required div. But it was requested, so here goes:

Usage:
Just the same as any form field helper, but sadly this results in quite 
ugly for="" attributes.
It works, but could be improved there for sure.


<?php

/***************************************************************
*  Copyright notice
*
*  (c) 2009 Nils Blattner <nb at cabag.ch>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

/**
  * View Helper which creates a label with an error class should there 
be an error.
  *
  * @version $Id:$
  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General 
Public License, version 3 or later
  * @api
  * @scope prototype
  */
class Tx_CabagSteps_ViewHelpers_LabelViewHelper extends 
Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper {

	/**
	 * @var string
	 */
	protected $tagName = 'label';

	/**
	 * Arguments initialization
	 *
	 * @return void
	 * @author Bastian Waidelich <bastian at typo3.org>
	 */
	public function initializeArguments() {
		$this->registerUniversalTagAttributes();
	}
	/**
	 * Renders the label.
	 * Only works for flat forms! (aka forms that directly correspond to an 
object)
	 *
	 * @param string $property the property the label belongs to
	 * @param string $required if the element is required
	 * @return string
	 * @author Nils Blattner <nb at cabag.ch>
	 * @api
	 */
	public function render($property = null, $required = null) {
		$errors = $this->controllerContext->getRequest()->getErrors();
		$this->tag->setContent($this->renderChildren());
		
		if ($property != null) {
			$this->tag->addAttribute('for', $property);
		} else {
			$property = '';
		}
		$this->setErrorClassAttribute();
		$requiredTag = '<div class="required';
		
		if ($this->arguments->hasArgument('class')) {
			$requiredTag .= ' '.$this->arguments['class'] . ' ';
		}
		$errors = $this->getErrorsForProperty();
		if (count($errors) > 0) {
			if ($this->arguments->hasArgument('errorClass')) {
				$requiredTag .= ' '.$this->arguments['errorClass'];
			} else {
				$requiredTag .= ' error';
			}
		}
		
		$requiredTag .= '">';
		if ($required != null) {
			$requiredTag .= $required;
		} else {
			$requiredTag .= '&nbsp;';
		}
		$requiredTag .= '</div>';
		
		$labelTag = $this->tag->render();
		return $labelTag.$requiredTag;
	}

	/**
	 * Get the name of this form element.
	 * Either returns arguments['name'], or the correct name for Object Access.
	 *
	 * In case property is something like bla.blubb (hierarchical), then 
[bla][blubb] is generated.
	 *
	 * @return string Name
	 * @author Sebastian Kurfürst <sebastian at typo3.org>
	 * @author Robert Lemke <robert at typo3.org>
	 * @author Karsten Dambekalns <karsten at typo3.org>
	 * @author Bastian Waidelich <bastian at typo3.org>
	 */
	protected function getName() {
		if ($this->isObjectAccessorMode()) {
			$formName = 
$this->viewHelperVariableContainer->get('Tx_Fluid_ViewHelpers_FormViewHelper', 
'formName');
			if (!empty($formName)) {
				$propertySegments = explode('.', $this->arguments['property']);
				$properties = '';
				foreach ($propertySegments as $segment) {
					$properties .= '[' . $segment . ']';
				}
				$name = $formName . $properties;
			} else {
				$name = $this->arguments['property'];
			}
		} else {
			$name = $this->arguments['name'];
		}
		if ($this->arguments->hasArgument('value') && 
is_object($this->arguments['value'])) {
			if (NULL !== 
$this->persistenceManager->getBackend()->getIdentifierByObject($this->arguments['value'])
				&& 
(!$this->persistenceManager->getBackend()->isNewObject($this->arguments['value']))) 
{
				$name .= '[__identity]';
			}
		}
		return $this->prefixFieldName($name);
	}

	/**
	 * Internal method which checks if we should evaluate a domain object 
or just output arguments['name'] and arguments['value']
	 *
	 * @return boolean TRUE if we should evaluate the domain object, FALSE 
otherwise.
	 * @author Sebastian Kurfürst <sebastian at typo3.org>
	 */
	protected function isObjectAccessorMode() {
		return $this->arguments->hasArgument('property')
			&& 
$this->viewHelperVariableContainer->exists('Tx_Fluid_ViewHelpers_FormViewHelper', 
'formName');
	}

}

?>


More information about the TYPO3-project-typo3v4mvc mailing list