TYPO3 CMS  TYPO3_6-2
CheckboxViewHelper.php
Go to the documentation of this file.
1 <?php
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  * */
44 
48  protected $tagName = 'input';
49 
56  public function initializeArguments() {
57  parent::initializeArguments();
58  $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
59  $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', FALSE, 'f3-form-error');
60  $this->overrideArgument('value', 'string', 'Value of input tag. Required for checkboxes', TRUE);
62  }
63 
73  public function render($checked = NULL, $multiple = NULL) {
74  $this->tag->addAttribute('type', 'checkbox');
75 
76  $nameAttribute = $this->getName();
77  $valueAttribute = $this->getValue();
78  if ($this->isObjectAccessorMode()) {
79  if ($this->hasMappingErrorOccurred()) {
80  $propertyValue = $this->getLastSubmittedFormData();
81  } else {
82  $propertyValue = $this->getPropertyValue();
83  }
84 
85  if ($propertyValue instanceof \Traversable) {
86  $propertyValue = iterator_to_array($propertyValue);
87  }
88  if (is_array($propertyValue)) {
89  $propertyValue = array_map(array($this, 'convertToPlainValue'), $propertyValue);
90  if ($checked === NULL) {
91  $checked = in_array($valueAttribute, $propertyValue);
92  }
93  $nameAttribute .= '[]';
94  } elseif ($multiple === TRUE) {
95  $nameAttribute .= '[]';
96  } elseif ($checked === NULL && $propertyValue !== NULL) {
97  $checked = (bool)$propertyValue === (bool)$valueAttribute;
98  }
99  }
100  $this->registerFieldNameForFormTokenGeneration($nameAttribute);
101  $this->tag->addAttribute('name', $nameAttribute);
102  $this->tag->addAttribute('value', $valueAttribute);
103  if ($checked) {
104  $this->tag->addAttribute('checked', 'checked');
105  }
106  $this->setErrorClassAttribute();
107  $hiddenField = $this->renderHiddenFieldForEmptyValue();
108  return $hiddenField . $this->tag->render();
109  }
110 }
registerTagAttribute($name, $type, $description, $required=FALSE, $default=NULL)
registerArgument($name, $type, $description, $required=FALSE, $defaultValue=NULL)
overrideArgument($name, $type, $description, $required=FALSE, $defaultValue=NULL)