SelectSingleBoxElement.php 8.05 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
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
20
use TYPO3\CMS\Core\Utility\StringUtility;
21
22
23
24

/**
 * Create a widget with a select box where multiple items can be selected
 *
25
 * This is rendered for config type=select, renderType=selectSingleBox
26
 */
27
28
class SelectSingleBoxElement extends AbstractFormElement
{
29
30
31
32
33
34
35
36
37
38
39
    /**
     * Default field information enabled for this element.
     *
     * @var array
     */
    protected $defaultFieldInformation = [
        'tcaDescription' => [
            'renderType' => 'tcaDescription',
        ],
    ];

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    /**
     * Default field controls for this element.
     *
     * @var array
     */
    protected $defaultFieldControl = [
        'resetSelection' => [
            'renderType' => 'resetSelection',
        ],
    ];

    /**
     * Default field wizards enabled for this element.
     *
     * @var array
     */
    protected $defaultFieldWizard = [
57
58
59
        'localizationStateSelector' => [
            'renderType' => 'localizationStateSelector',
        ],
60
61
        'otherLanguageContent' => [
            'renderType' => 'otherLanguageContent',
62
63
64
            'after' => [
                'localizationStateSelector'
            ],
65
66
67
68
69
70
71
72
73
        ],
        'defaultLanguageDifferences' => [
            'renderType' => 'defaultLanguageDifferences',
            'after' => [
                'otherLanguageContent',
            ],
        ],
    ];

74
75
76
77
78
79
80
    /**
     * This will render a selector box element, or possibly a special construction with two selector boxes.
     *
     * @return array As defined in initializeResultArray() of AbstractNode
     */
    public function render()
    {
81
82
83
        $languageService = $this->getLanguageService();
        $resultArray = $this->initializeResultArray();

84
85
86
87
        $parameterArray = $this->data['parameterArray'];
        // Field configuration from TCA:
        $config = $parameterArray['fieldConf']['config'];
        $selectItems = $parameterArray['fieldConf']['config']['items'];
88
        $disabled = !empty($config['readOnly']);
89
90
91

        // Get values in an array (and make unique, which is fine because there can be no duplicates anyway):
        $itemArray = array_flip($parameterArray['itemFormElValue']);
92
        $width = $this->formMaxWidth($this->defaultInputWidth);
93

94
        $optionElements = [];
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
        foreach ($selectItems as $i => $item) {
            $value = $item[1];
            $attributes = [];
            // Selected or not by default
            if (isset($itemArray[$value])) {
                $attributes['selected'] = 'selected';
                unset($itemArray[$value]);
            }
            // Non-selectable element
            if ((string)$value === '--div--') {
                $attributes['disabled'] = 'disabled';
                $attributes['class'] = 'formcontrol-select-divider';
            }
            $optionElements[] = $this->renderOptionElement($value, $item[0], $attributes);
        }

        $selectElement = $this->renderSelectElement($optionElements, $parameterArray, $config);

113
114
115
        $fieldInformationResult = $this->renderFieldInformation();
        $fieldInformationHtml = $fieldInformationResult['html'];
        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
116

117
        $fieldControlResult = $this->renderFieldControl();
118
        $fieldControlHtml = $fieldControlResult['html'];
119
        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
120

121
        $fieldWizardResult = $this->renderFieldWizard();
122
        $fieldWizardHtml = $fieldWizardResult['html'];
123
124
125
        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);

        $html = [];
126
        $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
127
        $html[] = $fieldInformationHtml;
128
129
130
131
132
133
134
135
136
137
        $html[] =   '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
        $html[] =       '<div class="form-wizards-wrap form-wizards-aside">';
        $html[] =           '<div class="form-wizards-element">';
        if (!$disabled) {
            // Add an empty hidden field which will send a blank value if all items are unselected.
            $html[] =           '<input type="hidden" name="' . htmlspecialchars($parameterArray['itemFormElName']) . '" value="">';
        }
        $html[] =               $selectElement;
        $html[] =           '</div>';
        if (!$disabled) {
138
139
140
141
142
            if (!empty($fieldControlHtml)) {
                $html[] =       '<div class="form-wizards-items-aside">';
                $html[] =           $fieldControlHtml;
                $html[] =       '</div>';
            }
143
            $html[] = '</div>'; // Close form-wizards-aside
144
            $html[] =   '<p>';
145
            $html[] =       '<em>' . htmlspecialchars($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.holdDownCTRL')) . '</em>';
146
            $html[] =   '</p>';
147
148
149
150
151
            if (!empty($fieldWizardHtml)) {
                $html[] = '<div class="form-wizards-items-bottom">';
                $html[] = $fieldWizardHtml;
                $html[] = '</div>';
            }
152
153
        } else {
            $html[] = '</div>'; // Close form-wizards-aside
154
155
156
157
158
        }
        $html[] =   '</div>';
        $html[] = '</div>';

        $resultArray['html'] = implode(LF, $html);
159
160
161
162
163
164
165
166
167
168
169
170
171
172
        return $resultArray;
    }

    /**
     * Renders a <select> element
     *
     * @param array $optionElements List of rendered <option> elements
     * @param array $parameterArray
     * @param array $config Field configuration
     * @return string
     */
    protected function renderSelectElement(array $optionElements, array $parameterArray, array $config)
    {
        $selectItems = $parameterArray['fieldConf']['config']['items'];
173
        $size = (int)($config['size'] ?? 0);
174
        $prefix = $size === 1 ? 'tceforms-select' : 'tceforms-multiselect';
175

176
        if ($config['autoSizeMax'] ?? false) {
177
178
179
180
181
182
183
184
185
186
187
            $size = MathUtility::forceIntegerInRange(
                count($selectItems) + 1,
                MathUtility::forceIntegerInRange($size, 1),
                $config['autoSizeMax']
            );
        }

        $attributes = [
            'name' => $parameterArray['itemFormElName'] . '[]',
            'multiple' => 'multiple',
            'onchange' => implode('', $parameterArray['fieldChangeFunc']),
188
189
            'id' => StringUtility::getUniqueId($prefix),
            'class' => 'form-select ',
190
            'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
191
192
        ];
        if ($size) {
193
            $attributes['size'] = (string)$size;
194
        }
195
        if ($config['readOnly'] ?? false) {
196
197
198
            $attributes['disabled'] = 'disabled';
        }

199
200
201
202
        $html = [];
        $html[] = '<select ' . GeneralUtility::implodeAttributes($attributes, true) . '>';
        $html[] =   implode(LF, $optionElements);
        $html[] = '</select>';
203
204
205
206
207
208
209
210
211
212
213
214
215
216

        return implode(LF, $html);
    }

    /**
     * Renders a single <option> element
     *
     * @param string $value The option value
     * @param string $label The option label
     * @param array $attributes Map of attribute names and values
     * @return string
     */
    protected function renderOptionElement($value, $label, array $attributes = [])
    {
217
        $attributes['value'] = $value;
218
        $html = [
219
            '<option ' . GeneralUtility::implodeAttributes($attributes, true) . '>',
220
                htmlspecialchars($this->appendValueToLabelInDebugMode($label, $value), ENT_COMPAT, 'UTF-8', false),
221
222
223
224
225
226
            '</option>'

        ];

        return implode('', $html);
    }
227
}