‪TYPO3CMS  ‪main
InputTextElement.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 
31 {
37  protected ‪$defaultFieldInformation = [
38  'tcaDescription' => [
39  'renderType' => 'tcaDescription',
40  ],
41  ];
42 
48  protected ‪$defaultFieldWizard = [
49  'localizationStateSelector' => [
50  'renderType' => 'localizationStateSelector',
51  ],
52  'otherLanguageContent' => [
53  'renderType' => 'otherLanguageContent',
54  'after' => [
55  'localizationStateSelector',
56  ],
57  ],
58  'defaultLanguageDifferences' => [
59  'renderType' => 'defaultLanguageDifferences',
60  'after' => [
61  'otherLanguageContent',
62  ],
63  ],
64  ];
65 
71  public function ‪render(): array
72  {
73  $table = $this->data['tableName'];
74  $fieldName = $this->data['fieldName'];
75  $parameterArray = $this->data['parameterArray'];
76  $resultArray = $this->‪initializeResultArray();
77  $config = $parameterArray['fieldConf']['config'];
78 
79  $itemValue = $parameterArray['itemFormElValue'];
80  $width = $this->‪formMaxWidth(
81  ‪MathUtility::forceIntegerInRange($config['size'] ?? $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth)
82  );
83  $fieldId = ‪StringUtility::getUniqueId('formengine-input-');
84  $renderedLabel = $this->‪renderLabel($fieldId);
85 
86  $fieldInformationResult = $this->‪renderFieldInformation();
87  $fieldInformationHtml = $fieldInformationResult['html'];
88  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
89 
90  if ($config['readOnly'] ?? false) {
91  $html = [];
92  $html[] = $renderedLabel;
93  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
94  $html[] = $fieldInformationHtml;
95  $html[] = '<div class="form-wizards-wrap">';
96  $html[] = '<div class="form-wizards-element">';
97  $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
98  $html[] = '<input class="form-control" id="' . htmlspecialchars($fieldId) . '" value="' . htmlspecialchars((string)$itemValue) . '" type="text" disabled>';
99  $html[] = '</div>';
100  $html[] = '</div>';
101  $html[] = '</div>';
102  $html[] = '</div>';
103  $resultArray['html'] = implode(LF, $html);
104  return $resultArray;
105  }
106 
107  $languageService = $this->‪getLanguageService();
108  $itemName = (string)$parameterArray['itemFormElName'];
109 
110  // @todo: The whole eval handling is a mess and needs refactoring
111  $evalList = ‪GeneralUtility::trimExplode(',', $config['eval'] ?? '', true);
112  foreach ($evalList as $func) {
113  // @todo: This is ugly: The code should find out on it's own whether an eval definition is a
114  // @todo: keyword like "date", or a class reference. The global registration could be dropped then
115  // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
116  if (isset(‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
117  if (class_exists($func)) {
118  $evalObj = GeneralUtility::makeInstance($func);
119  if (method_exists($evalObj, 'deevaluateFieldValue')) {
120  $_params = [
121  'value' => $itemValue,
122  ];
123  $itemValue = $evalObj->deevaluateFieldValue($_params);
124  }
125  $resultArray = $this->‪resolveJavaScriptEvaluation($resultArray, $func, $evalObj);
126  }
127  }
128  }
129 
130  if ($config['nullable'] ?? false) {
131  $evalList[] = 'null';
132  }
133 
134  $formEngineInputParams = [
135  'field' => $itemName,
136  ];
137  // The `is_in` constraint requires two parameters to work: the "eval" setting and a configuration of the
138  // actually allowed characters
139  if (in_array('is_in', $evalList, true)) {
140  if (($config['is_in'] ?? '') !== '') {
141  $formEngineInputParams['is_in'] = $config['is_in'];
142  } else {
143  $evalList = array_diff($evalList, ['is_in']);
144  }
145  } else {
146  unset($config['is_in']);
147  }
148  if ($evalList !== []) {
149  $formEngineInputParams['evalList'] = implode(',', $evalList);
150  }
151 
152  $attributes = [
153  'value' => '',
154  'id' => $fieldId,
155  'class' => implode(' ', [
156  'form-control',
157  'form-control-clearable',
158  't3js-clearable',
159  'hasDefaultValue',
160  ]),
161  'data-formengine-validation-rules' => $this->‪getValidationDataAsJsonString($config),
162  'data-formengine-input-params' => (string)json_encode($formEngineInputParams, JSON_THROW_ON_ERROR),
163  'data-formengine-input-name' => $itemName,
164  ];
165 
166  $maxLength = (int)($config['max'] ?? 0);
167  if ($maxLength > 0) {
168  $attributes['maxlength'] = (string)$maxLength;
169  }
170  $minLength = (int)($config['min'] ?? 0);
171  if ($minLength > 0 && ($maxLength === 0 || $minLength <= $maxLength)) {
172  $attributes['minlength'] = (string)$minLength;
173  }
174  if (!empty($config['placeholder'])) {
175  $attributes['placeholder'] = trim($config['placeholder']);
176  }
177  if (isset($config['autocomplete'])) {
178  $attributes['autocomplete'] = empty($config['autocomplete']) ? 'new-' . $fieldName : 'on';
179  }
180 
181  $valuePickerHtml = [];
182  if (is_array($config['valuePicker']['items'] ?? false)) {
183  $valuePickerConfiguration = [
184  'mode' => $config['valuePicker']['mode'] ?? 'replace',
185  'linked-field' => '[data-formengine-input-name="' . $itemName . '"]',
186  ];
187  $valuePickerAttributes = array_merge(
188  [
189  'class' => 'form-select form-control-adapt',
190  ],
191  $this->getOnFieldChangeAttrs('change', $parameterArray['fieldChangeFunc'] ?? [])
192  );
193 
194  $valuePickerHtml[] = '<typo3-formengine-valuepicker ' . GeneralUtility::implodeAttributes($valuePickerConfiguration, true) . '>';
195  $valuePickerHtml[] = '<select ' . GeneralUtility::implodeAttributes($valuePickerAttributes, true) . '>';
196  $valuePickerHtml[] = '<option></option>';
197  foreach ($config['valuePicker']['items'] as $item) {
198  $valuePickerHtml[] = '<option value="' . htmlspecialchars($item[1]) . '">' . htmlspecialchars($languageService->sL($item[0])) . '</option>';
199  }
200  $valuePickerHtml[] = '</select>';
201  $valuePickerHtml[] = '</typo3-formengine-valuepicker>';
202 
203  $resultArray['javaScriptModules'][] = ‪JavaScriptModuleInstruction::create('@typo3/backend/form-engine/field-wizard/value-picker.js');
204  }
205 
206  $fieldControlResult = $this->‪renderFieldControl();
207  $fieldControlHtml = $fieldControlResult['html'];
208  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
209 
210  $fieldWizardResult = $this->‪renderFieldWizard();
211  $fieldWizardHtml = $fieldWizardResult['html'];
212  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
213 
214  $mainFieldHtml = [];
215  $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
216  $mainFieldHtml[] = '<div class="form-wizards-wrap">';
217  $mainFieldHtml[] = '<div class="form-wizards-element">';
218  $mainFieldHtml[] = '<input type="text" ' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
219  $mainFieldHtml[] = '<input type="hidden" name="' . $itemName . '" value="' . htmlspecialchars((string)$itemValue) . '" />';
220  $mainFieldHtml[] = '</div>';
221  if (!empty($valuePickerHtml) || !empty($fieldControlHtml)) {
222  $mainFieldHtml[] = '<div class="form-wizards-items-aside form-wizards-items-aside--field-control">';
223  $mainFieldHtml[] = '<div class="btn-group">';
224  $mainFieldHtml[] = implode(LF, $valuePickerHtml);
225  $mainFieldHtml[] = $fieldControlHtml;
226  $mainFieldHtml[] = '</div>';
227  $mainFieldHtml[] = '</div>';
228  }
229  if (!empty($fieldWizardHtml)) {
230  $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
231  $mainFieldHtml[] = $fieldWizardHtml;
232  $mainFieldHtml[] = '</div>';
233  }
234  $mainFieldHtml[] = '</div>';
235  $mainFieldHtml[] = '</div>';
236  $mainFieldHtml = implode(LF, $mainFieldHtml);
237 
238  $nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $this->data['databaseRow']['uid'] . '][' . $fieldName . ']');
239 
240  $fullElement = $mainFieldHtml;
241  if ($this->‪hasNullCheckboxButNoPlaceholder()) {
242  $checked = $itemValue !== null ? ' checked="checked"' : '';
243  $fullElement = [];
244  $fullElement[] = '<div class="t3-form-field-disable"></div>';
245  $fullElement[] = '<div class="form-check t3-form-field-eval-null-checkbox">';
246  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
247  $fullElement[] = '<input type="checkbox" class="form-check-input" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
248  $fullElement[] = '<label class="form-check-label" for="' . $nullControlNameEscaped . '">';
249  $fullElement[] = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
250  $fullElement[] = '</label>';
251  $fullElement[] = '</div>';
252  $fullElement[] = $mainFieldHtml;
253  $fullElement = implode(LF, $fullElement);
254  } elseif ($this->‪hasNullCheckboxWithPlaceholder()) {
255  $checked = $itemValue !== null ? ' checked="checked"' : '';
256  $placeholder = $shortenedPlaceholder = trim((string)($config['placeholder'] ?? ''));
257  if ($placeholder !== '') {
258  $shortenedPlaceholder = ‪GeneralUtility::fixed_lgd_cs($placeholder, 20);
259  if ($placeholder !== $shortenedPlaceholder) {
260  $overrideLabel = sprintf(
261  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
262  '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
263  );
264  } else {
265  $overrideLabel = sprintf(
266  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
267  htmlspecialchars($placeholder)
268  );
269  }
270  } else {
271  $overrideLabel = $languageService->sL(
272  'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
273  );
274  }
275  $fullElement = [];
276  $fullElement[] = '<div class="form-check t3js-form-field-eval-null-placeholder-checkbox">';
277  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
278  $fullElement[] = '<input type="checkbox" class="form-check-input" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
279  $fullElement[] = '<label class="form-check-label" for="' . $nullControlNameEscaped . '">';
280  $fullElement[] = $overrideLabel;
281  $fullElement[] = '</label>';
282  $fullElement[] = '</div>';
283  $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
284  $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
285  $fullElement[] = '<input type="text" class="form-control" disabled="disabled" value="' . htmlspecialchars($shortenedPlaceholder) . '" />';
286  $fullElement[] = '</div>';
287  $fullElement[] = '</div>';
288  $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
289  $fullElement[] = $mainFieldHtml;
290  $fullElement[] = '</div>';
291  $fullElement = implode(LF, $fullElement);
292  }
293 
294  $resultArray['html'] = $renderedLabel . '
295  <div class="formengine-field-item t3js-formengine-field-item">
296  ' . $fieldInformationHtml . $fullElement . '
297  </div>';
298 
299  return $resultArray;
300  }
301 }
‪TYPO3\CMS\Backend\Form\Element\InputTextElement
Definition: InputTextElement.php:31
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:73
‪TYPO3\CMS\Core\Utility\GeneralUtility\fixed_lgd_cs
‪static string fixed_lgd_cs(string $string, int $chars, string $appendString='...')
Definition: GeneralUtility.php:92
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:104
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\create
‪static create(string $name, string $exportName=null)
Definition: JavaScriptModuleInstruction.php:47
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:37
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\resolveJavaScriptEvaluation
‪resolveJavaScriptEvaluation(array $resultArray, string $name, ?object $evalObject)
Definition: AbstractFormElement.php:349
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldControl
‪array renderFieldControl()
Definition: AbstractFormElement.php:89
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\getLanguageService
‪getLanguageService()
Definition: AbstractFormElement.php:456
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\formMaxWidth
‪int formMaxWidth($size=48)
Definition: AbstractFormElement.php:332
‪TYPO3\CMS\Backend\Form\Element\InputTextElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: InputTextElement.php:46
‪TYPO3\CMS\Backend\Form\Element\InputTextElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: InputTextElement.php:36
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:133
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderLabel
‪renderLabel(string $for)
Definition: AbstractFormElement.php:119
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange(mixed $theInt, int $min, int $max=2000000000, int $defaultValue=0)
Definition: MathUtility.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:105
‪TYPO3\CMS\Backend\Form\Element\InputTextElement\render
‪array render()
Definition: InputTextElement.php:69
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\hasNullCheckboxWithPlaceholder
‪hasNullCheckboxWithPlaceholder()
Definition: AbstractFormElement.php:195
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\hasNullCheckboxButNoPlaceholder
‪hasNullCheckboxButNoPlaceholder()
Definition: AbstractFormElement.php:163
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪initializeResultArray()
Definition: AbstractNode.php:77