-
Notifications
You must be signed in to change notification settings - Fork 646
/
CheckboxViewHelper.php
134 lines (123 loc) · 4.02 KB
/
CheckboxViewHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Fluid\ViewHelpers\Form;
/**
* ViewHelper which creates a simple checkbox :html:`<input type="checkbox">`.
*
* Examples
* ========
*
* Simple one
* ----------
*
* ::
*
* <f:form.checkbox name="myCheckBox" value="someValue" />
*
* Output::
*
* <input type="checkbox" name="myCheckBox" value="someValue" />
*
* Preselect
* ---------
*
* ::
*
* <f:form.checkbox name="myCheckBox" value="someValue" checked="{object.value} == 5" />
*
* Output::
*
* <input type="checkbox" name="myCheckBox" value="someValue" checked="checked" />
*
* Depending on bound ``object`` to surrounding :ref:`f:form <typo3-fluid-form>`.
*
* Bind to object property
* -----------------------
*
* ::
*
* <f:form.checkbox property="interests" value="TYPO3" multiple="1" />
*
* Output::
*
* <input type="checkbox" name="user[interests][]" value="TYPO3" checked="checked" />
*
* Depending on property ``interests``.
*/
final class CheckboxViewHelper extends AbstractFormFieldViewHelper
{
/**
* @var string
*/
protected $tagName = 'input';
public function initializeArguments(): void
{
parent::initializeArguments();
$this->registerTagAttribute(
'disabled',
'string',
'Specifies that the input element should be disabled when the page loads'
);
$this->registerArgument(
'errorClass',
'string',
'CSS class to set if there are errors for this ViewHelper',
false,
'f3-form-error'
);
$this->overrideArgument('value', 'string', 'Value of input tag. Required for checkboxes', true);
$this->registerUniversalTagAttributes();
$this->registerArgument('checked', 'bool', 'Specifies that the input element should be preselected');
$this->registerArgument('multiple', 'bool', 'Specifies whether this checkbox belongs to a multivalue (is part of a checkbox group)', false, false);
}
public function render(): string
{
$checked = $this->arguments['checked'];
$multiple = $this->arguments['multiple'];
$this->tag->addAttribute('type', 'checkbox');
$nameAttribute = $this->getName();
$valueAttribute = $this->getValueAttribute();
$propertyValue = null;
if ($this->hasMappingErrorOccurred()) {
$propertyValue = $this->getLastSubmittedFormData();
}
if ($checked === null && $propertyValue === null) {
$propertyValue = $this->getPropertyValue();
}
if ($propertyValue instanceof \Traversable) {
$propertyValue = iterator_to_array($propertyValue);
}
if (is_array($propertyValue)) {
$propertyValue = array_map($this->convertToPlainValue(...), $propertyValue);
if ($checked === null) {
$checked = in_array($valueAttribute, $propertyValue);
}
$nameAttribute .= '[]';
} elseif ($multiple === true) {
$nameAttribute .= '[]';
} elseif ($propertyValue !== null) {
$checked = (bool)$propertyValue === (bool)$valueAttribute;
}
$this->registerFieldNameForFormTokenGeneration($nameAttribute);
$this->tag->addAttribute('name', $nameAttribute);
$this->tag->addAttribute('value', $valueAttribute);
if ($checked === true) {
$this->tag->addAttribute('checked', 'checked');
}
$this->setErrorClassAttribute();
$hiddenField = $this->renderHiddenFieldForEmptyValue();
return $hiddenField . $this->tag->render();
}
}