TYPO3  7.6
CheckboxViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 namespace TYPO3\CMS\Fluid\ViewHelpers\Form;
4 
5 /* *
6  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
7  * *
8  * It is free software; you can redistribute it and/or modify it under *
9  * the terms of the GNU Lesser General Public License, either version 3 *
10  * of the License, or (at your option) any later version. *
11  * *
12  * The TYPO3 project - inspiring people to share! *
13  * */
14 
46 {
50  protected $tagName = 'input';
51 
58  public function initializeArguments()
59  {
60  parent::initializeArguments();
61  $this->registerTagAttribute(
62  'disabled', 'string', 'Specifies that the input element should be disabled when the page loads'
63  );
64  $this->registerArgument(
65  'errorClass', 'string', 'CSS class to set if there are errors for this view helper', false, 'f3-form-error'
66  );
67  $this->overrideArgument('value', 'string', 'Value of input tag. Required for checkboxes', true);
69  }
70 
80  public function render($checked = null, $multiple = null)
81  {
82  $this->tag->addAttribute('type', 'checkbox');
83 
84  $nameAttribute = $this->getName();
85  $valueAttribute = $this->getValueAttribute();
86  $propertyValue = null;
87  if ($this->hasMappingErrorOccurred()) {
88  $propertyValue = $this->getLastSubmittedFormData();
89  }
90  if ($checked === null && $propertyValue === null) {
91  $propertyValue = $this->getPropertyValue();
92  }
93 
94  if ($propertyValue instanceof \Traversable) {
95  $propertyValue = iterator_to_array($propertyValue);
96  }
97  if (is_array($propertyValue)) {
98  $propertyValue = array_map(array($this, 'convertToPlainValue'), $propertyValue);
99  if ($checked === null) {
100  $checked = in_array($valueAttribute, $propertyValue);
101  }
102  $nameAttribute .= '[]';
103  } elseif ($multiple === true) {
104  $nameAttribute .= '[]';
105  } elseif ($propertyValue !== null) {
106  $checked = (boolean) $propertyValue === (boolean) $valueAttribute;
107  }
108 
109  $this->registerFieldNameForFormTokenGeneration($nameAttribute);
110  $this->tag->addAttribute('name', $nameAttribute);
111  $this->tag->addAttribute('value', $valueAttribute);
112  if ($checked === true) {
113  $this->tag->addAttribute('checked', 'checked');
114  }
115 
116  $this->setErrorClassAttribute();
117  $hiddenField = $this->renderHiddenFieldForEmptyValue();
118  return $hiddenField . $this->tag->render();
119  }
120 }