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