‪TYPO3CMS  11.5
InputTextElement.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use TYPO3\CMS\Backend\Form\Behavior\OnFieldChangeTrait;
24 
32 {
33  use OnFieldChangeTrait;
34 
40  protected ‪$defaultFieldInformation = [
41  'tcaDescription' => [
42  'renderType' => 'tcaDescription',
43  ],
44  ];
45 
51  protected ‪$defaultFieldWizard = [
52  'localizationStateSelector' => [
53  'renderType' => 'localizationStateSelector',
54  ],
55  'otherLanguageContent' => [
56  'renderType' => 'otherLanguageContent',
57  'after' => [
58  'localizationStateSelector',
59  ],
60  ],
61  'defaultLanguageDifferences' => [
62  'renderType' => 'defaultLanguageDifferences',
63  'after' => [
64  'otherLanguageContent',
65  ],
66  ],
67  ];
68 
74  public function ‪render()
75  {
76  $languageService = $this->‪getLanguageService();
77 
78  $table = $this->data['tableName'];
79  $fieldName = $this->data['fieldName'];
80  $row = $this->data['databaseRow'];
81  $parameterArray = $this->data['parameterArray'];
82  $resultArray = $this->‪initializeResultArray();
83 
84  $itemValue = $parameterArray['itemFormElValue'];
85  $config = $parameterArray['fieldConf']['config'];
86  $evalList = ‪GeneralUtility::trimExplode(',', $config['eval'] ?? '', true);
87  $size = ‪MathUtility::forceIntegerInRange($config['size'] ?? $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
88  $width = $this->‪formMaxWidth($size);
89  $nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $row['uid'] . '][' . $fieldName . ']');
90 
91  $fieldInformationResult = $this->‪renderFieldInformation();
92  $fieldInformationHtml = $fieldInformationResult['html'];
93  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
94 
95  if ($config['readOnly'] ?? false) {
96  // Early return for read only fields
97  if (in_array('password', $evalList, true)) {
98  $itemValue = $this->‪getObfuscatedSecretValue($itemValue);
99  }
100 
101  $disabledFieldAttributes = [
102  'class' => 'form-control',
103  'data-formengine-input-name' => $parameterArray['itemFormElName'],
104  'type' => 'text',
105  'value' => $itemValue,
106  'placeholder' => trim($config['placeholder'] ?? ''),
107  ];
108 
109  $html = [];
110  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
111  $html[] = $fieldInformationHtml;
112  $html[] = '<div class="form-wizards-wrap">';
113  $html[] = '<div class="form-wizards-element">';
114  $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
115  $html[] = '<input ' . GeneralUtility::implodeAttributes($disabledFieldAttributes, true) . ' disabled>';
116  $html[] = '</div>';
117  $html[] = '</div>';
118  $html[] = '</div>';
119  $html[] = '</div>';
120  $resultArray['html'] = implode(LF, $html);
121  return $resultArray;
122  }
123 
124  // @todo: The whole eval handling is a mess and needs refactoring
125  foreach ($evalList as $func) {
126  // @todo: This is ugly: The code should find out on it's own whether an eval definition is a
127  // @todo: keyword like "date", or a class reference. The global registration could be dropped then
128  // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
129  if (isset(‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
130  if (class_exists($func)) {
131  $evalObj = GeneralUtility::makeInstance($func);
132  if (method_exists($evalObj, 'deevaluateFieldValue')) {
133  $_params = [
134  'value' => $itemValue,
135  ];
136  $itemValue = $evalObj->deevaluateFieldValue($_params);
137  }
138  $resultArray = $this->‪resolveJavaScriptEvaluation($resultArray, $func, $evalObj);
139  }
140  }
141  }
142 
143  $fieldId = ‪StringUtility::getUniqueId('formengine-input-');
144 
145  $attributes = [
146  'value' => '',
147  'id' => $fieldId,
148  'class' => implode(' ', [
149  'form-control',
150  't3js-clearable',
151  'hasDefaultValue',
152  ]),
153  'data-formengine-validation-rules' => $this->‪getValidationDataAsJsonString($config),
154  'data-formengine-input-params' => (string)json_encode([
155  'field' => $parameterArray['itemFormElName'],
156  'evalList' => implode(',', $evalList),
157  'is_in' => trim($config['is_in'] ?? ''),
158  ]),
159  'data-formengine-input-name' => (string)$parameterArray['itemFormElName'],
160  ];
161 
162  $maxLength = $config['max'] ?? 0;
163  if ((int)$maxLength > 0) {
164  $attributes['maxlength'] = (string)(int)$maxLength;
165  }
166  if (!empty($config['placeholder'])) {
167  $attributes['placeholder'] = trim($config['placeholder']);
168  }
169  if (isset($config['autocomplete'])) {
170  $attributes['autocomplete'] = empty($config['autocomplete']) ? 'new-' . $fieldName : 'on';
171  }
172 
173  $valuePickerHtml = [];
174  if (isset($config['valuePicker']['items']) && is_array($config['valuePicker']['items'])) {
175  $valuePickerConfiguration = [
176  'mode' => $config['valuePicker']['mode'] ?? 'replace',
177  'linked-field' => '[data-formengine-input-name="' . $parameterArray['itemFormElName'] . '"]',
178  ];
179  $valuePickerAttributes = array_merge(
180  [
181  'class' => 'form-select form-control-adapt',
182  ],
183  $this->getOnFieldChangeAttrs('change', $parameterArray['fieldChangeFunc'] ?? [])
184  );
185 
186  $valuePickerHtml[] = '<typo3-formengine-valuepicker ' . GeneralUtility::implodeAttributes($valuePickerConfiguration, true) . '>';
187  $valuePickerHtml[] = '<select ' . GeneralUtility::implodeAttributes($valuePickerAttributes, true) . '>';
188  $valuePickerHtml[] = '<option></option>';
189  foreach ($config['valuePicker']['items'] as $item) {
190  $valuePickerHtml[] = '<option value="' . htmlspecialchars($item[1]) . '">' . htmlspecialchars($languageService->sL($item[0])) . '</option>';
191  }
192  $valuePickerHtml[] = '</select>';
193  $valuePickerHtml[] = '</typo3-formengine-valuepicker>';
194 
195  $resultArray['requireJsModules'][] = ‪JavaScriptModuleInstruction::forRequireJS('TYPO3/CMS/Backend/FormEngine/FieldWizard/ValuePicker');
196  }
197 
198  $valueSliderHtml = [];
199  if (isset($config['slider']) && is_array($config['slider'])) {
200  $id = 'slider-' . $fieldId;
201  $resultArray['requireJsModules'][] = ‪JavaScriptModuleInstruction::forRequireJS(
202  'TYPO3/CMS/Backend/FormEngine/FieldWizard/ValueSlider'
203  )->instance($id);
204  $min = $config['range']['lower'] ?? 0;
205  $max = $config['range']['upper'] ?? 10000;
206  $step = $config['slider']['step'] ?? 1;
207  $width = $config['slider']['width'] ?? 400;
208  $valueType = 'null';
209  if (in_array('int', $evalList, true)) {
210  $valueType = 'int';
211  $itemValue = (int)$itemValue;
212  } elseif (in_array('double2', $evalList, true)) {
213  $valueType = 'double';
214  $itemValue = (float)$itemValue;
215  }
216  $rangeAttributes = [
217  'id' => $id,
218  'type' => 'range',
219  'class' => 'slider',
220  'min' => (string)(int)$min,
221  'max' => (string)(int)$max,
222  'step' => (string)$step,
223  'style' => 'width: ' . (int)$width . 'px',
224  'title' => (string)$itemValue,
225  'value' => (string)$itemValue,
226  'data-slider-id' => $id,
227  'data-slider-value-type' => $valueType,
228  'data-slider-item-name' => (string)($parameterArray['itemFormElName'] ?? ''),
229  ];
230  $valueSliderHtml[] = '<div class="slider-wrapper">';
231  $valueSliderHtml[] = '<input ' . GeneralUtility::implodeAttributes($rangeAttributes, true) . '>';
232  $valueSliderHtml[] = '</div>';
233  }
234 
235  $fieldControlResult = $this->‪renderFieldControl();
236  $fieldControlHtml = $fieldControlResult['html'];
237  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
238 
239  $fieldWizardResult = $this->‪renderFieldWizard();
240  $fieldWizardHtml = $fieldWizardResult['html'];
241  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
242  $inputType = 'text';
243  $hiddenElementProps = ' value="' . htmlspecialchars((string)$itemValue) . '"';
244 
245  if (in_array('email', $evalList, true)) {
246  $inputType = 'email';
247  } elseif (!empty(array_intersect($evalList, ['int', 'num']))) {
248  $inputType = 'number';
249 
250  if (isset($config['range']['lower'])) {
251  $attributes['min'] = (string)(int)$config['range']['lower'];
252  }
253  if (isset($config['range']['upper'])) {
254  $attributes['max'] = (string)(int)$config['range']['upper'];
255  }
256  }
257  if (in_array('password', $evalList, true)) {
258  $attributes['spellcheck'] = 'false';
259  $hiddenElementProps = ' value="' . htmlspecialchars($this->‪getObfuscatedSecretValue($itemValue)) . '" disabled data-enable-on-modification="true"';
260  }
261 
262  $mainFieldHtml = [];
263  $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
264  $mainFieldHtml[] = '<div class="form-wizards-wrap">';
265  $mainFieldHtml[] = '<div class="form-wizards-element">';
266  $mainFieldHtml[] = '<input type="' . $inputType . '" ' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
267  $mainFieldHtml[] = '<input type="hidden" name="' . htmlspecialchars($parameterArray['itemFormElName']) . '" ' . $hiddenElementProps . ' />';
268  $mainFieldHtml[] = '</div>';
269  if (!empty($valuePickerHtml) || !empty($valueSliderHtml) || !empty($fieldControlHtml)) {
270  $mainFieldHtml[] = '<div class="form-wizards-items-aside form-wizards-items-aside--field-control">';
271  $mainFieldHtml[] = '<div class="btn-group">';
272  $mainFieldHtml[] = implode(LF, $valuePickerHtml);
273  $mainFieldHtml[] = implode(LF, $valueSliderHtml);
274  $mainFieldHtml[] = $fieldControlHtml;
275  $mainFieldHtml[] = '</div>';
276  $mainFieldHtml[] = '</div>';
277  }
278  if (!empty($fieldWizardHtml)) {
279  $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
280  $mainFieldHtml[] = $fieldWizardHtml;
281  $mainFieldHtml[] = '</div>';
282  }
283  $mainFieldHtml[] = '</div>';
284  $mainFieldHtml[] = '</div>';
285  $mainFieldHtml = implode(LF, $mainFieldHtml);
286 
287  $fullElement = $mainFieldHtml;
288  if ($this->‪hasNullCheckboxButNoPlaceholder()) {
289  $checked = $itemValue !== null ? ' checked="checked"' : '';
290  $fullElement = [];
291  $fullElement[] = '<div class="t3-form-field-disable"></div>';
292  $fullElement[] = '<div class="form-check t3-form-field-eval-null-checkbox">';
293  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
294  $fullElement[] = '<input type="checkbox" class="form-check-input" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
295  $fullElement[] = '<label class="form-check-label" for="' . $nullControlNameEscaped . '">';
296  $fullElement[] = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
297  $fullElement[] = '</label>';
298  $fullElement[] = '</div>';
299  $fullElement[] = $mainFieldHtml;
300  $fullElement = implode(LF, $fullElement);
301  } elseif ($this->‪hasNullCheckboxWithPlaceholder()) {
302  $checked = $itemValue !== null ? ' checked="checked"' : '';
303  $placeholder = $shortenedPlaceholder = trim($config['placeholder'] ?? '');
304  $disabled = '';
305  $fallbackValue = 0;
306  if (strlen($placeholder) > 0) {
307  $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
308  if ($placeholder !== $shortenedPlaceholder) {
309  $overrideLabel = sprintf(
310  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
311  '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
312  );
313  } else {
314  $overrideLabel = sprintf(
315  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
316  htmlspecialchars($placeholder)
317  );
318  }
319  } else {
320  $overrideLabel = $languageService->sL(
321  'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
322  );
323  }
324  $fullElement = [];
325  $fullElement[] = '<div class="form-check t3js-form-field-eval-null-placeholder-checkbox">';
326  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="' . $fallbackValue . '" />';
327  $fullElement[] = '<input type="checkbox" class="form-check-input" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . $disabled . ' />';
328  $fullElement[] = '<label class="form-check-label" for="' . $nullControlNameEscaped . '">';
329  $fullElement[] = $overrideLabel;
330  $fullElement[] = '</label>';
331  $fullElement[] = '</div>';
332  $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
333  $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
334  $fullElement[] = '<input type="text" class="form-control" disabled="disabled" value="' . htmlspecialchars($shortenedPlaceholder) . '" />';
335  $fullElement[] = '</div>';
336  $fullElement[] = '</div>';
337  $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
338  $fullElement[] = $mainFieldHtml;
339  $fullElement[] = '</div>';
340  $fullElement = implode(LF, $fullElement);
341  }
342 
343  $resultArray['html'] = '<div class="formengine-field-item t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
344  return $resultArray;
345  }
346 
354  protected function ‪getObfuscatedSecretValue(?string $value): string
355  {
356  if ($value === null || $value === '') {
357  return '';
358  }
359  return '*********';
360  }
361 
365  protected function ‪getLanguageService()
366  {
367  return ‪$GLOBALS['LANG'];
368  }
369 }
‪TYPO3\CMS\Backend\Form\Element\InputTextElement
Definition: InputTextElement.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:999
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:76
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:120
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪array initializeResultArray()
Definition: AbstractNode.php:91
‪TYPO3\CMS\Backend\Form\Element\InputTextElement\getObfuscatedSecretValue
‪getObfuscatedSecretValue(?string $value)
Definition: InputTextElement.php:351
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:32
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:35
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\forRequireJS
‪static self forRequireJS(string $name, string $exportName=null)
Definition: JavaScriptModuleInstruction.php:49
‪TYPO3\CMS\Backend\Form\Element\InputTextElement\getLanguageService
‪LanguageService getLanguageService()
Definition: InputTextElement.php:362
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\resolveJavaScriptEvaluation
‪array resolveJavaScriptEvaluation(array $resultArray, string $name, ?object $evalObject)
Definition: AbstractFormElement.php:325
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪string getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:156
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldControl
‪array renderFieldControl()
Definition: AbstractFormElement.php:92
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\hasNullCheckboxWithPlaceholder
‪bool hasNullCheckboxWithPlaceholder()
Definition: AbstractFormElement.php:171
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\hasNullCheckboxButNoPlaceholder
‪bool hasNullCheckboxButNoPlaceholder()
Definition: AbstractFormElement.php:136
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\formMaxWidth
‪int formMaxWidth($size=48)
Definition: AbstractFormElement.php:304
‪TYPO3\CMS\Backend\Form\Element\InputTextElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: InputTextElement.php:48
‪TYPO3\CMS\Backend\Form\Element\InputTextElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: InputTextElement.php:38
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:128
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:108
‪TYPO3\CMS\Backend\Form\Element\InputTextElement\render
‪array render()
Definition: InputTextElement.php:71