‪TYPO3CMS  11.5
TextElement.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;
25 
30 {
31  use OnFieldChangeTrait;
32 
38  protected ‪$defaultFieldInformation = [
39  'tcaDescription' => [
40  'renderType' => 'tcaDescription',
41  ],
42  ];
43 
49  protected ‪$defaultFieldWizard = [
50  'localizationStateSelector' => [
51  'renderType' => 'localizationStateSelector',
52  ],
53  'otherLanguageContent' => [
54  'renderType' => 'otherLanguageContent',
55  'after' => [
56  'localizationStateSelector',
57  ],
58  ],
59  'defaultLanguageDifferences' => [
60  'renderType' => 'defaultLanguageDifferences',
61  'after' => [
62  'otherLanguageContent',
63  ],
64  ],
65  ];
66 
73  protected ‪$charactersPerRow = 40;
74 
80  public function ‪render()
81  {
82  $languageService = $this->‪getLanguageService();
83  $backendUser = $this->‪getBackendUserAuthentication();
84 
85  $table = $this->data['tableName'];
86  $fieldName = $this->data['fieldName'];
87  $row = $this->data['databaseRow'];
88  $parameterArray = $this->data['parameterArray'];
89  $resultArray = $this->‪initializeResultArray();
90 
91  $itemValue = $parameterArray['itemFormElValue'];
92  $config = $parameterArray['fieldConf']['config'];
93  $evalList = ‪GeneralUtility::trimExplode(',', $config['eval'] ?? '', true);
94  $width = null;
95  if ($config['cols'] ?? false) {
96  $width = $this->‪formMaxWidth(‪MathUtility::forceIntegerInRange($config['cols'], $this->minimumInputWidth, $this->maxInputWidth));
97  }
98  $nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $row['uid'] . '][' . $fieldName . ']');
99 
100  // Setting number of rows
101  $rows = ‪MathUtility::forceIntegerInRange(($config['rows'] ?? 5) ?: 5, 1, 20);
102  $originalRows = $rows;
103  $itemFormElementValueLength = strlen((string)$itemValue);
104  if ($itemFormElementValueLength > $this->charactersPerRow * 2) {
106  (int)round($itemFormElementValueLength / $this->charactersPerRow),
107  count(explode(LF, $itemValue)),
108  20
109  );
110  if ($rows < $originalRows) {
111  $rows = $originalRows;
112  }
113  }
114 
115  $fieldInformationResult = $this->‪renderFieldInformation();
116  $fieldInformationHtml = $fieldInformationResult['html'];
117  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
118 
119  if ($config['readOnly'] ?? false) {
120  $html = [];
121  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
122  $html[] = $fieldInformationHtml;
123  $html[] = '<div class="form-wizards-wrap">';
124  $html[] = '<div class="form-wizards-element">';
125  $html[] = '<div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px">' : '>');
126  $html[] = '<textarea class="form-control" rows="' . $rows . '" disabled>';
127  $html[] = htmlspecialchars((string)$itemValue);
128  $html[] = '</textarea>';
129  $html[] = '</div>';
130  $html[] = '</div>';
131  $html[] = '</div>';
132  $html[] = '</div>';
133  $resultArray['html'] = implode(LF, $html);
134  return $resultArray;
135  }
136 
137  // @todo: The whole eval handling is a mess and needs refactoring
138  foreach ($evalList as $func) {
139  // @todo: This is ugly: The code should find out on it's own whether an eval definition is a
140  // @todo: keyword like "date", or a class reference. The global registration could be dropped then
141  // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
142  // There is a similar hook for "evaluateFieldValue" in DataHandler and InputTextElement
143  if (isset(‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
144  if (class_exists($func)) {
145  $evalObj = GeneralUtility::makeInstance($func);
146  if (method_exists($evalObj, 'deevaluateFieldValue')) {
147  $_params = [
148  'value' => $itemValue,
149  ];
150  $itemValue = $evalObj->deevaluateFieldValue($_params);
151  }
152  }
153  }
154  }
155 
156  $fieldId = ‪StringUtility::getUniqueId('formengine-textarea-');
157 
158  $attributes = array_merge(
159  [
160  'id' => $fieldId,
161  'name' => htmlspecialchars($parameterArray['itemFormElName']),
162  'data-formengine-validation-rules' => $this->‪getValidationDataAsJsonString($config),
163  'data-formengine-input-name' => htmlspecialchars($parameterArray['itemFormElName']),
164  'rows' => (string)$rows,
165  'wrap' => (string)(($config['wrap'] ?? 'virtual') ?: 'virtual'),
166  ],
167  $this->getOnFieldChangeAttrs('change', $parameterArray['fieldChangeFunc'] ?? [])
168  );
169  $classes = [
170  'form-control',
171  't3js-formengine-textarea',
172  'formengine-textarea',
173  ];
174  if ($config['fixedFont'] ?? false) {
175  $classes[] = 'text-monospace';
176  }
177  if ($config['enableTabulator'] ?? false) {
178  $classes[] = 't3js-enable-tab';
179  }
180  $attributes['class'] = implode(' ', $classes);
181  $maximumHeight = (int)$backendUser->uc['resizeTextareas_MaxHeight'];
182  ‪if ($maximumHeight > 0) {
183  // add the max-height from the users' preference to it
184  $attributes['style'] = 'max-height: ' . $maximumHeight . 'px';
185  }
186  if (isset($config['max']) && (int)$config['max'] > 0) {
187  $attributes['maxlength'] = (string)(int)$config['max'];
188  }
189  if (!empty($config['placeholder'])) {
190  $attributes['placeholder'] = htmlspecialchars(trim($config['placeholder']));
191  }
192 
193  $valuePickerHtml = [];
194  if (isset($config['valuePicker']['items']) && is_array($config['valuePicker']['items'])) {
195  $valuePickerConfiguration = [
196  'mode' => $config['valuePicker']['mode'] ?? 'replace',
197  'linked-field' => '[data-formengine-input-name="' . $parameterArray['itemFormElName'] . '"]',
198  ];
199  $valuePickerAttributes = array_merge(
200  [
201  'class' => 'form-select form-control-adapt',
202  ],
203  $this->getOnFieldChangeAttrs('change', $parameterArray['fieldChangeFunc'] ?? [])
204  );
205 
206  $valuePickerHtml[] = '<typo3-formengine-valuepicker ' . GeneralUtility::implodeAttributes($valuePickerConfiguration, true) . '>';
207  $valuePickerHtml[] = '<select ' . GeneralUtility::implodeAttributes($valuePickerAttributes, true) . '>';
208  $valuePickerHtml[] = '<option></option>';
209  foreach ($config['valuePicker']['items'] as $item) {
210  $valuePickerHtml[] = '<option value="' . htmlspecialchars($item[1]) . '">' . htmlspecialchars($languageService->sL($item[0])) . '</option>';
211  }
212  $valuePickerHtml[] = '</select>';
213  $valuePickerHtml[] = '</typo3-formengine-valuepicker>';
214 
215  $resultArray['requireJsModules'][] = ‪JavaScriptModuleInstruction::forRequireJS('TYPO3/CMS/Backend/FormEngine/FieldWizard/ValuePicker');
216  }
217 
218  $fieldControlResult = $this->‪renderFieldControl();
219  $fieldControlHtml = $fieldControlResult['html'];
220  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
221 
222  $fieldWizardResult = $this->‪renderFieldWizard();
223  $fieldWizardHtml = $fieldWizardResult['html'];
224  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
225 
226  $mainFieldHtml = [];
227  $mainFieldHtml[] = '<div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px">' : '>');
228  $mainFieldHtml[] = '<div class="form-wizards-wrap">';
229  $mainFieldHtml[] = '<div class="form-wizards-element">';
230  $mainFieldHtml[] = '<textarea ' . GeneralUtility::implodeAttributes($attributes, true) . '>' . htmlspecialchars((string)$itemValue) . '</textarea>';
231  $mainFieldHtml[] = '</div>';
232  if (!empty($valuePickerHtml) || !empty($fieldControlHtml)) {
233  $mainFieldHtml[] = '<div class="form-wizards-items-aside form-wizards-items-aside--field-control">';
234  $mainFieldHtml[] = '<div class="btn-group">';
235  $mainFieldHtml[] = implode(LF, $valuePickerHtml);
236  $mainFieldHtml[] = $fieldControlHtml;
237  $mainFieldHtml[] = '</div>';
238  $mainFieldHtml[] = '</div>';
239  }
240  if (!empty($fieldWizardHtml)) {
241  $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
242  $mainFieldHtml[] = $fieldWizardHtml;
243  $mainFieldHtml[] = '</div>';
244  }
245  $mainFieldHtml[] = '</div>';
246  $mainFieldHtml[] = '</div>';
247  $mainFieldHtml = implode(LF, $mainFieldHtml);
248 
249  $fullElement = $mainFieldHtml;
250  if ($this->‪hasNullCheckboxButNoPlaceholder()) {
251  $checked = $itemValue !== null ? ' checked="checked"' : '';
252  $fullElement = [];
253  $fullElement[] = '<div class="t3-form-field-disable"></div>';
254  $fullElement[] = '<div class="form-check t3-form-field-eval-null-checkbox">';
255  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
256  $fullElement[] = '<input type="checkbox" class="form-check-input" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
257  $fullElement[] = '<label class="form-check-label" for="' . $nullControlNameEscaped . '">';
258  $fullElement[] = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
259  $fullElement[] = '</label>';
260  $fullElement[] = '</div>';
261  $fullElement[] = $mainFieldHtml;
262  $fullElement = implode(LF, $fullElement);
263  } elseif ($this->‪hasNullCheckboxWithPlaceholder()) {
264  $checked = $itemValue !== null ? ' checked="checked"' : '';
265  $placeholder = $shortenedPlaceholder = $config['placeholder'] ?? '';
266  $disabled = '';
267  $fallbackValue = 0;
268  if (strlen($placeholder) > 0) {
269  $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
270  if ($placeholder !== $shortenedPlaceholder) {
271  $overrideLabel = sprintf(
272  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
273  '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
274  );
275  } else {
276  $overrideLabel = sprintf(
277  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
278  htmlspecialchars($placeholder)
279  );
280  }
281  } else {
282  $overrideLabel = $languageService->sL(
283  'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
284  );
285  }
286  $fullElement = [];
287  $fullElement[] = '<div class="form-check t3js-form-field-eval-null-placeholder-checkbox">';
288  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="' . $fallbackValue . '" />';
289  $fullElement[] = '<input type="checkbox" class="form-check-input" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . $disabled . ' />';
290  $fullElement[] = '<label class="form-check-label" for="' . $nullControlNameEscaped . '">';
291  $fullElement[] = $overrideLabel;
292  $fullElement[] = '</label>';
293  $fullElement[] = '</div>';
294  $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
295  $fullElement[] = '<div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px">' : '>');
296  $fullElement[] = '<textarea';
297  $fullElement[] = ' class="form-control formengine-textarea' . (isset($config['fixedFont']) ? ' text-monospace' : '') . '"';
298  $fullElement[] = ' disabled="disabled"';
299  $fullElement[] = ' rows="' . htmlspecialchars($attributes['rows']) . '"';
300  $fullElement[] = ' wrap="' . htmlspecialchars($attributes['wrap']) . '"';
301  $fullElement[] = isset($attributes['style']) ? ' style="' . htmlspecialchars($attributes['style']) . '"' : '';
302  $fullElement[] = isset($attributes['maxlength']) ? ' maxlength="' . htmlspecialchars($attributes['maxlength']) . '"' : '';
303  $fullElement[] = '>';
304  $fullElement[] = htmlspecialchars($shortenedPlaceholder);
305  $fullElement[] = '</textarea>';
306  $fullElement[] = '</div>';
307  $fullElement[] = '</div>';
308  $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
309  $fullElement[] = $mainFieldHtml;
310  $fullElement[] = '</div>';
311  $fullElement = implode(LF, $fullElement);
312  }
313 
314  $resultArray['requireJsModules'][] = ‪JavaScriptModuleInstruction::forRequireJS(
315  'TYPO3/CMS/Backend/FormEngine/Element/TextElement'
316  )->instance($fieldId);
317  $resultArray['html'] = '<div class="formengine-field-item t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
318  return $resultArray;
319  }
320 
324  protected function ‪getBackendUserAuthentication()
325  {
326  return ‪$GLOBALS['BE_USER'];
327  }
328 
332  protected function ‪getLanguageService()
333  {
334  return ‪$GLOBALS['LANG'];
335  }
336 }
‪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\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\TextElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: TextElement.php:36
‪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\TextElement\getBackendUserAuthentication
‪BackendUserAuthentication getBackendUserAuthentication()
Definition: TextElement.php:320
‪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\TextElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: TextElement.php:46
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\formMaxWidth
‪int formMaxWidth($size=48)
Definition: AbstractFormElement.php:304
‪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
‪if
‪if(PHP_SAPI !=='cli')
Definition: checkNamespaceIntegrity.php:25
‪TYPO3\CMS\Backend\Form\Element\TextElement
Definition: TextElement.php:30
‪TYPO3\CMS\Backend\Form\Element\TextElement\render
‪array render()
Definition: TextElement.php:76
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Backend\Form\Element\TextElement\getLanguageService
‪LanguageService getLanguageService()
Definition: TextElement.php:328
‪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\TextElement\$charactersPerRow
‪int $charactersPerRow
Definition: TextElement.php:69