TextElement.php 15.2 KB
Newer Older
1
<?php
2

3
4
declare(strict_types=1);

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

18
19
namespace TYPO3\CMS\Backend\Form\Element;

20
use TYPO3\CMS\Core\Page\JavaScriptModuleInstruction;
21
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
23
use TYPO3\CMS\Core\Utility\StringUtility;
24
25

/**
26
27
28
 * General type=text element
 *
 * The InputTextElement renders a html textarea field.
29
 */
30
31
class TextElement extends AbstractFormElement
{
32
33
34
35
36
37
38
39
40
41
42
    /**
     * Default field information enabled for this element.
     *
     * @var array
     */
    protected $defaultFieldInformation = [
        'tcaDescription' => [
            'renderType' => 'tcaDescription',
        ],
    ];

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

66
67
68
69
70
71
72
73
74
75
76
77
78
    /**
     * The number of chars expected per row when the height of a text area field is
     * automatically calculated based on the number of characters found in the field content.
     *
     * @var int
     */
    protected $charactersPerRow = 40;

    /**
     * This will render a <textarea>
     *
     * @return array As defined in initializeResultArray() of AbstractNode
     */
79
    public function render(): array
80
81
82
83
84
85
    {
        $table = $this->data['tableName'];
        $fieldName = $this->data['fieldName'];
        $parameterArray = $this->data['parameterArray'];
        $resultArray = $this->initializeResultArray();

86
        $itemValue = $parameterArray['itemFormElValue'];
87
        $config = $parameterArray['fieldConf']['config'];
88
89
90
91
        $width = null;
        if ($config['cols'] ?? false) {
            $width = $this->formMaxWidth(MathUtility::forceIntegerInRange($config['cols'], $this->minimumInputWidth, $this->maxInputWidth));
        }
92
93

        // Setting number of rows
94
        $rows = MathUtility::forceIntegerInRange(($config['rows'] ?? 5) ?: 5, 1, 20);
95
        $originalRows = $rows;
96
        $itemFormElementValueLength = strlen((string)$itemValue);
97
        if ($itemFormElementValueLength > ($this->charactersPerRow * 2)) {
98
            $rows = MathUtility::forceIntegerInRange(
99
                (int)round($itemFormElementValueLength / $this->charactersPerRow),
100
                count(explode(LF, (string)$itemValue)),
101
102
103
104
105
106
107
                20
            );
            if ($rows < $originalRows) {
                $rows = $originalRows;
            }
        }

108
109
110
111
        $fieldInformationResult = $this->renderFieldInformation();
        $fieldInformationHtml = $fieldInformationResult['html'];
        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);

112
        if ($config['readOnly'] ?? false) {
113
            $html = [];
114
            $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
115
            $html[] =   $fieldInformationHtml;
116
117
            $html[] =   '<div class="form-wizards-wrap">';
            $html[] =       '<div class="form-wizards-element">';
118
            $html[] =           '<div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px">' : '>');
119
            $html[] =               '<textarea class="form-control" rows="' . $rows . '" disabled>';
120
            $html[] =                   htmlspecialchars((string)$itemValue);
121
122
123
124
125
126
127
            $html[] =               '</textarea>';
            $html[] =           '</div>';
            $html[] =       '</div>';
            $html[] =   '</div>';
            $html[] = '</div>';
            $resultArray['html'] = implode(LF, $html);
            return $resultArray;
128
129
        }

130
131
132
133
134
135
136
137
        $languageService = $this->getLanguageService();
        $fieldId = StringUtility::getUniqueId('formengine-textarea-');
        $itemName = (string)$parameterArray['itemFormElName'];

        // @todo: The whole eval handling is a mess and needs refactoring - Especially for this element,
        //        since the resolved $evalList is currently not used at all, because FormEngineValidation
        //        does not support eval for <textarea> elements.
        $evalList = GeneralUtility::trimExplode(',', $config['eval'] ?? '', true);
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
141
142
143
144
145
146
147
            // @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()
            // There is a similar hook for "evaluateFieldValue" in DataHandler and InputTextElement
            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 = [
148
                            'value' => $itemValue,
149
150
                        ];
                        $itemValue = $evalObj->deevaluateFieldValue($_params);
151
152
153
                    }
                }
            }
154
        }
155

