[TASK] Streamline phpdoc annotations in EXT:fluid
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Form / CheckboxViewHelper.php
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
17 /**
18 * View Helper which creates a simple checkbox (<input type="checkbox">).
19 *
20 * = Examples =
21 *
22 * <code title="Example">
23 * <f:form.checkbox name="myCheckBox" value="someValue" />
24 * </code>
25 * <output>
26 * <input type="checkbox" name="myCheckBox" value="someValue" />
27 * </output>
28 *
29 * <code title="Preselect">
30 * <f:form.checkbox name="myCheckBox" value="someValue" checked="{object.value} == 5" />
31 * </code>
32 * <output>
33 * <input type="checkbox" name="myCheckBox" value="someValue" checked="checked" />
34 * (depending on $object)
35 * </output>
36 *
37 * <code title="Bind to object property">
38 * <f:form.checkbox property="interests" value="TYPO3" />
39 * </code>
40 * <output>
41 * <input type="checkbox" name="user[interests][]" value="TYPO3" checked="checked" />
42 * (depending on property "interests")
43 * </output>
44 */
45 class CheckboxViewHelper extends AbstractFormFieldViewHelper
46 {
47 /**
48 * @var string
49 */
50 protected $tagName = 'input';
51
52 /**
53 * Initialize the arguments.
54 */
55 public function initializeArguments()
56 {
57 parent::initializeArguments();
58 $this->registerTagAttribute(
59 'disabled',
60 'string',
61 'Specifies that the input element should be disabled when the page loads'
62 );
63 $this->registerArgument(
64 'errorClass',
65 'string',
66 'CSS class to set if there are errors for this view helper',
67 false,
68 'f3-form-error'
69 );
70 $this->overrideArgument('value', 'string', 'Value of input tag. Required for checkboxes', true);
71 $this->registerUniversalTagAttributes();
72 $this->registerArgument('checked', 'bool', 'Specifies that the input element should be preselected');
73 $this->registerArgument('multiple', 'bool', 'Specifies whether this checkbox belongs to a multivalue (is part of a checkbox group)', false, false);
74 }
75
76 /**
77 * Renders the checkbox.
78 *
79 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
80 * @return string
81 */
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 }