[TASK] Deprecate itemListStyle and selectedListStyle
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / SelectSingleBoxElement.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\Element;
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 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Core\Utility\MathUtility;
19 use TYPO3\CMS\Core\Utility\StringUtility;
20
21 /**
22 * Create a widget with a select box where multiple items can be selected
23 *
24 * This is rendered for config type=select, renderType=selectSingleBox
25 */
26 class SelectSingleBoxElement extends AbstractFormElement
27 {
28 /**
29 * Default field controls for this element.
30 *
31 * @var array
32 */
33 protected $defaultFieldControl = [
34 'resetSelection' => [
35 'renderType' => 'resetSelection',
36 ],
37 ];
38
39 /**
40 * Default field wizards enabled for this element.
41 *
42 * @var array
43 */
44 protected $defaultFieldWizard = [
45 'localizationStateSelector' => [
46 'renderType' => 'localizationStateSelector',
47 ],
48 'otherLanguageContent' => [
49 'renderType' => 'otherLanguageContent',
50 'after' => [
51 'localizationStateSelector'
52 ],
53 ],
54 'defaultLanguageDifferences' => [
55 'renderType' => 'defaultLanguageDifferences',
56 'after' => [
57 'otherLanguageContent',
58 ],
59 ],
60 ];
61
62 /**
63 * This will render a selector box element, or possibly a special construction with two selector boxes.
64 *
65 * @return array As defined in initializeResultArray() of AbstractNode
66 */
67 public function render()
68 {
69 $languageService = $this->getLanguageService();
70 $resultArray = $this->initializeResultArray();
71
72 $parameterArray = $this->data['parameterArray'];
73 // Field configuration from TCA:
74 $config = $parameterArray['fieldConf']['config'];
75 $selectItems = $parameterArray['fieldConf']['config']['items'];
76 $disabled = !empty($config['readOnly']);
77
78 // Get values in an array (and make unique, which is fine because there can be no duplicates anyway):
79 $itemArray = array_flip($parameterArray['itemFormElValue']);
80 $width = $this->formMaxWidth($this->defaultInputWidth);
81
82 $optionElements = [];
83 foreach ($selectItems as $i => $item) {
84 $value = $item[1];
85 $attributes = [];
86 // Selected or not by default
87 if (isset($itemArray[$value])) {
88 $attributes['selected'] = 'selected';
89 unset($itemArray[$value]);
90 }
91 // Non-selectable element
92 if ((string)$value === '--div--') {
93 $attributes['disabled'] = 'disabled';
94 $attributes['class'] = 'formcontrol-select-divider';
95 }
96 $optionElements[] = $this->renderOptionElement($value, $item[0], $attributes);
97 }
98
99 $selectElement = $this->renderSelectElement($optionElements, $parameterArray, $config);
100
101 $legacyWizards = $this->renderWizards();
102 $legacyFieldControlHtml = implode(LF, $legacyWizards['fieldControl']);
103 $legacyFieldWizardHtml = implode(LF, $legacyWizards['fieldWizard']);
104
105 $fieldInformationResult = $this->renderFieldInformation();
106 $fieldInformationHtml = $fieldInformationResult['html'];
107 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
108
109 $fieldControlResult = $this->renderFieldControl();
110 $fieldControlHtml = $legacyFieldControlHtml . $fieldControlResult['html'];
111 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
112
113 $fieldWizardResult = $this->renderFieldWizard();
114 $fieldWizardHtml = $legacyFieldWizardHtml . $fieldWizardResult['html'];
115 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
116
117 $html = [];
118 $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
119 if (!$disabled) {
120 $html[] = $fieldInformationHtml;
121 }
122 $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
123 $html[] = '<div class="form-wizards-wrap form-wizards-aside">';
124 $html[] = '<div class="form-wizards-element">';
125 if (!$disabled) {
126 // Add an empty hidden field which will send a blank value if all items are unselected.
127 $html[] = '<input type="hidden" name="' . htmlspecialchars($parameterArray['itemFormElName']) . '" value="">';
128 }
129 $html[] = $selectElement;
130 $html[] = '</div>';
131 if (!$disabled) {
132 $html[] = '<div class="form-wizards-items-aside">';
133 $html[] = $fieldControlHtml;
134 $html[] = '</div>';
135 $html[] = '</div>';
136
137 $html[] = '<p>';
138 $html[] = '<em>' . htmlspecialchars($languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.holdDownCTRL')) . '</em>';
139 $html[] = '</p>';
140 $html[] = '<div class="form-wizards-items-bottom">';
141 $html[] = $fieldWizardHtml;
142 $html[] = '</div>';
143 }
144 $html[] = '</div>';
145 $html[] = '</div>';
146
147 $resultArray['html'] = implode(LF, $html);
148 return $resultArray;
149 }
150
151 /**
152 * Renders a <select> element
153 *
154 * @param array $optionElements List of rendered <option> elements
155 * @param array $parameterArray
156 * @param array $config Field configuration
157 * @return string
158 */
159 protected function renderSelectElement(array $optionElements, array $parameterArray, array $config)
160 {
161 $selectItems = $parameterArray['fieldConf']['config']['items'];
162 $size = (int)$config['size'];
163 $cssPrefix = $size === 1 ? 'tceforms-select' : 'tceforms-multiselect';
164
165 if ($config['autoSizeMax']) {
166 $size = MathUtility::forceIntegerInRange(
167 count($selectItems) + 1,
168 MathUtility::forceIntegerInRange($size, 1),
169 $config['autoSizeMax']
170 );
171 }
172
173 $attributes = [
174 'name' => $parameterArray['itemFormElName'] . '[]',
175 'multiple' => 'multiple',
176 'onchange' => implode('', $parameterArray['fieldChangeFunc']),
177 'id' => StringUtility::getUniqueId($cssPrefix),
178 'class' => 'form-control ' . $cssPrefix,
179 'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
180 ];
181 if ($size) {
182 $attributes['size'] = $size;
183 }
184 if ($config['readOnly']) {
185 $attributes['disabled'] = 'disabled';
186 }
187 if (isset($config['itemListStyle'])) {
188 GeneralUtility::deprecationLog('TCA property itemListStyle is deprecated since TYPO3 v8 and will be removed in v9');
189 $attributes['style'] = $config['itemListStyle'];
190 }
191
192 $html = [];
193 $html[] = '<select ' . GeneralUtility::implodeAttributes($attributes, true) . '>';
194 $html[] = implode(LF, $optionElements);
195 $html[] = '</select>';
196
197 return implode(LF, $html);
198 }
199
200 /**
201 * Renders a single <option> element
202 *
203 * @param string $value The option value
204 * @param string $label The option label
205 * @param array $attributes Map of attribute names and values
206 * @return string
207 */
208 protected function renderOptionElement($value, $label, array $attributes = [])
209 {
210 $attributes['value'] = $value;
211 $html = [
212 '<option ' . GeneralUtility::implodeAttributes($attributes, true) . '>',
213 htmlspecialchars($label, ENT_COMPAT, 'UTF-8', false),
214 '</option>'
215
216 ];
217
218 return implode('', $html);
219 }
220 }