ca3949862c704283ed3f4483207ade5d13b81fcf
[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 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 $fieldInformationResult = $this->renderFieldInformation();
193 $fieldInformationHtml = $fieldInformationResult['html'];
194 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
195
196 $fieldControlResult = $this->renderFieldControl();
197 $fieldControlHtml = $fieldControlResult['html'];
198 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
199
200 $fieldWizardResult = $this->renderFieldWizard();
201 $fieldWizardHtml = $fieldWizardResult['html'];
202 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
203
204 $mainFieldHtml = [];
205 $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
206 $mainFieldHtml[] = '<div class="form-wizards-wrap">';
207 $mainFieldHtml[] = '<div class="form-wizards-element">';
208 $mainFieldHtml[] = '<textarea ' . GeneralUtility::implodeAttributes($attributes, true) . '>' . htmlspecialchars($itemValue) . '</textarea>';
209 $mainFieldHtml[] = '</div>';
210 $mainFieldHtml[] = '<div class="form-wizards-items-aside">';
211 $mainFieldHtml[] = '<div class="btn-group">';
212 $mainFieldHtml[] = implode(LF, $valuePickerHtml);
213 $mainFieldHtml[] = $fieldControlHtml;
214 $mainFieldHtml[] = '</div>';
215 $mainFieldHtml[] = '</div>';
216 $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
217 $mainFieldHtml[] = $fieldWizardHtml;
218 $mainFieldHtml[] = '</div>';
219 $mainFieldHtml[] = '</div>';
220 $mainFieldHtml[] = '</div>';
221 $mainFieldHtml = implode(LF, $mainFieldHtml);
222
223 $fullElement = $mainFieldHtml;
224 if ($this->hasNullCheckboxButNoPlaceholder()) {
225 $checked = $itemValue !== null ? ' checked="checked"' : '';
226 $fullElement = [];
227 $fullElement[] = '<div class="t3-form-field-disable"></div>';
228 $fullElement[] = '<div class="checkbox t3-form-field-eval-null-checkbox">';
229 $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
230 $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
231 $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
232 $fullElement[] = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
233 $fullElement[] = '</label>';
234 $fullElement[] = '</div>';
235 $fullElement[] = $mainFieldHtml;
236 $fullElement = implode(LF, $fullElement);
237 } elseif ($this->hasNullCheckboxWithPlaceholder()) {
238 $checked = $itemValue !== null ? ' checked="checked"' : '';
239 $placeholder = $shortenedPlaceholder = $config['placeholder'] ?? '';
240 $disabled = '';
241 $fallbackValue = 0;
242 if (strlen($placeholder) > 0) {
243 $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
244 if ($placeholder !== $shortenedPlaceholder) {
245 $overrideLabel = sprintf(
246 $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
247 '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
248 );
249 } else {
250 $overrideLabel = sprintf(
251 $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
252 htmlspecialchars($placeholder)
253 );
254 }
255 } else {
256 $fallbackValue = 1;
257 $checked = ' checked="checked"';
258 $disabled = ' disabled="disabled"';
259 $overrideLabel = $languageService->sL(
260 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
261 );
262 }
263 $fullElement = [];
264 $fullElement[] = '<div class="checkbox t3js-form-field-eval-null-placeholder-checkbox">';
265 $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
266 $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="' . $fallbackValue . '" />';
267 $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . $disabled . ' />';
268 $fullElement[] = $overrideLabel;
269 $fullElement[] = '</label>';
270 $fullElement[] = '</div>';
271 $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
272 $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
273 $fullElement[] = '<textarea';
274 $fullElement[] = ' class="form-control formengine-textarea' . (isset($config['fixedFont']) ? ' text-monospace' : '') . '"';
275 $fullElement[] = ' disabled="disabled"';
276 $fullElement[] = ' rows="' . htmlspecialchars($attributes['rows']) . '"';
277 $fullElement[] = ' wrap="' . htmlspecialchars($attributes['wrap']) . '"';
278 $fullElement[] = isset($attributes['style']) ? ' style="' . htmlspecialchars($attributes['style']) . '"' : '';
279 $fullElement[] = isset($attributes['maxlength']) ? ' maxlength="' . htmlspecialchars($attributes['maxlength']) . '"' : '';
280 $fullElement[] = '>';
281 $fullElement[] = htmlspecialchars($shortenedPlaceholder);
282 $fullElement[] = '</textarea>';
283 $fullElement[] = '</div>';
284 $fullElement[] = '</div>';
285 $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
286 $fullElement[] = $mainFieldHtml;
287 $fullElement[] = '</div>';
288 $fullElement = implode(LF, $fullElement);
289 }
290
291 $resultArray['html'] = '<div class="formengine-field-item t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
292 return $resultArray;
293 }
294
295 /**
296 * @return BackendUserAuthentication
297 */
298 protected function getBackendUserAuthentication()
299 {
300 return $GLOBALS['BE_USER'];
301 }
302
303 /**
304 * @return LanguageService
305 */
306 protected function getLanguageService()
307 {
308 return $GLOBALS['LANG'];
309 }
310 }