‪TYPO3CMS  9.5
InputTextElement.php
Go to the documentation of this file.
1 <?php
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 
21 
29 {
35  protected ‪$defaultFieldInformation = [
36  'tcaDescription' => [
37  'renderType' => 'tcaDescription',
38  ],
39  ];
40 
46  protected ‪$defaultFieldWizard = [
47  'localizationStateSelector' => [
48  'renderType' => 'localizationStateSelector',
49  ],
50  'otherLanguageContent' => [
51  'renderType' => 'otherLanguageContent',
52  'after' => [
53  'localizationStateSelector'
54  ],
55  ],
56  'defaultLanguageDifferences' => [
57  'renderType' => 'defaultLanguageDifferences',
58  'after' => [
59  'otherLanguageContent',
60  ],
61  ],
62  ];
63 
69  public function ‪render()
70  {
71  $languageService = $this->‪getLanguageService();
72 
73  $table = $this->data['tableName'];
74  $fieldName = $this->data['fieldName'];
75  $row = $this->data['databaseRow'];
76  $parameterArray = $this->data['parameterArray'];
77  $resultArray = $this->‪initializeResultArray();
78 
79  $itemValue = $parameterArray['itemFormElValue'];
80  $config = $parameterArray['fieldConf']['config'];
81  $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
82  $size = ‪MathUtility::forceIntegerInRange($config['size'] ?? $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
83  $width = (int)$this->‪formMaxWidth($size);
84  $nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $row['uid'] . '][' . $fieldName . ']');
85 
86  $fieldInformationResult = $this->‪renderFieldInformation();
87  $fieldInformationHtml = $fieldInformationResult['html'];
88  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
89 
90  if ($config['readOnly']) {
91  // Early return for read only fields
92  if (in_array('password', $evalList, true)) {
93  $itemValue = $itemValue ? '*********' : '';
94  }
95 
96  $disabledFieldAttributes = [
97  'class' => 'form-control',
98  'data-formengine-input-name' => $parameterArray['itemFormElName'],
99  'type' => 'text',
100  'value' => $itemValue,
101  ];
102 
103  $html = [];
104  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
105  $html[] = $fieldInformationHtml;
106  $html[] = '<div class="form-wizards-wrap">';
107  $html[] = '<div class="form-wizards-element">';
108  $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
109  $html[] = '<input ' . GeneralUtility::implodeAttributes($disabledFieldAttributes, true) . ' disabled>';
110  $html[] = '</div>';
111  $html[] = '</div>';
112  $html[] = '</div>';
113  $html[] = '</div>';
114  $resultArray['html'] = implode(LF, $html);
115  return $resultArray;
116  }
117 
118  // @todo: The whole eval handling is a mess and needs refactoring
119  foreach ($evalList as $func) {
120  // @todo: This is ugly: The code should find out on it's own whether a eval definition is a
121  // @todo: keyword like "date", or a class reference. The global registration could be dropped then
122  // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
123  if (isset(‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
124  if (class_exists($func)) {
125  $evalObj = GeneralUtility::makeInstance($func);
126  if (method_exists($evalObj, 'deevaluateFieldValue')) {
127  $_params = [
128  'value' => $itemValue
129  ];
130  $itemValue = $evalObj->deevaluateFieldValue($_params);
131  }
132  if (method_exists($evalObj, 'returnFieldJS')) {
133  $resultArray['additionalJavaScriptPost'][] = 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($func) . ']'
134  . ' = function(value) {' . $evalObj->returnFieldJS() . '};';
135  }
136  }
137  }
138  }
139 
140  $fieldId = ‪StringUtility::getUniqueId('formengine-input-');
141 
142  $attributes = [
143  'value' => '',
144  'id' => $fieldId,
145  'class' => implode(' ', [
146  'form-control',
147  't3js-clearable',
148  'hasDefaultValue',
149  ]),
150  'data-formengine-validation-rules' => $this->‪getValidationDataAsJsonString($config),
151  'data-formengine-input-params' => json_encode([
152  'field' => $parameterArray['itemFormElName'],
153  'evalList' => implode(',', $evalList),
154  'is_in' => trim($config['is_in'])
155  ]),
156  'data-formengine-input-name' => $parameterArray['itemFormElName'],
157  ];
158 
159  $maxLength = $config['max'] ?? 0;
160  if ((int)$maxLength > 0) {
161  $attributes['maxlength'] = (int)$maxLength;
162  }
163  if (!empty($config['placeholder'])) {
164  $attributes['placeholder'] = trim($config['placeholder']);
165  }
166  if (isset($config['autocomplete'])) {
167  $attributes['autocomplete'] = empty($config['autocomplete']) ? 'new-' . $fieldName : 'on';
168  }
169 
170  $valuePickerHtml = [];
171  if (isset($config['valuePicker']['items']) && is_array($config['valuePicker']['items'])) {
172  $mode = $config['valuePicker']['mode'] ?? '';
173  $itemName = $parameterArray['itemFormElName'];
174  $fieldChangeFunc = $parameterArray['fieldChangeFunc'];
175  if ($mode === 'append') {
176  $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
177  . '.value+=\'\'+this.options[this.selectedIndex].value';
178  } elseif ($mode === 'prepend') {
179  $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
180  . '.value=\'\'+this.options[this.selectedIndex].value+document.editform[' . GeneralUtility::quoteJSvalue($itemName) . '].value';
181  } else {
182  $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
183  . '.value=this.options[this.selectedIndex].value';
184  }
185  $valuePickerHtml[] = '<select';
186  $valuePickerHtml[] = ' class="form-control tceforms-select tceforms-wizardselect"';
187  $valuePickerHtml[] = ' onchange="' . htmlspecialchars($assignValue . ';this.blur();this.selectedIndex=0;' . implode('', $fieldChangeFunc)) . '"';
188  $valuePickerHtml[] = '>';
189  $valuePickerHtml[] = '<option></option>';
190  foreach ($config['valuePicker']['items'] as $item) {
191  $valuePickerHtml[] = '<option value="' . htmlspecialchars($item[1]) . '">' . htmlspecialchars($languageService->sL($item[0])) . '</option>';
192  }
193  $valuePickerHtml[] = '</select>';
194  }
195 
196  $valueSliderHtml = [];
197  if (isset($config['slider']) && is_array($config['slider'])) {
198  $id = 'slider-' . $fieldId;
199  $resultArray['requireJsModules'][] = ['TYPO3/CMS/Backend/FormEngine/FieldWizard/ValueSlider' =>
200  'function(ValueSlider) { new ValueSlider(' . GeneralUtility::quoteJSvalue($id) . '); }'
201  ];
202  $min = $config['range']['lower'] ?? 0;
203  $max = $config['range']['upper'] ?? 10000;
204  $step = $config['slider']['step'] ?? 1;
205  $width = $config['slider']['width'] ?? 400;
206  $valueType = 'null';
207  if (in_array('int', $evalList, true)) {
208  $valueType = 'int';
209  $itemValue = (int)$itemValue;
210  } elseif (in_array('double2', $evalList, true)) {
211  $valueType = 'double';
212  $itemValue = (double)$itemValue;
213  }
214  $callbackParams = [ $table, $row['uid'], $fieldName, $parameterArray['itemFormElName'] ];
215  $valueSliderHtml[] = '<div';
216  $valueSliderHtml[] = ' id="' . $id . '"';
217  $valueSliderHtml[] = ' data-slider-id="' . $id . '"';
218  $valueSliderHtml[] = ' data-slider-min="' . (int)$min . '"';
219  $valueSliderHtml[] = ' data-slider-max="' . (int)$max . '"';
220  $valueSliderHtml[] = ' data-slider-step="' . htmlspecialchars($step) . '"';
221  $valueSliderHtml[] = ' data-slider-value="' . htmlspecialchars($itemValue) . '"';
222  $valueSliderHtml[] = ' data-slider-value-type="' . htmlspecialchars($valueType) . '"';
223  $valueSliderHtml[] = ' data-slider-item-name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
224  $valueSliderHtml[] = ' data-slider-callback-params="' . htmlspecialchars(json_encode($callbackParams)) . '"';
225  $valueSliderHtml[] = ' style="width: ' . $width . 'px;"';
226  $valueSliderHtml[] = '>';
227  $valueSliderHtml[] = '</div>';
228  }
229 
230  $fieldControlResult = $this->‪renderFieldControl();
231  $fieldControlHtml = $fieldControlResult['html'];
232  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
233 
234  $fieldWizardResult = $this->‪renderFieldWizard();
235  $fieldWizardHtml = $fieldWizardResult['html'];
236  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
237  $inputType = 'text';
238 
239  if (in_array('email', $evalList, true)) {
240  $inputType = 'email';
241  } elseif (!empty(array_intersect($evalList, ['int', 'num']))) {
242  $inputType = 'number';
243 
244  if (isset($config['range']['lower'])) {
245  $attributes['min'] = (int)$config['range']['lower'];
246  }
247  if (isset($config['range']['upper'])) {
248  $attributes['max'] = (int)$config['range']['upper'];
249  }
250  }
251 
252  $mainFieldHtml = [];
253  $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
254  $mainFieldHtml[] = '<div class="form-wizards-wrap">';
255  $mainFieldHtml[] = '<div class="form-wizards-element">';
256  $mainFieldHtml[] = '<input type="' . $inputType . '" ' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
257  $mainFieldHtml[] = '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($itemValue) . '" />';
258  $mainFieldHtml[] = '</div>';
259  if (!empty($valuePickerHtml) || !empty($valueSliderHtml) || !empty($fieldControlHtml)) {
260  $mainFieldHtml[] = '<div class="form-wizards-items-aside">';
261  $mainFieldHtml[] = '<div class="btn-group">';
262  $mainFieldHtml[] = implode(LF, $valuePickerHtml);
263  $mainFieldHtml[] = implode(LF, $valueSliderHtml);
264  $mainFieldHtml[] = $fieldControlHtml;
265  $mainFieldHtml[] = '</div>';
266  $mainFieldHtml[] = '</div>';
267  }
268  if (!empty($fieldWizardHtml)) {
269  $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
270  $mainFieldHtml[] = $fieldWizardHtml;
271  $mainFieldHtml[] = '</div>';
272  }
273  $mainFieldHtml[] = '</div>';
274  $mainFieldHtml[] = '</div>';
275  $mainFieldHtml = implode(LF, $mainFieldHtml);
276 
277  $fullElement = $mainFieldHtml;
278  if ($this->‪hasNullCheckboxButNoPlaceholder()) {
279  $checked = $itemValue !== null ? ' checked="checked"' : '';
280  $fullElement = [];
281  $fullElement[] = '<div class="t3-form-field-disable"></div>';
282  $fullElement[] = '<div class="checkbox t3-form-field-eval-null-checkbox">';
283  $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
284  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
285  $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
286  $fullElement[] = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
287  $fullElement[] = '</label>';
288  $fullElement[] = '</div>';
289  $fullElement[] = $mainFieldHtml;
290  $fullElement = implode(LF, $fullElement);
291  } elseif ($this->‪hasNullCheckboxWithPlaceholder()) {
292  $checked = $itemValue !== null ? ' checked="checked"' : '';
293  $placeholder = $shortenedPlaceholder = $config['placeholder'] ?? '';
294  $disabled = '';
295  $fallbackValue = 0;
296  if (strlen($placeholder) > 0) {
297  $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
298  if ($placeholder !== $shortenedPlaceholder) {
299  $overrideLabel = sprintf(
300  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
301  '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
302  );
303  } else {
304  $overrideLabel = sprintf(
305  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
306  htmlspecialchars($placeholder)
307  );
308  }
309  } else {
310  $overrideLabel = $languageService->sL(
311  'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
312  );
313  }
314  $fullElement = [];
315  $fullElement[] = '<div class="checkbox t3js-form-field-eval-null-placeholder-checkbox">';
316  $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
317  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="' . $fallbackValue . '" />';
318  $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . $disabled . ' />';
319  $fullElement[] = $overrideLabel;
320  $fullElement[] = '</label>';
321  $fullElement[] = '</div>';
322  $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
323  $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
324  $fullElement[] = '<input type="text" class="form-control" disabled="disabled" value="' . htmlspecialchars($shortenedPlaceholder) . '" />';
325  $fullElement[] = '</div>';
326  $fullElement[] = '</div>';
327  $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
328  $fullElement[] = $mainFieldHtml;
329  $fullElement[] = '</div>';
330  $fullElement = implode(LF, $fullElement);
331  }
332 
333  $resultArray['html'] = '<div class="formengine-field-item t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
334  return $resultArray;
335  }
336 
340  protected function ‪getLanguageService()
341  {
342  return ‪$GLOBALS['LANG'];
343  }
344 }
‪TYPO3\CMS\Backend\Form\Element\InputTextElement
Definition: InputTextElement.php:29
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:71
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:115
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪array initializeResultArray()
Definition: AbstractNode.php:88
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:31
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:2
‪TYPO3\CMS\Backend\Form\Element\InputTextElement\getLanguageService
‪LanguageService getLanguageService()
Definition: InputTextElement.php:338
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪string getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:153
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldControl
‪array renderFieldControl()
Definition: AbstractFormElement.php:87
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\hasNullCheckboxWithPlaceholder
‪bool hasNullCheckboxWithPlaceholder()
Definition: AbstractFormElement.php:166
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\hasNullCheckboxButNoPlaceholder
‪bool hasNullCheckboxButNoPlaceholder()
Definition: AbstractFormElement.php:131
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\formMaxWidth
‪int formMaxWidth($size=48)
Definition: AbstractFormElement.php:297
‪TYPO3\CMS\Backend\Form\Element\InputTextElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: InputTextElement.php:44
‪TYPO3\CMS\Backend\Form\Element\InputTextElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: InputTextElement.php:34
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:91
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:21
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:103
‪TYPO3\CMS\Backend\Form\Element\InputTextElement\render
‪array render()
Definition: InputTextElement.php:67