156
157
158
159
        if ($config['nullable'] ?? false) {
            $evalList[] = 'null';
        }

160
161
162
        $attributes = array_merge(
            [
                'id' => $fieldId,
163
                'name' => $itemName,
164
                'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
165
                'data-formengine-input-name' => $itemName,
166
167
168
169
170
                'rows' => (string)$rows,
                'wrap' => (string)(($config['wrap'] ?? 'virtual') ?: 'virtual'),
            ],
            $this->getOnFieldChangeAttrs('change', $parameterArray['fieldChangeFunc'] ?? [])
        );
171
172
173
174
175
        $classes = [
            'form-control',
            't3js-formengine-textarea',
            'formengine-textarea',
        ];
176
        if ($config['fixedFont'] ?? false) {
177
178
            $classes[] = 'text-monospace';
        }
179
        if ($config['enableTabulator'] ?? false) {
180
181
182
            $classes[] = 't3js-enable-tab';
        }
        $attributes['class'] = implode(' ', $classes);
183

184
        if (isset($config['max']) && (int)$config['max'] > 0) {
185
            $attributes['maxlength'] = (string)(int)$config['max'];
186
187
        }
        if (!empty($config['placeholder'])) {
188
            $attributes['placeholder'] = trim($config['placeholder']);
189
        }
190

191
        $valuePickerHtml = [];
192
        if (is_array($config['valuePicker']['items'] ?? false)) {
193
194
            $valuePickerConfiguration = [
                'mode' => $config['valuePicker']['mode'] ?? 'replace',
195
                'linked-field' => '[data-formengine-input-name="' . $itemName . '"]',
196
            ];
197
198
199
200
201
202
            $valuePickerAttributes = array_merge(
                [
                    'class' => 'form-select form-control-adapt',
                ],
                $this->getOnFieldChangeAttrs('change', $parameterArray['fieldChangeFunc'] ?? [])
            );
203
204
205

            $valuePickerHtml[] = '<typo3-formengine-valuepicker ' . GeneralUtility::implodeAttributes($valuePickerConfiguration, true) . '>';
            $valuePickerHtml[] = '<select ' . GeneralUtility::implodeAttributes($valuePickerAttributes, true) . '>';
206
207
208
            $valuePickerHtml[] = '<option></option>';
            foreach ($config['valuePicker']['items'] as $item) {
                $valuePickerHtml[] = '<option value="' . htmlspecialchars($item[1]) . '">' . htmlspecialchars($languageService->sL($item[0])) . '</option>';
209
            }
210
            $valuePickerHtml[] = '</select>';
211
212
            $valuePickerHtml[] = '</typo3-formengine-valuepicker>';

213
            $resultArray['requireJsModules'][] = JavaScriptModuleInstruction::create('@typo3/backend/form-engine/field-wizard/value-picker.js');
214
        }
215

216
        $fieldControlResult = $this->renderFieldControl();
217
        $fieldControlHtml = $fieldControlResult['html'];
218
219
220
        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);

        $fieldWizardResult = $this->renderFieldWizard();
221
        $fieldWizardHtml = $fieldWizardResult['html'];
222
        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
223

224
        $mainFieldHtml = [];
225
        $mainFieldHtml[] = '<div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px">' : '>');
226
227
        $mainFieldHtml[] =  '<div class="form-wizards-wrap">';
        $mainFieldHtml[] =      '<div class="form-wizards-element">';
228
        $mainFieldHtml[] =          '<textarea ' . GeneralUtility::implodeAttributes($attributes, true) . '>' . htmlspecialchars((string)$itemValue) . '</textarea>';
229
        $mainFieldHtml[] =      '</div>';
230
        if (!empty($valuePickerHtml) || !empty($fieldControlHtml)) {
231
            $mainFieldHtml[] =      '<div class="form-wizards-items-aside form-wizards-items-aside--field-control">';
232
233
234
235
236
237
238
239
240
241
242
            $mainFieldHtml[] =          '<div class="btn-group">';
            $mainFieldHtml[] =              implode(LF, $valuePickerHtml);
            $mainFieldHtml[] =              $fieldControlHtml;
            $mainFieldHtml[] =          '</div>';
            $mainFieldHtml[] =      '</div>';
        }
        if (!empty($fieldWizardHtml)) {
            $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
            $mainFieldHtml[] = $fieldWizardHtml;
            $mainFieldHtml[] = '</div>';
        }
