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