/
InputTextElement.php
350 lines (324 loc) · 16.6 KB
/
InputTextElement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Backend\Form\Element;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
/**
* General type=input element.
*
* This one kicks in if no specific renderType like "inputDateTime"
* or "inputColorPicker" is set.
*/
class InputTextElement extends AbstractFormElement
{
/**
* Default field information enabled for this element.
*
* @var array
*/
protected $defaultFieldInformation = [
'tcaDescription' => [
'renderType' => 'tcaDescription',
],
];
/**
* Default field wizards enabled for this element.
*
* @var array
*/
protected $defaultFieldWizard = [
'localizationStateSelector' => [
'renderType' => 'localizationStateSelector',
],
'otherLanguageContent' => [
'renderType' => 'otherLanguageContent',
'after' => [
'localizationStateSelector'
],
],
'defaultLanguageDifferences' => [
'renderType' => 'defaultLanguageDifferences',
'after' => [
'otherLanguageContent',
],
],
];
/**
* This will render a single-line input form field, possibly with various control/validation features
*
* @return array As defined in initializeResultArray() of AbstractNode
*/
public function render()
{
$languageService = $this->getLanguageService();
$table = $this->data['tableName'];
$fieldName = $this->data['fieldName'];
$row = $this->data['databaseRow'];
$parameterArray = $this->data['parameterArray'];
$resultArray = $this->initializeResultArray();
$itemValue = $parameterArray['itemFormElValue'];
$config = $parameterArray['fieldConf']['config'];
$evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
$size = MathUtility::forceIntegerInRange($config['size'] ?? $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
$width = (int)$this->formMaxWidth($size);
$nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $row['uid'] . '][' . $fieldName . ']');
$fieldInformationResult = $this->renderFieldInformation();
$fieldInformationHtml = $fieldInformationResult['html'];
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
if ($config['readOnly']) {
// Early return for read only fields
if (in_array('password', $evalList, true)) {
$itemValue = $itemValue ? '*********' : '';
}
$disabledFieldAttributes = [
'class' => 'form-control',
'data-formengine-input-name' => $parameterArray['itemFormElName'],
'type' => 'text',
'value' => $itemValue,
];
$html = [];
$html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
$html[] = $fieldInformationHtml;
$html[] = '<div class="form-wizards-wrap">';
$html[] = '<div class="form-wizards-element">';
$html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
$html[] = '<input ' . GeneralUtility::implodeAttributes($disabledFieldAttributes, true) . ' disabled>';
$html[] = '</div>';
$html[] = '</div>';
$html[] = '</div>';
$html[] = '</div>';
$resultArray['html'] = implode(LF, $html);
return $resultArray;
}
// @todo: The whole eval handling is a mess and needs refactoring
foreach ($evalList as $func) {
// @todo: This is ugly: The code should find out on it's own whether an eval definition is a
// @todo: keyword like "date", or a class reference. The global registration could be dropped then
// Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
if (class_exists($func)) {
$evalObj = GeneralUtility::makeInstance($func);
if (method_exists($evalObj, 'deevaluateFieldValue')) {
$_params = [
'value' => $itemValue
];
$itemValue = $evalObj->deevaluateFieldValue($_params);
}
if (method_exists($evalObj, 'returnFieldJS')) {
$resultArray['additionalJavaScriptPost'][] = 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($func) . ']'
. ' = function(value) {' . $evalObj->returnFieldJS() . '};';
}
}
}
}
$fieldId = StringUtility::getUniqueId('formengine-input-');
$attributes = [
'value' => '',
'id' => $fieldId,
'class' => implode(' ', [
'form-control',
't3js-clearable',
'hasDefaultValue',
]),
'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
'data-formengine-input-params' => json_encode([
'field' => $parameterArray['itemFormElName'],
'evalList' => implode(',', $evalList),
'is_in' => trim($config['is_in'])
]),
'data-formengine-input-name' => $parameterArray['itemFormElName'],
];
$maxLength = $config['max'] ?? 0;
if ((int)$maxLength > 0) {
$attributes['maxlength'] = (int)$maxLength;
}
if (!empty($config['placeholder'])) {
$attributes['placeholder'] = trim($config['placeholder']);
}
if (isset($config['autocomplete'])) {
$attributes['autocomplete'] = empty($config['autocomplete']) ? 'new-' . $fieldName : 'on';
}
$valuePickerHtml = [];
if (isset($config['valuePicker']['items']) && is_array($config['valuePicker']['items'])) {
$mode = $config['valuePicker']['mode'] ?? '';
$itemName = $parameterArray['itemFormElName'];
$fieldChangeFunc = $parameterArray['fieldChangeFunc'];
if ($mode === 'append') {
$assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
. '.value+=\'\'+this.options[this.selectedIndex].value';
} elseif ($mode === 'prepend') {
$assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
. '.value=\'\'+this.options[this.selectedIndex].value+document.editform[' . GeneralUtility::quoteJSvalue($itemName) . '].value';
} else {
$assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
. '.value=this.options[this.selectedIndex].value';
}
$valuePickerHtml[] = '<select';
$valuePickerHtml[] = ' class="form-control tceforms-select tceforms-wizardselect"';
$valuePickerHtml[] = ' onchange="' . htmlspecialchars($assignValue . ';this.blur();this.selectedIndex=0;' . implode('', $fieldChangeFunc)) . '"';
$valuePickerHtml[] = '>';
$valuePickerHtml[] = '<option></option>';
foreach ($config['valuePicker']['items'] as $item) {
$valuePickerHtml[] = '<option value="' . htmlspecialchars($item[1]) . '">' . htmlspecialchars($languageService->sL($item[0])) . '</option>';
}
$valuePickerHtml[] = '</select>';
}
$valueSliderHtml = [];
if (isset($config['slider']) && is_array($config['slider'])) {
$id = 'slider-' . $fieldId;
$resultArray['requireJsModules'][] = ['TYPO3/CMS/Backend/FormEngine/FieldWizard/ValueSlider' =>
'function(ValueSlider) { new ValueSlider(' . GeneralUtility::quoteJSvalue($id) . '); }'
];
$min = $config['range']['lower'] ?? 0;
$max = $config['range']['upper'] ?? 10000;
$step = $config['slider']['step'] ?? 1;
$width = $config['slider']['width'] ?? 400;
$valueType = 'null';
if (in_array('int', $evalList, true)) {
$valueType = 'int';
$itemValue = (int)$itemValue;
} elseif (in_array('double2', $evalList, true)) {
$valueType = 'double';
$itemValue = (double)$itemValue;
}
$callbackParams = [ $table, $row['uid'], $fieldName, $parameterArray['itemFormElName'] ];
$rangeAttributes = [
'id' => $id,
'type' => 'range',
'class' => 'slider',
'min' => (int)$min,
'max' => (int)$max,
'step' => $step,
'style' => 'width: ' . (int)$width . 'px',
'title' => $itemValue,
'value' => $itemValue,
'data-slider-id' => $id,
'data-slider-value-type' => $valueType,
'data-slider-item-name' => $parameterArray['itemFormElName'],
'data-slider-callback-params' => json_encode($callbackParams),
];
$valueSliderHtml[] = '<div class="slider-wrapper">';
$valueSliderHtml[] = '<input ' . GeneralUtility::implodeAttributes($rangeAttributes, true) . '>';
$valueSliderHtml[] = '</div>';
}
$fieldControlResult = $this->renderFieldControl();
$fieldControlHtml = $fieldControlResult['html'];
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
$fieldWizardResult = $this->renderFieldWizard();
$fieldWizardHtml = $fieldWizardResult['html'];
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
$inputType = 'text';
if (in_array('email', $evalList, true)) {
$inputType = 'email';
} elseif (!empty(array_intersect($evalList, ['int', 'num']))) {
$inputType = 'number';
if (isset($config['range']['lower'])) {
$attributes['min'] = (int)$config['range']['lower'];
}
if (isset($config['range']['upper'])) {
$attributes['max'] = (int)$config['range']['upper'];
}
}
$mainFieldHtml = [];
$mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
$mainFieldHtml[] = '<div class="form-wizards-wrap">';
$mainFieldHtml[] = '<div class="form-wizards-element">';
$mainFieldHtml[] = '<input type="' . $inputType . '" ' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
$mainFieldHtml[] = '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($itemValue) . '" />';
$mainFieldHtml[] = '</div>';
if (!empty($valuePickerHtml) || !empty($valueSliderHtml) || !empty($fieldControlHtml)) {
$mainFieldHtml[] = '<div class="form-wizards-items-aside">';
$mainFieldHtml[] = '<div class="btn-group">';
$mainFieldHtml[] = implode(LF, $valuePickerHtml);
$mainFieldHtml[] = implode(LF, $valueSliderHtml);
$mainFieldHtml[] = $fieldControlHtml;
$mainFieldHtml[] = '</div>';
$mainFieldHtml[] = '</div>';
}
if (!empty($fieldWizardHtml)) {
$mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
$mainFieldHtml[] = $fieldWizardHtml;
$mainFieldHtml[] = '</div>';
}
$mainFieldHtml[] = '</div>';
$mainFieldHtml[] = '</div>';
$mainFieldHtml = implode(LF, $mainFieldHtml);
$fullElement = $mainFieldHtml;
if ($this->hasNullCheckboxButNoPlaceholder()) {
$checked = $itemValue !== null ? ' checked="checked"' : '';
$fullElement = [];
$fullElement[] = '<div class="t3-form-field-disable"></div>';
$fullElement[] = '<div class="checkbox t3-form-field-eval-null-checkbox">';
$fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
$fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
$fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
$fullElement[] = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
$fullElement[] = '</label>';
$fullElement[] = '</div>';
$fullElement[] = $mainFieldHtml;
$fullElement = implode(LF, $fullElement);
} elseif ($this->hasNullCheckboxWithPlaceholder()) {
$checked = $itemValue !== null ? ' checked="checked"' : '';
$placeholder = $shortenedPlaceholder = $config['placeholder'] ?? '';
$disabled = '';
$fallbackValue = 0;
if (strlen($placeholder) > 0) {
$shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
if ($placeholder !== $shortenedPlaceholder) {
$overrideLabel = sprintf(
$languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
'<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
);
} else {
$overrideLabel = sprintf(
$languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
htmlspecialchars($placeholder)
);
}
} else {
$overrideLabel = $languageService->sL(
'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
);
}
$fullElement = [];
$fullElement[] = '<div class="checkbox t3js-form-field-eval-null-placeholder-checkbox">';
$fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
$fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="' . $fallbackValue . '" />';
$fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . $disabled . ' />';
$fullElement[] = $overrideLabel;
$fullElement[] = '</label>';
$fullElement[] = '</div>';
$fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
$fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
$fullElement[] = '<input type="text" class="form-control" disabled="disabled" value="' . htmlspecialchars($shortenedPlaceholder) . '" />';
$fullElement[] = '</div>';
$fullElement[] = '</div>';
$fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
$fullElement[] = $mainFieldHtml;
$fullElement[] = '</div>';
$fullElement = implode(LF, $fullElement);
}
$resultArray['html'] = '<div class="formengine-field-item t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
return $resultArray;
}
/**
* @return LanguageService
*/
protected function getLanguageService()
{
return $GLOBALS['LANG'];
}
}