‪TYPO3CMS  9.5
CheckboxViewHelper.php
Go to the documentation of this file.
1 <?php
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 
61 {
65  protected ‪$tagName = 'input';
66 
70  public function ‪initializeArguments()
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 
97  public function ‪render()
98  {
99  $checked = $this->arguments['checked'];
100  $multiple = $this->arguments['multiple'];
101 
102  $this->tag->addAttribute('type', 'checkbox');
103 
104  $nameAttribute = $this->‪getName();
105  $valueAttribute = $this->‪getValueAttribute();
106  $propertyValue = null;
107  if ($this->‪hasMappingErrorOccurred()) {
108  $propertyValue = $this->‪getLastSubmittedFormData();
109  }
110  if ($checked === null && $propertyValue === null) {
111  $propertyValue = $this->‪getPropertyValue();
112  }
113 
114  if ($propertyValue instanceof \Traversable) {
115  $propertyValue = iterator_to_array($propertyValue);
116  }
117  if (is_array($propertyValue)) {
118  $propertyValue = array_map([$this, 'convertToPlainValue'], $propertyValue);
119  if ($checked === null) {
120  $checked = in_array($valueAttribute, $propertyValue);
121  }
122  $nameAttribute .= '[]';
123  } elseif ($multiple === true) {
124  $nameAttribute .= '[]';
125  } elseif ($propertyValue !== null) {
126  $checked = (boolean)$propertyValue === (boolean)$valueAttribute;
127  }
128 
129  $this->‪registerFieldNameForFormTokenGeneration($nameAttribute);
130  $this->tag->addAttribute('name', $nameAttribute);
131  $this->tag->addAttribute('value', $valueAttribute);
132  if ($checked === true) {
133  $this->tag->addAttribute('checked', 'checked');
134  }
135 
136  $this->‪setErrorClassAttribute();
137  $hiddenField = $this->‪renderHiddenFieldForEmptyValue();
138  return $hiddenField . $this->tag->render();
139  }
140 }
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\setErrorClassAttribute
‪setErrorClassAttribute()
Definition: AbstractFormFieldViewHelper.php:324
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getPropertyValue
‪mixed getPropertyValue()
Definition: AbstractFormFieldViewHelper.php:291
‪TYPO3\CMS\Fluid\ViewHelpers\Form
Definition: AbstractFormFieldViewHelper.php:2
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getLastSubmittedFormData
‪mixed getLastSubmittedFormData()
Definition: AbstractFormFieldViewHelper.php:225
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\registerFieldNameForFormTokenGeneration
‪registerFieldNameForFormTokenGeneration($fieldName)
Definition: AbstractFormViewHelper.php:101
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\renderHiddenFieldForEmptyValue
‪string renderHiddenFieldForEmptyValue()
Definition: AbstractFormFieldViewHelper.php:367
‪TYPO3\CMS\Fluid\ViewHelpers\Form\CheckboxViewHelper\initializeArguments
‪initializeArguments()
Definition: CheckboxViewHelper.php:69
‪TYPO3\CMS\Fluid\ViewHelpers\Form\CheckboxViewHelper\$tagName
‪string $tagName
Definition: CheckboxViewHelper.php:64
‪TYPO3\CMS\Fluid\ViewHelpers\Form\CheckboxViewHelper
Definition: CheckboxViewHelper.php:61
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
Definition: AbstractFormFieldViewHelper.php:26
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\hasMappingErrorOccurred
‪bool hasMappingErrorOccurred()
Definition: AbstractFormFieldViewHelper.php:214
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getName
‪string getName()
Definition: AbstractFormFieldViewHelper.php:86
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getValueAttribute
‪mixed getValueAttribute()
Definition: AbstractFormFieldViewHelper.php:147
‪TYPO3\CMS\Fluid\ViewHelpers\Form\CheckboxViewHelper\render
‪string render()
Definition: CheckboxViewHelper.php:96