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