TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
fluid/Classes/ViewHelpers/Form/CheckboxViewHelper.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Form;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
48 {
52  protected $tagName = 'input';
53 
60  public function initializeArguments()
61  {
62  parent::initializeArguments();
63  $this->registerTagAttribute(
64  'disabled', 'string', 'Specifies that the input element should be disabled when the page loads'
65  );
66  $this->registerArgument(
67  'errorClass', 'string', 'CSS class to set if there are errors for this view helper', false, 'f3-form-error'
68  );
69  $this->overrideArgument('value', 'string', 'Value of input tag. Required for checkboxes', true);
71  $this->registerArgument('checked', 'bool', 'Specifies that the input element should be preselected');
72  $this->registerArgument('multiple', 'bool', 'Specifies whether this checkbox belongs to a multivalue (is part of a checkbox group)', false, false);
73  }
74 
82  public function render()
83  {
84  $checked = $this->arguments['checked'];
85  $multiple = $this->arguments['multiple'];
86 
87  $this->tag->addAttribute('type', 'checkbox');
88 
89  $nameAttribute = $this->getName();
90  $valueAttribute = $this->getValueAttribute();
91  $propertyValue = null;
92  if ($this->hasMappingErrorOccurred()) {
93  $propertyValue = $this->getLastSubmittedFormData();
94  }
95  if ($checked === null && $propertyValue === null) {
96  $propertyValue = $this->getPropertyValue();
97  }
98 
99  if ($propertyValue instanceof \Traversable) {
100  $propertyValue = iterator_to_array($propertyValue);
101  }
102  if (is_array($propertyValue)) {
103  $propertyValue = array_map([$this, 'convertToPlainValue'], $propertyValue);
104  if ($checked === null) {
105  $checked = in_array($valueAttribute, $propertyValue);
106  }
107  $nameAttribute .= '[]';
108  } elseif ($multiple === true) {
109  $nameAttribute .= '[]';
110  } elseif ($propertyValue !== null) {
111  $checked = (boolean) $propertyValue === (boolean) $valueAttribute;
112  }
113 
114  $this->registerFieldNameForFormTokenGeneration($nameAttribute);
115  $this->tag->addAttribute('name', $nameAttribute);
116  $this->tag->addAttribute('value', $valueAttribute);
117  if ($checked === true) {
118  $this->tag->addAttribute('checked', 'checked');
119  }
120 
121  $this->setErrorClassAttribute();
122  $hiddenField = $this->renderHiddenFieldForEmptyValue();
123  return $hiddenField . $this->tag->render();
124  }
125 }
registerTagAttribute($name, $type, $description, $required=false, $default=null)