243
244
245
246
        $mainFieldHtml[] =  '</div>';
        $mainFieldHtml[] = '</div>';
        $mainFieldHtml = implode(LF, $mainFieldHtml);

247
248
        $nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $this->data['databaseRow']['uid'] . '][' . $fieldName . ']');

249
250
251
252
253
        $fullElement = $mainFieldHtml;
        if ($this->hasNullCheckboxButNoPlaceholder()) {
            $checked = $itemValue !== null ? ' checked="checked"' : '';
            $fullElement = [];
            $fullElement[] = '<div class="t3-form-field-disable"></div>';
254
255
256
257
            $fullElement[] = '<div class="form-check t3-form-field-eval-null-checkbox">';
            $fullElement[] =     '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
            $fullElement[] =     '<input type="checkbox" class="form-check-input" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
            $fullElement[] =     '<label class="form-check-label" for="' . $nullControlNameEscaped . '">';
258
            $fullElement[] =         $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
259
260
261
262
263
264
            $fullElement[] =     '</label>';
            $fullElement[] = '</div>';
            $fullElement[] = $mainFieldHtml;
            $fullElement = implode(LF, $fullElement);
        } elseif ($this->hasNullCheckboxWithPlaceholder()) {
            $checked = $itemValue !== null ? ' checked="checked"' : '';
265
266
            $placeholder = $shortenedPlaceholder = (string)($config['placeholder'] ?? '');
            if ($placeholder !== '') {
267
268
269
                $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
                if ($placeholder !== $shortenedPlaceholder) {
                    $overrideLabel = sprintf(
270
                        $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
271
272
273
274
                        '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
                    );
                } else {
                    $overrideLabel = sprintf(
275
                        $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
276
277
278
279
280
                        htmlspecialchars($placeholder)
                    );
                }
            } else {
                $overrideLabel = $languageService->sL(
281
                    'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
282
283
284
                );
            }
            $fullElement = [];
285
            $fullElement[] = '<div class="form-check t3js-form-field-eval-null-placeholder-checkbox">';
286
287
            $fullElement[] =     '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
            $fullElement[] =     '<input type="checkbox" class="form-check-input" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
288
            $fullElement[] =     '<label class="form-check-label" for="' . $nullControlNameEscaped . '">';
289
290
291
292
            $fullElement[] =         $overrideLabel;
            $fullElement[] =     '</label>';
            $fullElement[] = '</div>';
            $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
293
            $fullElement[] =    '<div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px">' : '>');
294
295
296
297
298
299
300
301
            $fullElement[] =        '<textarea';
            $fullElement[] =            ' class="form-control formengine-textarea' . (isset($config['fixedFont']) ? ' text-monospace'  : '') . '"';
            $fullElement[] =            ' disabled="disabled"';
            $fullElement[] =            ' rows="' . htmlspecialchars($attributes['rows']) . '"';
            $fullElement[] =            ' wrap="' . htmlspecialchars($attributes['wrap']) . '"';
            $fullElement[] =            isset($attributes['style']) ? ' style="' . htmlspecialchars($attributes['style']) . '"' : '';
            $fullElement[] =            isset($attributes['maxlength']) ? ' maxlength="' . htmlspecialchars($attributes['maxlength']) . '"' : '';
            $fullElement[] =        '>';
302
            $fullElement[] =            htmlspecialchars($shortenedPlaceholder);
303
304
305
306
307
308
309
            $fullElement[] =        '</textarea>';
            $fullElement[] =    '</div>';
            $fullElement[] = '</div>';
            $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
            $fullElement[] =    $mainFieldHtml;
            $fullElement[] = '</div>';
            $fullElement = implode(LF, $fullElement);
310
311
        }

312
313
314
315
316
317
318
319
320
        $resultArray['html'] = '
             <typo3-formengine-element-text recordFieldId="' . htmlspecialchars($fieldId) . '">
                <div class="formengine-field-item t3js-formengine-field-item">
                    ' . $fieldInformationHtml . $fullElement . '
                </div>
            </typo3-formengine-element-color>';

        $resultArray['requireJsModules'][] = JavaScriptModuleInstruction::create('@typo3/backend/form-engine/element/text-element.js');

321
322
        return $resultArray;
    }
323
}