InputTextElement.php 16.7 KB
Newer Older
1
<?php
2

3
/*
4
5
6
7
8
9
10
11
12
13
14
15
 * 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!
 */

16
17
namespace TYPO3\CMS\Backend\Form\Element;

18
use TYPO3\CMS\Core\Localization\LanguageService;
19
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
21
use TYPO3\CMS\Core\Utility\StringUtility;
22
23

/**
24
25
26
27
 * General type=input element.
 *
 * This one kicks in if no specific renderType like "inputDateTime"
 * or "inputColorPicker" is set.
28
 */
29
30
class InputTextElement extends AbstractFormElement
{
31
32
33
34
35
36
37
38
39
40
41
    /**
     * Default field information enabled for this element.
     *
     * @var array
     */
    protected $defaultFieldInformation = [
        'tcaDescription' => [
            'renderType' => 'tcaDescription',
        ],
    ];

42
43
44
45
46
47
    /**
     * Default field wizards enabled for this element.
     *
     * @var array
     */
    protected $defaultFieldWizard = [
48
49
50
        'localizationStateSelector' => [
            'renderType' => 'localizationStateSelector',
        ],
51
52
        'otherLanguageContent' => [
            'renderType' => 'otherLanguageContent',
53
54
55
            'after' => [
                'localizationStateSelector'
            ],
56
57
58
59
60
61
62
63
64
        ],
        'defaultLanguageDifferences' => [
            'renderType' => 'defaultLanguageDifferences',
            'after' => [
                'otherLanguageContent',
            ],
        ],
    ];

65
66
67
68
69
70
71
    /**
     * 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()
    {
72
73
        $languageService = $this->getLanguageService();

74
75
76
77
78
79
        $table = $this->data['tableName'];
        $fieldName = $this->data['fieldName'];
        $row = $this->data['databaseRow'];
        $parameterArray = $this->data['parameterArray'];
        $resultArray = $this->initializeResultArray();

80
        $itemValue = $parameterArray['itemFormElValue'];
81
82
        $config = $parameterArray['fieldConf']['config'];
        $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
83
84
        $size = MathUtility::forceIntegerInRange($config['size'] ?? $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
        $width = (int)$this->formMaxWidth($size);
85
        $nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $row['uid'] . '][' . $fieldName . ']');
86

87
88
89
90
        $fieldInformationResult = $this->renderFieldInformation();
        $fieldInformationHtml = $fieldInformationResult['html'];
        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);

91
        if ($config['readOnly']) {
92
93
94
            // Early return for read only fields
            if (in_array('password', $evalList, true)) {
                $itemValue = $itemValue ? '*********' : '';
95
            }
96
97
98
99
100
101

            $disabledFieldAttributes = [
                'class' => 'form-control',
                'data-formengine-input-name' => $parameterArray['itemFormElName'],
                'type' => 'text',
                'value' => $itemValue,
102
                'placeholder' => trim($config['placeholder']) ?? '',
103
104
            ];

105
            $html = [];
106
            $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
107
            $html[] =   $fieldInformationHtml;
108
109
110
            $html[] =   '<div class="form-wizards-wrap">';
            $html[] =       '<div class="form-wizards-element">';
            $html[] =           '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
111
            $html[] =               '<input ' . GeneralUtility::implodeAttributes($disabledFieldAttributes, true) . ' disabled>';
112
113
114
115
116
117
            $html[] =           '</div>';
            $html[] =       '</div>';
            $html[] =   '</div>';
            $html[] = '</div>';
            $resultArray['html'] = implode(LF, $html);
            return $resultArray;
118
119
120
121
        }

        // @todo: The whole eval handling is a mess and needs refactoring
        foreach ($evalList as $func) {
122
            // @todo: This is ugly: The code should find out on it's own whether an eval definition is a
123
124
125
126
127
128
129
130
131
132
            // @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);
133
                    }
134
135
136
137
138
                    if (method_exists($evalObj, 'returnFieldJS')) {
                        $resultArray['additionalJavaScriptPost'][] = 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($func) . ']'
                            . ' = function(value) {' . $evalObj->returnFieldJS() . '};';
                    }
                }
139
140
            }
        }
141

142
143
        $fieldId = StringUtility::getUniqueId('formengine-input-');

144
145
        $attributes = [
            'value' => '',
146
            'id' => $fieldId,
147
148
149
150
151
152
            'class' => implode(' ', [
                'form-control',
                't3js-clearable',
                'hasDefaultValue',
            ]),
            'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
153
            'data-formengine-input-params' => (string)json_encode([
154
155
156
157
                'field' => $parameterArray['itemFormElName'],
                'evalList' => implode(',', $evalList),
                'is_in' => trim($config['is_in'])
            ]),
158
            'data-formengine-input-name' => (string)$parameterArray['itemFormElName'],
159
        ];
160

161
162
        $maxLength = $config['max'] ?? 0;
        if ((int)$maxLength > 0) {
163
            $attributes['maxlength'] = (string)(int)$maxLength;
164
        }
165
166
167
168
        if (!empty($config['placeholder'])) {
            $attributes['placeholder'] = trim($config['placeholder']);
        }
        if (isset($config['autocomplete'])) {
169
            $attributes['autocomplete'] = empty($config['autocomplete']) ? 'new-' . $fieldName : 'on';
170
171
        }

172
173
174
175
176
177
178
        $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]'
179
                    . '.value+=\'\'+this.options[this.selectedIndex].value';
180
181
            } elseif ($mode === 'prepend') {
                $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
182
                    . '.value=\'\'+this.options[this.selectedIndex].value+document.editform[' . GeneralUtility::quoteJSvalue($itemName) . '].value';
183
184
185
186
187
            } else {
                $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
                    . '.value=this.options[this.selectedIndex].value';
            }
            $valuePickerHtml[] = '<select';
188
            $valuePickerHtml[] =  ' class="form-select form-control-adapt"';
189
190
191
192
193
194
195
            $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>';
196
197
        }

198
199
        $valueSliderHtml = [];
        if (isset($config['slider']) && is_array($config['slider'])) {
200
201
202
203
            $id = 'slider-' . $fieldId;
            $resultArray['requireJsModules'][] = ['TYPO3/CMS/Backend/FormEngine/FieldWizard/ValueSlider' =>
                'function(ValueSlider) { new ValueSlider(' . GeneralUtility::quoteJSvalue($id) . '); }'
            ];
204
205
206
207
208
209
210
211
212
213
214
215
216
            $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'] ];
217
218
219
220
            $rangeAttributes = [
                'id' => $id,
                'type' => 'range',
                'class' => 'slider',
221
222
223
                'min' => (string)(int)$min,
                'max' => (string)(int)$max,
                'step' => (string)$step,
224
                'style' => 'width: ' . (int)$width . 'px',
225
226
                'title' => (string)$itemValue,
                'value' => (string)$itemValue,
227
228
                'data-slider-id' => $id,
                'data-slider-value-type' => $valueType,
229
230
                'data-slider-item-name' => (string)($parameterArray['itemFormElName'] ?? ''),
                'data-slider-callback-params' => (string)json_encode($callbackParams),
231
232
233
            ];
            $valueSliderHtml[] = '<div class="slider-wrapper">';
            $valueSliderHtml[] = '<input ' . GeneralUtility::implodeAttributes($rangeAttributes, true) . '>';
234
235
            $valueSliderHtml[] = '</div>';
        }
236

237
        $fieldControlResult = $this->renderFieldControl();
238
        $fieldControlHtml = $fieldControlResult['html'];
239
240
241
        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);

        $fieldWizardResult = $this->renderFieldWizard();
242
        $fieldWizardHtml = $fieldWizardResult['html'];
243
        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
244
245
246
247
248
249
250
251
        $inputType = 'text';

        if (in_array('email', $evalList, true)) {
            $inputType = 'email';
        } elseif (!empty(array_intersect($evalList, ['int', 'num']))) {
            $inputType = 'number';

            if (isset($config['range']['lower'])) {
252
                $attributes['min'] = (string)(int)$config['range']['lower'];
253
254
            }
            if (isset($config['range']['upper'])) {
255
                $attributes['max'] = (string)(int)$config['range']['upper'];
256
257
            }
        }
258
259
260
261
262

        $mainFieldHtml = [];
        $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
        $mainFieldHtml[] =  '<div class="form-wizards-wrap">';
        $mainFieldHtml[] =      '<div class="form-wizards-element">';
263
        $mainFieldHtml[] =          '<input type="' . $inputType . '" ' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
264
265
        $mainFieldHtml[] =          '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($itemValue) . '" />';
        $mainFieldHtml[] =      '</div>';
266
267
268
269
270
271
272
273
274
275
276
277
278
279
        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>';
        }
280
281
282
283
284
285
286
287
288
289
        $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">';
290
291
292
            $fullElement[] =     '<label for="' . $nullControlNameEscaped . '">';
            $fullElement[] =         '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
            $fullElement[] =         '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
293
            $fullElement[] =         $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
294
295
296
297
298
299
            $fullElement[] =     '</label>';
            $fullElement[] = '</div>';
            $fullElement[] = $mainFieldHtml;
            $fullElement = implode(LF, $fullElement);
        } elseif ($this->hasNullCheckboxWithPlaceholder()) {
            $checked = $itemValue !== null ? ' checked="checked"' : '';
300
            $placeholder = $shortenedPlaceholder = trim($config['placeholder']) ?? '';
301
302
303
304
305
306
            $disabled = '';
            $fallbackValue = 0;
            if (strlen($placeholder) > 0) {
                $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
                if ($placeholder !== $shortenedPlaceholder) {
                    $overrideLabel = sprintf(
307
                        $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
308
309
310
311
                        '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
                    );
                } else {
                    $overrideLabel = sprintf(
312
                        $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
313
314
                        htmlspecialchars($placeholder)
                    );
315
                }
316
317
            } else {
                $overrideLabel = $languageService->sL(
318
                    'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
319
                );
320
            }
321
322
            $fullElement = [];
            $fullElement[] = '<div class="checkbox t3js-form-field-eval-null-placeholder-checkbox">';
323
324
325
            $fullElement[] =     '<label for="' . $nullControlNameEscaped . '">';
            $fullElement[] =         '<input type="hidden" name="' . $nullControlNameEscaped . '" value="' . $fallbackValue . '" />';
            $fullElement[] =         '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . $disabled . ' />';
326
327
328
329
330
            $fullElement[] =         $overrideLabel;
            $fullElement[] =     '</label>';
            $fullElement[] = '</div>';
            $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
            $fullElement[] =    '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
331
            $fullElement[] =        '<input type="text" class="form-control" disabled="disabled" value="' . htmlspecialchars($shortenedPlaceholder) . '" />';
332
333
334
335
336
337
            $fullElement[] =    '</div>';
            $fullElement[] = '</div>';
            $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
            $fullElement[] =    $mainFieldHtml;
            $fullElement[] = '</div>';
            $fullElement = implode(LF, $fullElement);
338
339
        }

340
        $resultArray['html'] = '<div class="formengine-field-item t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
341
342
        return $resultArray;
    }
343
344
345
346
347
348
349
350

    /**
     * @return LanguageService
     */
    protected function getLanguageService()
    {
        return $GLOBALS['LANG'];
    }
351
}