-
Notifications
You must be signed in to change notification settings - Fork 638
/
CheckboxLabeledToggleElement.php
210 lines (194 loc) · 8.3 KB
/
CheckboxLabeledToggleElement.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?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\Backend\Form\Element;
use TYPO3\CMS\Backend\Form\NodeFactory;
use TYPO3\CMS\Core\Imaging\IconRegistry;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
/**
* Generation of TCEform elements of the type "check"
*/
class CheckboxLabeledToggleElement extends AbstractFormElement
{
/**
* @var IconRegistry
*/
private $iconRegistry;
/**
* Default field information enabled for this element.
*
* @var array
*/
protected $defaultFieldInformation = [
'tcaDescription' => [
'renderType' => 'tcaDescription',
],
];
/**
* Default field wizards enabled for this element.
*
* @var array
*/
protected $defaultFieldWizard = [
'localizationStateSelector' => [
'renderType' => 'localizationStateSelector',
],
'otherLanguageContent' => [
'renderType' => 'otherLanguageContent',
'after' => [
'localizationStateSelector'
],
],
'defaultLanguageDifferences' => [
'renderType' => 'defaultLanguageDifferences',
'after' => [
'otherLanguageContent',
],
],
];
/**
* @param NodeFactory $nodeFactory
* @param array $data
*/
public function __construct(NodeFactory $nodeFactory, array $data)
{
parent::__construct($nodeFactory, $data);
$this->iconRegistry = GeneralUtility::makeInstance(IconRegistry::class);
}
/**
* This will render a checkbox or an array of checkboxes
*
* @return array As defined in initializeResultArray() of AbstractNode
*/
public function render(): array
{
$resultArray = $this->initializeResultArray();
$elementHtml = '';
$disabled = false;
if ($this->data['parameterArray']['fieldConf']['config']['readOnly']) {
$disabled = true;
}
// Traversing the array of items
$items = $this->data['parameterArray']['fieldConf']['config']['items'];
$numberOfItems = \count($items);
if ($numberOfItems === 0) {
$items[] = ['', ''];
$numberOfItems = 1;
}
$formElementValue = (int)$this->data['parameterArray']['itemFormElValue'];
$cols = (int)$this->data['parameterArray']['fieldConf']['config']['cols'];
if ($cols > 1) {
[$colClass, $colClear] = $this->calculateColumnMarkup($cols);
$elementHtml .= '<div class="checkbox-row row">';
$counter = 0;
// $itemKey is important here, because items could have been removed via TSConfig
foreach ($items as $itemKey => $itemDefinition) {
$label = $itemDefinition[0];
$elementHtml .=
'<div class="checkbox-column ' . $colClass . '">'
. $this->renderSingleCheckboxElement($label, $itemKey, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled) .
'</div>';
++$counter;
if ($counter < $numberOfItems && !empty($colClear)) {
foreach ($colClear as $rowBreakAfter => $clearClass) {
if ($counter % $rowBreakAfter === 0) {
$elementHtml .= '<div class="clearfix ' . $clearClass . '"></div>';
}
}
}
}
$elementHtml .= '</div>';
} else {
$counter = 0;
foreach ($items as $itemKey => $itemDefinition) {
$label = $itemDefinition[0];
$elementHtml .= $this->renderSingleCheckboxElement($label, $counter, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled);
++$counter;
}
}
if (!$disabled) {
$elementHtml .= '<input type="hidden" name="' . htmlspecialchars($this->data['parameterArray']['itemFormElName']) . '" value="' . htmlspecialchars((string)$formElementValue) . '" />';
}
$fieldInformationResult = $this->renderFieldInformation();
$fieldInformationHtml = $fieldInformationResult['html'];
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
$fieldWizardResult = $this->renderFieldWizard();
$fieldWizardHtml = $fieldWizardResult['html'];
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
$html = [];
$html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
$html[] = $fieldInformationHtml;
$html[] = '<div class="form-wizards-wrap">';
$html[] = '<div class="form-wizards-element">';
$html[] = $elementHtml;
$html[] = '</div>';
if (!$disabled && !empty($fieldWizardHtml)) {
$html[] = '<div class="form-wizards-items-bottom">';
$html[] = $fieldWizardHtml;
$html[] = '</div>';
}
$html[] = '</div>';
$html[] = '</div>';
$resultArray['html'] = implode(LF, $html);
return $resultArray;
}
/**
* This functions builds the HTML output for the checkbox
*
* @param string $label Label of this item
* @param int $itemCounter Number of this element in the list of all elements
* @param int $formElementValue Value of this element
* @param int $numberOfItems Full number of items
* @param array $additionalInformation Information with additional configuration options.
* @param bool $disabled TRUE if form element is disabled
* @return string Single element HTML
*/
protected function renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled): string
{
$config = $additionalInformation['fieldConf']['config'];
$inline = !empty($config['cols']) && $config['cols'] === 'inline';
$invert = isset($config['items'][$itemCounter]['invertStateDisplay']) && $config['items'][$itemCounter]['invertStateDisplay'] === true;
$checkboxParameters = $this->checkBoxParams(
$additionalInformation['itemFormElName'],
$formElementValue,
$itemCounter,
$numberOfItems,
implode('', $additionalInformation['fieldChangeFunc'])
);
$uniqueId = StringUtility::getUniqueId('_');
$checkboxId = $additionalInformation['itemFormElID'] . '_' . $itemCounter . $uniqueId;
return '
<div class="checkbox checkbox-type-labeled-toggle' . ($invert ? ' checkbox-invert' : '') . ($inline ? ' checkbox-inline' : '') . (!$disabled ? '' : ' disabled') . '">
<input type="checkbox"
class="checkbox-input"
value="1"
data-formengine-input-name="' . htmlspecialchars($additionalInformation['itemFormElName']) . '"
' . $checkboxParameters . '
' . (!$disabled ? '' : ' disabled="disabled"') . '
id="' . $checkboxId . '" />
<label class="checkbox-label" for="' . $checkboxId . '">
<span class="checkbox-label-switch">
<span class="checkbox-label-switch-checked">
' . $config['items'][$itemCounter]['labelChecked'] . '
</span>
<span class="checkbox-label-switch-unchecked">
' . $config['items'][$itemCounter]['labelUnchecked'] . '
</span>
</span>
<span class="checkbox-label-text">' . $this->appendValueToLabelInDebugMode(($label ? htmlspecialchars($label) : ' '), $formElementValue) . '</span>
</label>
</div>';
}
}