[BUGFIX] backend formfields: Make boolean fields UI working in Edge/IE
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / TextElement.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\Authentication\BackendUserAuthentication;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Utility\MathUtility;
20 use TYPO3\CMS\Core\Utility\StringUtility;
21 use TYPO3\CMS\Lang\LanguageService;
22
23 /**
24 * Generation of TCEform elements of the type "text"
25 */
26 class TextElement extends AbstractFormElement
27 {
28 /**
29 * Default field wizards enabled for this element.
30 *
31 * @var array
32 */
33 protected $defaultFieldWizard = [
34 'localizationStateSelector' => [
35 'renderType' => 'localizationStateSelector',
36 ],
37 'otherLanguageContent' => [
38 'renderType' => 'otherLanguageContent',
39 'after' => [
40 'localizationStateSelector'
41 ],
42 ],
43 'defaultLanguageDifferences' => [
44 'renderType' => 'defaultLanguageDifferences',
45 'after' => [
46 'otherLanguageContent',
47 ],
48 ],
49 ];
50
51 /**
52 * The number of chars expected per row when the height of a text area field is
53 * automatically calculated based on the number of characters found in the field content.
54 *
55 * @var int
56 */
57 protected $charactersPerRow = 40;
58
59 /**
60 * This will render a <textarea>
61 *
62 * @return array As defined in initializeResultArray() of AbstractNode
63 */
64 public function render()
65 {
66 $languageService = $this->getLanguageService();
67 $backendUser = $this->getBackendUserAuthentication();
68
69 $table = $this->data['tableName'];
70 $fieldName = $this->data['fieldName'];
71 $row = $this->data['databaseRow'];
72 $parameterArray = $this->data['parameterArray'];
73 $resultArray = $this->initializeResultArray();
74
75 $itemValue = $parameterArray['itemFormElValue'];
76 $config = $parameterArray['fieldConf']['config'];
77 $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
78 $cols = MathUtility::forceIntegerInRange($config['cols'] ?: $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
79 $width = $this->formMaxWidth($cols);
80 $nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $row['uid'] . '][' . $fieldName . ']');
81
82 // Setting number of rows
83 $rows = MathUtility::forceIntegerInRange($config['rows'] ?: 5, 1, 20);
84 $originalRows = $rows;
85 $itemFormElementValueLength = strlen($itemValue);
86 if ($itemFormElementValueLength > $this->charactersPerRow * 2) {
87 $rows = MathUtility::forceIntegerInRange(
88 round($itemFormElementValueLength / $this->charactersPerRow),
89 count(explode(LF, $itemValue)),
90 20
91 );
92 if ($rows < $originalRows) {
93 $rows = $originalRows;
94 }
95 }
96
97 if ($config['readOnly']) {
98 $html = [];
99 $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
100 $html[] = '<div class="form-wizards-wrap">';
101 $html[] = '<div class="form-wizards-element">';
102 $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
103 $html[] = '<textarea class="form-control" rows="' . $rows . '" disabled>';
104 $html[] = htmlspecialchars($itemValue);
105 $html[] = '</textarea>';
106 $html[] = '</div>';
107 $html[] = '</div>';
108 $html[] = '</div>';
109 $html[] = '</div>';
110 $resultArray['html'] = implode(LF, $html);
111 return $resultArray;
112 }
113
114 // @todo: The whole eval handling is a mess and needs refactoring
115 foreach ($evalList as $func) {
116 // @todo: This is ugly: The code should find out on it's own whether a eval definition is a
117 // @todo: keyword like "date", or a class reference. The global registration could be dropped then
118 // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
119 // There is a similar hook for "evaluateFieldValue" in DataHandler and InputTextElement
120 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
121 if (class_exists($func)) {
122 $evalObj = GeneralUtility::makeInstance($func);
123 if (method_exists($evalObj, 'deevaluateFieldValue')) {
124 $_params = [
125 'value' => $itemValue
126 ];
127 $itemValue = $evalObj->deevaluateFieldValue($_params);
128 }
129 }
130 }
131 }
132
133 $attributes = [
134 'id' => StringUtility::getUniqueId('formengine-textarea-'),
135 'name' => htmlspecialchars($parameterArray['itemFormElName']),
136 'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
137 'data-formengine-input-name' => htmlspecialchars($parameterArray['itemFormElName']),
138 'rows' => $rows,
139 'wrap' => $config['wrap'] ?: 'virtual',
140 'onChange' => implode('', $parameterArray['fieldChangeFunc']),
141 ];
142 $classes = [
143 'form-control',
144 't3js-formengine-textarea',
145 'formengine-textarea',
146 ];
147 if ($config['fixedFont']) {
148 $classes[] = 'text-monospace';
149 }
150 if ($config['enableTabulator']) {
151 $classes[] = 't3js-enable-tab';
152 }
153 $attributes['class'] = implode(' ', $classes);
154 $maximumHeight = (int)$backendUser->uc['resizeTextareas_MaxHeight'];
155 if ($maximumHeight > 0) {
156 // add the max-height from the users' preference to it
157 $attributes['style'] = 'max-height: ' . $maximumHeight . 'px';
158 }
159 if (isset($config['max']) && (int)$config['max'] > 0) {
160 $attributes['maxlength'] = (int)$config['max'];
161 }
162 if (!empty($config['placeholder'])) {
163 $attributes['placeholder'] = htmlspecialchars(trim($config['placeholder']));
164 }
165
166 $valuePickerHtml = [];
167 if (isset($config['valuePicker']['items']) && is_array($config['valuePicker']['items'])) {
168 $mode = $config['valuePicker']['mode'] ?? '';
169 $itemName = $parameterArray['itemFormElName'];
170 $fieldChangeFunc = $parameterArray['fieldChangeFunc'];
171 if ($mode === 'append') {
172 $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
173 . '.value=\'\'+this.options[this.selectedIndex].value+document.editform[' . GeneralUtility::quoteJSvalue($itemName) . '].value';
174 } elseif ($mode === 'prepend') {
175 $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
176 . '.value+=\'\'+this.options[this.selectedIndex].value';
177 } else {
178 $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
179 . '.value=this.options[this.selectedIndex].value';
180 }
181 $valuePickerHtml[] = '<select';
182 $valuePickerHtml[] = ' class="form-control tceforms-select tceforms-wizardselect"';
183 $valuePickerHtml[] = ' onchange="' . htmlspecialchars($assignValue . ';this.blur();this.selectedIndex=0;' . implode('', $fieldChangeFunc)) . '"';
184 $valuePickerHtml[] = '>';
185 $valuePickerHtml[] = '<option></option>';
186 foreach ($config['valuePicker']['items'] as $item) {
187 $valuePickerHtml[] = '<option value="' . htmlspecialchars($item[1]) . '">' . htmlspecialchars($languageService->sL($item[0])) . '</option>';
188 }
189 $valuePickerHtml[] = '</select>';
190 }
191
192 $legacyWizards = $this->renderWizards();
193 $legacyFieldControlHtml = implode(LF, $legacyWizards['fieldControl']);
194 $legacyFieldWizardHtml = implode(LF, $legacyWizards['fieldWizard']);
195
196 $fieldInformationResult = $this->renderFieldInformation();
197 $fieldInformationHtml = $fieldInformationResult['html'];
198 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
199
200 $fieldControlResult = $this->renderFieldControl();
201 $fieldControlHtml = $legacyFieldControlHtml . $fieldControlResult['html'];
202 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
203
204 $fieldWizardResult = $this->renderFieldWizard();
205 $fieldWizardHtml = $legacyFieldWizardHtml . $fieldWizardResult['html'];
206 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
207
208 $mainFieldHtml = [];
209 $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
210 $mainFieldHtml[] = '<div class="form-wizards-wrap">';
211 $mainFieldHtml[] = '<div class="form-wizards-element">';
212 $mainFieldHtml[] = '<textarea ' . GeneralUtility::implodeAttributes($attributes, true) . '>' . htmlspecialchars($itemValue) . '</textarea>';
213 $mainFieldHtml[] = '</div>';
214 $mainFieldHtml[] = '<div class="form-wizards-items-aside">';
215 $mainFieldHtml[] = '<div class="btn-group">';
216 $mainFieldHtml[] = implode(LF, $valuePickerHtml);
217 $mainFieldHtml[] = $fieldControlHtml;
218 $mainFieldHtml[] = '</div>';
219 $mainFieldHtml[] = '</div>';
220 $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
221 $mainFieldHtml[] = $fieldWizardHtml;
222 $mainFieldHtml[] = '</div>';
223 $mainFieldHtml[] = '</div>';
224 $mainFieldHtml[] = '</div>';
225 $mainFieldHtml = implode(LF, $mainFieldHtml);
226
227 $fullElement = $mainFieldHtml;
228 if ($this->hasNullCheckboxButNoPlaceholder()) {
229 $checked = $itemValue !== null ? ' checked="checked"' : '';
230 $fullElement = [];
231 $fullElement[] = '<div class="t3-form-field-disable"></div>';
232 $fullElement[] = '<div class="checkbox t3-form-field-eval-null-checkbox">';
233 $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
234 $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
235 $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
236 $fullElement[] = $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
237 $fullElement[] = '</label>';
238 $fullElement[] = '</div>';
239 $fullElement[] = $mainFieldHtml;
240 $fullElement = implode(LF, $fullElement);
241 } elseif ($this->hasNullCheckboxWithPlaceholder()) {
242 $checked = $itemValue !== null ? ' checked="checked"' : '';
243 $placeholder = $shortenedPlaceholder = $config['placeholder'] ?? '';
244 $disabled = '';
245 $fallbackValue = 0;
246 if (strlen($placeholder) > 0) {
247 $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
248 if ($placeholder !== $shortenedPlaceholder) {
249 $overrideLabel = sprintf(
250 $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
251 '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
252 );
253 } else {
254 $overrideLabel = sprintf(
255 $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
256 htmlspecialchars($placeholder)
257 );
258 }
259 } else {
260 $fallbackValue = 1;
261 $checked = ' checked="checked"';
262 $disabled = ' disabled="disabled"';
263 $overrideLabel = $languageService->sL(
264 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
265 );
266 }
267 $fullElement = [];
268 $fullElement[] = '<div class="checkbox t3js-form-field-eval-null-placeholder-checkbox">';
269 $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
270 $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="' . $fallbackValue . '" />';
271 $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . $disabled . ' />';
272 $fullElement[] = $overrideLabel;
273 $fullElement[] = '</label>';
274 $fullElement[] = '</div>';
275 $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
276 $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
277 $fullElement[] = '<textarea';
278 $fullElement[] = ' class="form-control formengine-textarea' . (isset($config['fixedFont']) ? ' text-monospace' : '') . '"';
279 $fullElement[] = ' disabled="disabled"';
280 $fullElement[] = ' rows="' . htmlspecialchars($attributes['rows']) . '"';
281 $fullElement[] = ' wrap="' . htmlspecialchars($attributes['wrap']) . '"';
282 $fullElement[] = isset($attributes['style']) ? ' style="' . htmlspecialchars($attributes['style']) . '"' : '';
283 $fullElement[] = isset($attributes['maxlength']) ? ' maxlength="' . htmlspecialchars($attributes['maxlength']) . '"' : '';
284 $fullElement[] = '>';
285 $fullElement[] = htmlspecialchars($itemValue);
286 $fullElement[] = '</textarea>';
287 $fullElement[] = '</div>';
288 $fullElement[] = '</div>';
289 $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
290 $fullElement[] = $mainFieldHtml;
291 $fullElement[] = '</div>';
292 $fullElement = implode(LF, $fullElement);
293 }
294
295 $resultArray['html'] = '<div class="formengine-field-item t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
296 return $resultArray;
297 }
298
299 /**
300 * @return BackendUserAuthentication
301 */
302 protected function getBackendUserAuthentication()
303 {
304 return $GLOBALS['BE_USER'];
305 }
306
307 /**
308 * @return LanguageService
309 */
310 protected function getLanguageService()
311 {
312 return $GLOBALS['LANG'];
313 }
314 }