‪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  $attributes = [
135  'value' => '',
136  'id' => $fieldId,
137  'class' => implode(' ', [
138  'form-control',
139  'form-control-clearable',
140  't3js-clearable',
141  'hasDefaultValue',
142  ]),
143  'data-formengine-validation-rules' => $this->‪getValidationDataAsJsonString($config),
144  'data-formengine-input-params' => (string)json_encode([
145  'field' => $itemName,
146  'evalList' => implode(',', $evalList),
147  'is_in' => trim($config['is_in'] ?? ''),
148  ], JSON_THROW_ON_ERROR),
149  'data-formengine-input-name' => $itemName,
150  ];
151 
152  $maxLength = (int)($config['max'] ?? 0);
153  if ($maxLength > 0) {
154  $attributes['maxlength'] = (string)$maxLength;
155  }
156  $minLength = (int)($config['min'] ?? 0);
157  if ($minLength > 0 && ($maxLength === 0 || $minLength <= $maxLength)) {
158  $attributes['minlength'] = (string)$minLength;
159  }
160  if (!empty($config['placeholder'])) {
161  $attributes['placeholder'] = trim($config['placeholder']);
162  }
163  if (isset($config['autocomplete'])) {
164  $attributes['autocomplete'] = empty($config['autocomplete']) ? 'new-' . $fieldName : 'on';
165  }
166 
167  $valuePickerHtml = [];
168  if (is_array($config['valuePicker']['items'] ?? false)) {
169  $valuePickerConfiguration = [
170  'mode' => $config['valuePicker']['mode'] ?? 'replace',
171  'linked-field' => '[data-formengine-input-name="' . $itemName . '"]',
172  ];
173  $valuePickerAttributes = array_merge(
174  [
175  'class' => 'form-select form-control-adapt',
176  ],
177  $this->getOnFieldChangeAttrs('change', $parameterArray['fieldChangeFunc'] ?? [])
178  );
179 
180  $valuePickerHtml[] = '<typo3-formengine-valuepicker ' . GeneralUtility::implodeAttributes($valuePickerConfiguration, true) . '>';
181  $valuePickerHtml[] = '<select ' . GeneralUtility::implodeAttributes($valuePickerAttributes, true) . '>';
182  $valuePickerHtml[] = '<option></option>';
183  foreach ($config['valuePicker']['items'] as $item) {
184  $valuePickerHtml[] = '<option value="' . htmlspecialchars($item[1]) . '">' . htmlspecialchars($languageService->sL($item[0])) . '</option>';
185  }
186  $valuePickerHtml[] = '</select>';
187  $valuePickerHtml[] = '</typo3-formengine-valuepicker>';
188 
189  $resultArray['javaScriptModules'][] = ‪JavaScriptModuleInstruction::create('@typo3/backend/form-engine/field-wizard/value-picker.js');
190  }
191 
192  $fieldControlResult = $this->‪renderFieldControl();
193  $fieldControlHtml = $fieldControlResult['html'];
194  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
195 
196  $fieldWizardResult = $this->‪renderFieldWizard();
197  $fieldWizardHtml = $fieldWizardResult['html'];
198  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
199 
200  $mainFieldHtml = [];
201  $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
202  $mainFieldHtml[] = '<div class="form-wizards-wrap">';
203  $mainFieldHtml[] = '<div class="form-wizards-element">';
204  $mainFieldHtml[] = '<input type="text" ' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
205  $mainFieldHtml[] = '<input type="hidden" name="' . $itemName . '" value="' . htmlspecialchars((string)$itemValue) . '" />';
206  $mainFieldHtml[] = '</div>';
207  if (!empty($valuePickerHtml) || !empty($fieldControlHtml)) {
208  $mainFieldHtml[] = '<div class="form-wizards-items-aside form-wizards-items-aside--field-control">';
209  $mainFieldHtml[] = '<div class="btn-group">';
210  $mainFieldHtml[] = implode(LF, $valuePickerHtml);
211  $mainFieldHtml[] = $fieldControlHtml;
212  $mainFieldHtml[] = '</div>';
213  $mainFieldHtml[] = '</div>';
214  }
215  if (!empty($fieldWizardHtml)) {
216  $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
217  $mainFieldHtml[] = $fieldWizardHtml;
218  $mainFieldHtml[] = '</div>';
219  }
220  $mainFieldHtml[] = '</div>';
221  $mainFieldHtml[] = '</div>';
222  $mainFieldHtml = implode(LF, $mainFieldHtml);
223 
224  $nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $this->data['databaseRow']['uid'] . '][' . $fieldName . ']');
225 
226  $fullElement = $mainFieldHtml;
227  if ($this->‪hasNullCheckboxButNoPlaceholder()) {
228  $checked = $itemValue !== null ? ' checked="checked"' : '';
229  $fullElement = [];
230  $fullElement[] = '<div class="t3-form-field-disable"></div>';
231  $fullElement[] = '<div class="form-check t3-form-field-eval-null-checkbox">';
232  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
233  $fullElement[] = '<input type="checkbox" class="form-check-input" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
234  $fullElement[] = '<label class="form-check-label" for="' . $nullControlNameEscaped . '">';
235  $fullElement[] = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
236  $fullElement[] = '</label>';
237  $fullElement[] = '</div>';
238  $fullElement[] = $mainFieldHtml;
239  $fullElement = implode(LF, $fullElement);
240  } elseif ($this->‪hasNullCheckboxWithPlaceholder()) {
241  $checked = $itemValue !== null ? ' checked="checked"' : '';
242  $placeholder = $shortenedPlaceholder = trim((string)($config['placeholder'] ?? ''));
243  if ($placeholder !== '') {
244  $shortenedPlaceholder = ‪GeneralUtility::fixed_lgd_cs($placeholder, 20);
245  if ($placeholder !== $shortenedPlaceholder) {
246  $overrideLabel = sprintf(
247  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
248  '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
249  );
250  } else {
251  $overrideLabel = sprintf(
252  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
253  htmlspecialchars($placeholder)
254  );
255  }
256  } else {
257  $overrideLabel = $languageService->sL(
258  'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
259  );
260  }
261  $fullElement = [];
262  $fullElement[] = '<div class="form-check t3js-form-field-eval-null-placeholder-checkbox">';
263  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
264  $fullElement[] = '<input type="checkbox" class="form-check-input" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
265  $fullElement[] = '<label class="form-check-label" for="' . $nullControlNameEscaped . '">';
266  $fullElement[] = $overrideLabel;
267  $fullElement[] = '</label>';
268  $fullElement[] = '</div>';
269  $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
270  $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
271  $fullElement[] = '<input type="text" class="form-control" disabled="disabled" value="' . htmlspecialchars($shortenedPlaceholder) . '" />';
272  $fullElement[] = '</div>';
273  $fullElement[] = '</div>';
274  $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
275  $fullElement[] = $mainFieldHtml;
276  $fullElement[] = '</div>';
277  $fullElement = implode(LF, $fullElement);
278  }
279 
280  $resultArray['html'] = $renderedLabel . '
281  <div class="formengine-field-item t3js-formengine-field-item">
282  ' . $fieldInformationHtml . $fullElement . '
283  </div>';
284 
285  return $resultArray;
286  }
287 }
‪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:817
‪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