‪TYPO3CMS  10.4
CheckboxViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
62 {
66  protected ‪$tagName = 'input';
67 
71  public function ‪initializeArguments()
72  {
73  parent::initializeArguments();
74  $this->registerTagAttribute(
75  'disabled',
76  'string',
77  'Specifies that the input element should be disabled when the page loads'
78  );
79  $this->registerArgument(
80  'errorClass',
81  'string',
82  'CSS class to set if there are errors for this ViewHelper',
83  false,
84  'f3-form-error'
85  );
86  $this->overrideArgument('value', 'string', 'Value of input tag. Required for checkboxes', true);
87  $this->registerUniversalTagAttributes();
88  $this->registerArgument('checked', 'bool', 'Specifies that the input element should be preselected');
89  $this->registerArgument('multiple', 'bool', 'Specifies whether this checkbox belongs to a multivalue (is part of a checkbox group)', false, false);
90  }
91 
98  public function ‪render()
99  {
100  $checked = $this->arguments['checked'];
101  $multiple = $this->arguments['multiple'];
102 
103  $this->tag->addAttribute('type', 'checkbox');
104 
105  $nameAttribute = $this->‪getName();
106  $valueAttribute = $this->‪getValueAttribute();
107  $propertyValue = null;
108  if ($this->‪hasMappingErrorOccurred()) {
109  $propertyValue = $this->‪getLastSubmittedFormData();
110  }
111  if ($checked === null && $propertyValue === null) {
112  $propertyValue = $this->‪getPropertyValue();
113  }
114 
115  if ($propertyValue instanceof \Traversable) {
116  $propertyValue = iterator_to_array($propertyValue);
117  }
118  if (is_array($propertyValue)) {
119  $propertyValue = array_map([$this, 'convertToPlainValue'], $propertyValue);
120  if ($checked === null) {
121  $checked = in_array($valueAttribute, $propertyValue);
122  }
123  $nameAttribute .= '[]';
124  } elseif ($multiple === true) {
125  $nameAttribute .= '[]';
126  } elseif ($propertyValue !== null) {
127  $checked = (boolean)$propertyValue === (boolean)$valueAttribute;
128  }
129 
130  $this->‪registerFieldNameForFormTokenGeneration($nameAttribute);
131  $this->tag->addAttribute('name', $nameAttribute);
132  $this->tag->addAttribute('value', $valueAttribute);
133  if ($checked === true) {
134  $this->tag->addAttribute('checked', 'checked');
135  }
136 
137  $this->‪setErrorClassAttribute();
138  $hiddenField = $this->‪renderHiddenFieldForEmptyValue();
139  return $hiddenField . $this->tag->render();
140  }
141 }
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\setErrorClassAttribute
‪setErrorClassAttribute()
Definition: AbstractFormFieldViewHelper.php:331
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getPropertyValue
‪mixed getPropertyValue()
Definition: AbstractFormFieldViewHelper.php:295
‪TYPO3\CMS\Fluid\ViewHelpers\Form
Definition: AbstractFormFieldViewHelper.php:16
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getLastSubmittedFormData
‪mixed getLastSubmittedFormData()
Definition: AbstractFormFieldViewHelper.php:229
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\registerFieldNameForFormTokenGeneration
‪registerFieldNameForFormTokenGeneration($fieldName)
Definition: AbstractFormViewHelper.php:106
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\renderHiddenFieldForEmptyValue
‪string renderHiddenFieldForEmptyValue()
Definition: AbstractFormFieldViewHelper.php:374
‪TYPO3\CMS\Fluid\ViewHelpers\Form\CheckboxViewHelper\initializeArguments
‪initializeArguments()
Definition: CheckboxViewHelper.php:70
‪TYPO3\CMS\Fluid\ViewHelpers\Form\CheckboxViewHelper\$tagName
‪string $tagName
Definition: CheckboxViewHelper.php:65
‪TYPO3\CMS\Fluid\ViewHelpers\Form\CheckboxViewHelper
Definition: CheckboxViewHelper.php:62
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
Definition: AbstractFormFieldViewHelper.php:30
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\hasMappingErrorOccurred
‪bool hasMappingErrorOccurred()
Definition: AbstractFormFieldViewHelper.php:218
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getName
‪string getName()
Definition: AbstractFormFieldViewHelper.php:90
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getValueAttribute
‪mixed getValueAttribute()
Definition: AbstractFormFieldViewHelper.php:151
‪TYPO3\CMS\Fluid\ViewHelpers\Form\CheckboxViewHelper\render
‪string render()
Definition: CheckboxViewHelper.php:97