TYPO3 CMS  TYPO3_8-7
TextElement.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
22 
27 {
33  protected $defaultFieldWizard = [
34  'localizationStateSelector' => [
35  'renderType' => 'localizationStateSelector',
36  ],
37  'otherLanguageContent' => [
38  'renderType' => 'otherLanguageContent',
39  'after' => [
40  'localizationStateSelector'
41  ],
42  ],
43  'defaultLanguageDifferences' => [
44  'renderType' => 'defaultLanguageDifferences',
45  'after' => [
46  'otherLanguageContent',
47  ],
48  ],
49  ];
50 
57  protected $charactersPerRow = 40;
58 
64  public function render()
65  {
66  $languageService = $this->getLanguageService();
67  $backendUser = $this->getBackendUserAuthentication();
68 
69  $table = $this->data['tableName'];
70  $fieldName = $this->data['fieldName'];
71  $row = $this->data['databaseRow'];
72  $parameterArray = $this->data['parameterArray'];
73  $resultArray = $this->initializeResultArray();
74 
75  $itemValue = $parameterArray['itemFormElValue'];
76  $config = $parameterArray['fieldConf']['config'];
77  $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
78  $cols = MathUtility::forceIntegerInRange($config['cols'] ?: $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
79  $width = $this->formMaxWidth($cols);
80  $nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $row['uid'] . '][' . $fieldName . ']');
81 
82  // Setting number of rows
83  $rows = MathUtility::forceIntegerInRange($config['rows'] ?: 5, 1, 20);
84  $originalRows = $rows;
85  $itemFormElementValueLength = strlen($itemValue);
86  if ($itemFormElementValueLength > $this->charactersPerRow * 2) {
88  round($itemFormElementValueLength / $this->charactersPerRow),
89  count(explode(LF, $itemValue)),
90  20
91  );
92  if ($rows < $originalRows) {
93  $rows = $originalRows;
94  }
95  }
96 
97  if ($config['readOnly']) {
98  $html = [];
99  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
100  $html[] = '<div class="form-wizards-wrap">';
101  $html[] = '<div class="form-wizards-element">';
102  $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
103  $html[] = '<textarea class="form-control" rows="' . $rows . '" disabled>';
104  $html[] = htmlspecialchars($itemValue);
105  $html[] = '</textarea>';
106  $html[] = '</div>';
107  $html[] = '</div>';
108  $html[] = '</div>';
109  $html[] = '</div>';
110  $resultArray['html'] = implode(LF, $html);
111  return $resultArray;
112  }
113 
114  // @todo: The whole eval handling is a mess and needs refactoring
115  foreach ($evalList as $func) {
116  // @todo: This is ugly: The code should find out on it's own whether a eval definition is a
117  // @todo: keyword like "date", or a class reference. The global registration could be dropped then
118  // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
119  // There is a similar hook for "evaluateFieldValue" in DataHandler and InputTextElement
120  if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
121  if (class_exists($func)) {
122  $evalObj = GeneralUtility::makeInstance($func);
123  if (method_exists($evalObj, 'deevaluateFieldValue')) {
124  $_params = [
125  'value' => $itemValue
126  ];
127  $itemValue = $evalObj->deevaluateFieldValue($_params);
128  }
129  }
130  }
131  }
132 
133  $attributes = [
134  'id' => StringUtility::getUniqueId('formengine-textarea-'),
135  'name' => htmlspecialchars($parameterArray['itemFormElName']),
136  'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
137  'data-formengine-input-name' => htmlspecialchars($parameterArray['itemFormElName']),
138  'rows' => $rows,
139  'wrap' => $config['wrap'] ?: 'virtual',
140  'onChange' => implode('', $parameterArray['fieldChangeFunc']),
141  ];
142  $classes = [
143  'form-control',
144  't3js-formengine-textarea',
145  'formengine-textarea',
146  ];
147  if ($config['fixedFont']) {
148  $classes[] = 'text-monospace';
149  }
150  if ($config['enableTabulator']) {
151  $classes[] = 't3js-enable-tab';
152  }
153  $attributes['class'] = implode(' ', $classes);
154  $maximumHeight = (int)$backendUser->uc['resizeTextareas_MaxHeight'];
155  if ($maximumHeight > 0) {
156  // add the max-height from the users' preference to it
157  $attributes['style'] = 'max-height: ' . $maximumHeight . 'px';
158  }
159  if (isset($config['max']) && (int)$config['max'] > 0) {
160  $attributes['maxlength'] = (int)$config['max'];
161  }
162  if (!empty($config['placeholder'])) {
163  $attributes['placeholder'] = htmlspecialchars(trim($config['placeholder']));
164  }
165 
166  $valuePickerHtml = [];
167  if (isset($config['valuePicker']['items']) && is_array($config['valuePicker']['items'])) {
168  $mode = $config['valuePicker']['mode'] ?? '';
169  $itemName = $parameterArray['itemFormElName'];
170  $fieldChangeFunc = $parameterArray['fieldChangeFunc'];
171  if ($mode === 'append') {
172  $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
173  . '.value=\'\'+this.options[this.selectedIndex].value+document.editform[' . GeneralUtility::quoteJSvalue($itemName) . '].value';
174  } elseif ($mode === 'prepend') {
175  $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
176  . '.value+=\'\'+this.options[this.selectedIndex].value';
177  } else {
178  $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
179  . '.value=this.options[this.selectedIndex].value';
180  }
181  $valuePickerHtml[] = '<select';
182  $valuePickerHtml[] = ' class="form-control tceforms-select tceforms-wizardselect"';
183  $valuePickerHtml[] = ' onchange="' . htmlspecialchars($assignValue . ';this.blur();this.selectedIndex=0;' . implode('', $fieldChangeFunc)) . '"';
184  $valuePickerHtml[] = '>';
185  $valuePickerHtml[] = '<option></option>';
186  foreach ($config['valuePicker']['items'] as $item) {
187  $valuePickerHtml[] = '<option value="' . htmlspecialchars($item[1]) . '">' . htmlspecialchars($languageService->sL($item[0])) . '</option>';
188  }
189  $valuePickerHtml[] = '</select>';
190  }
191 
192  $legacyWizards = $this->renderWizards();
193  $legacyFieldControlHtml = implode(LF, $legacyWizards['fieldControl']);
194  $legacyFieldWizardHtml = implode(LF, $legacyWizards['fieldWizard']);
195 
196  $fieldInformationResult = $this->renderFieldInformation();
197  $fieldInformationHtml = $fieldInformationResult['html'];
198  $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
199 
200  $fieldControlResult = $this->renderFieldControl();
201  $fieldControlHtml = $legacyFieldControlHtml . $fieldControlResult['html'];
202  $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
203 
204  $fieldWizardResult = $this->renderFieldWizard();
205  $fieldWizardHtml = $legacyFieldWizardHtml . $fieldWizardResult['html'];
206  $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
207 
208  $mainFieldHtml = [];
209  $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
210  $mainFieldHtml[] = '<div class="form-wizards-wrap">';
211  $mainFieldHtml[] = '<div class="form-wizards-element">';
212  $mainFieldHtml[] = '<textarea ' . GeneralUtility::implodeAttributes($attributes, true) . '>' . htmlspecialchars($itemValue) . '</textarea>';
213  $mainFieldHtml[] = '</div>';
214  $mainFieldHtml[] = '<div class="form-wizards-items-aside">';
215  $mainFieldHtml[] = '<div class="btn-group">';
216  $mainFieldHtml[] = implode(LF, $valuePickerHtml);
217  $mainFieldHtml[] = $fieldControlHtml;
218  $mainFieldHtml[] = '</div>';
219  $mainFieldHtml[] = '</div>';
220  $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
221  $mainFieldHtml[] = $fieldWizardHtml;
222  $mainFieldHtml[] = '</div>';
223  $mainFieldHtml[] = '</div>';
224  $mainFieldHtml[] = '</div>';
225  $mainFieldHtml = implode(LF, $mainFieldHtml);
226 
227  $fullElement = $mainFieldHtml;
228  if ($this->hasNullCheckboxButNoPlaceholder()) {
229  $checked = $itemValue !== null ? ' checked="checked"' : '';
230  $fullElement = [];
231  $fullElement[] = '<div class="t3-form-field-disable"></div>';
232  $fullElement[] = '<div class="checkbox t3-form-field-eval-null-checkbox">';
233  $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
234  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
235  $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
236  $fullElement[] = $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
237  $fullElement[] = '</label>';
238  $fullElement[] = '</div>';
239  $fullElement[] = $mainFieldHtml;
240  $fullElement = implode(LF, $fullElement);
241  } elseif ($this->hasNullCheckboxWithPlaceholder()) {
242  $checked = $itemValue !== null ? ' checked="checked"' : '';
243  $placeholder = $shortenedPlaceholder = $config['placeholder'] ?? '';
244  $disabled = '';
245  $fallbackValue = 0;
246  if (strlen($placeholder) > 0) {
247  $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
248  if ($placeholder !== $shortenedPlaceholder) {
249  $overrideLabel = sprintf(
250  $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
251  '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
252  );
253  } else {
254  $overrideLabel = sprintf(
255  $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
256  htmlspecialchars($placeholder)
257  );
258  }
259  } else {
260  $overrideLabel = $languageService->sL(
261  'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
262  );
263  }
264  $fullElement = [];
265  $fullElement[] = '<div class="checkbox t3js-form-field-eval-null-placeholder-checkbox">';
266  $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
267  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="' . $fallbackValue . '" />';
268  $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . $disabled . ' />';
269  $fullElement[] = $overrideLabel;
270  $fullElement[] = '</label>';
271  $fullElement[] = '</div>';
272  $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
273  $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
274  $fullElement[] = '<textarea';
275  $fullElement[] = ' class="form-control formengine-textarea' . (isset($config['fixedFont']) ? ' text-monospace' : '') . '"';
276  $fullElement[] = ' disabled="disabled"';
277  $fullElement[] = ' rows="' . htmlspecialchars($attributes['rows']) . '"';
278  $fullElement[] = ' wrap="' . htmlspecialchars($attributes['wrap']) . '"';
279  $fullElement[] = isset($attributes['style']) ? ' style="' . htmlspecialchars($attributes['style']) . '"' : '';
280  $fullElement[] = isset($attributes['maxlength']) ? ' maxlength="' . htmlspecialchars($attributes['maxlength']) . '"' : '';
281  $fullElement[] = '>';
282  $fullElement[] = htmlspecialchars($shortenedPlaceholder);
283  $fullElement[] = '</textarea>';
284  $fullElement[] = '</div>';
285  $fullElement[] = '</div>';
286  $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
287  $fullElement[] = $mainFieldHtml;
288  $fullElement[] = '</div>';
289  $fullElement = implode(LF, $fullElement);
290  }
291 
292  $resultArray['html'] = '<div class="formengine-field-item t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
293  return $resultArray;
294  }
295 
299  protected function getBackendUserAuthentication()
300  {
301  return $GLOBALS['BE_USER'];
302  }
303 
307  protected function getLanguageService()
308  {
309  return $GLOBALS['LANG'];
310  }
311 }
static implodeAttributes(array $arr, $xhtmlSafe=false, $dontOmitBlankAttribs=false)
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
renderWizards( $itemKinds=null, $wizConf=null, $table=null, $row=null, $fieldName=null, $PA=null, $itemName=null, $specConf=null, $RTE=null)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
static makeInstance($className,... $constructorArguments)
static fixed_lgd_cs($string, $chars, $appendString='...')
mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']