‪TYPO3CMS  ‪main
SelectSingleBoxElement.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 
28 {
34  protected ‪$defaultFieldInformation = [
35  'tcaDescription' => [
36  'renderType' => 'tcaDescription',
37  ],
38  ];
39 
45  protected ‪$defaultFieldControl = [
46  'resetSelection' => [
47  'renderType' => 'resetSelection',
48  ],
49  ];
50 
56  protected ‪$defaultFieldWizard = [
57  'localizationStateSelector' => [
58  'renderType' => 'localizationStateSelector',
59  ],
60  'otherLanguageContent' => [
61  'renderType' => 'otherLanguageContent',
62  'after' => [
63  'localizationStateSelector',
64  ],
65  ],
66  'defaultLanguageDifferences' => [
67  'renderType' => 'defaultLanguageDifferences',
68  'after' => [
69  'otherLanguageContent',
70  ],
71  ],
72  ];
73 
79  public function ‪render(): array
80  {
81  $languageService = $this->‪getLanguageService();
82  $resultArray = $this->‪initializeResultArray();
83 
84  $parameterArray = $this->data['parameterArray'];
85  // Field configuration from TCA:
86  $config = $parameterArray['fieldConf']['config'];
87  $selectItems = $parameterArray['fieldConf']['config']['items'];
88  $disabled = !empty($config['readOnly']);
89 
90  // Get item value as array and make unique, which is fine because there can be no duplicates anyway.
91  $itemArray = array_flip($parameterArray['itemFormElValue']);
92  $width = $this->‪formMaxWidth($this->defaultInputWidth);
93 
94  $optionElements = [];
95  foreach ($selectItems as $item) {
96  $value = $item['value'];
97  $attributes = [];
98  // Selected or not by default
99  if (isset($itemArray[$value])) {
100  $attributes['selected'] = 'selected';
101  unset($itemArray[$value]);
102  }
103  // Non-selectable element
104  if ((string)$value === '--div--') {
105  $attributes['disabled'] = 'disabled';
106  $attributes['class'] = 'formcontrol-select-divider';
107  }
108  $optionElements[] = $this->‪renderOptionElement($value, $item['label'], $attributes);
109  }
110 
111  $selectItems = $parameterArray['fieldConf']['config']['items'];
112  $size = (int)($config['size'] ?? 0);
113  $autoSizeMax = (int)($config['autoSizeMax'] ?? 0);
114  if ($autoSizeMax > 0) {
115  $size = ‪MathUtility::forceIntegerInRange($size, 1);
116  $size = ‪MathUtility::forceIntegerInRange(count($selectItems) + 1, $size, $autoSizeMax);
117  }
118  $selectId = ‪StringUtility::getUniqueId($size === 1 ? 'tceforms-select' : 'tceforms-multiselect');
119  $selectElement = $this->‪renderSelectElement($optionElements, $parameterArray, $config, $selectId, $size);
120 
121  $fieldInformationResult = $this->‪renderFieldInformation();
122  $fieldInformationHtml = $fieldInformationResult['html'];
123  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
124 
125  $fieldControlResult = $this->‪renderFieldControl();
126  $fieldControlHtml = $fieldControlResult['html'];
127  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
128 
129  $fieldWizardResult = $this->‪renderFieldWizard();
130  $fieldWizardHtml = $fieldWizardResult['html'];
131  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
132 
133  $html = [];
134  $html[] = $this->‪renderLabel($selectId);
135  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
136  $html[] = $fieldInformationHtml;
137  $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
138  $html[] = '<div class="form-wizards-wrap form-wizards-aside">';
139  $html[] = '<div class="form-wizards-element">';
140  if (!$disabled) {
141  // Add an empty hidden field which will send a blank value if all items are unselected.
142  $html[] = '<input type="hidden" name="' . htmlspecialchars($parameterArray['itemFormElName']) . '" value="">';
143  }
144  $html[] = $selectElement;
145  $html[] = '</div>';
146  if (!$disabled) {
147  if (!empty($fieldControlHtml)) {
148  $html[] = '<div class="form-wizards-items-aside form-wizards-items-aside--field-control">';
149  $html[] = $fieldControlHtml;
150  $html[] = '</div>';
151  }
152  $html[] = '</div>'; // Close form-wizards-aside
153  $html[] = '<p>';
154  $html[] = '<em>' . htmlspecialchars($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.holdDownCTRL')) . '</em>';
155  $html[] = '</p>';
156  if (!empty($fieldWizardHtml)) {
157  $html[] = '<div class="form-wizards-items-bottom">';
158  $html[] = $fieldWizardHtml;
159  $html[] = '</div>';
160  }
161  } else {
162  $html[] = '</div>'; // Close form-wizards-aside
163  }
164  $html[] = '</div>';
165  $html[] = '</div>';
166 
167  $resultArray['html'] = implode(LF, $html);
168  return $resultArray;
169  }
170 
174  protected function ‪renderSelectElement(array $optionElements, array $parameterArray, array $config, string $selectId, int $size): string
175  {
176  $attributes = array_merge(
177  [
178  'name' => $parameterArray['itemFormElName'] . '[]',
179  'multiple' => 'multiple',
180  'id' => $selectId,
181  'class' => 'form-select ',
182  'data-formengine-validation-rules' => $this->‪getValidationDataAsJsonString($config),
183  ],
184  $this->getOnFieldChangeAttrs('change', $parameterArray['fieldChangeFunc'] ?? [])
185  );
186  if ($size) {
187  $attributes['size'] = (string)$size;
188  }
189  if ($config['readOnly'] ?? false) {
190  $attributes['disabled'] = 'disabled';
191  }
192 
193  $html = [];
194  $html[] = '<select ' . GeneralUtility::implodeAttributes($attributes, true) . '>';
195  $html[] = implode(LF, $optionElements);
196  $html[] = '</select>';
197 
198  return implode(LF, $html);
199  }
200 
209  protected function ‪renderOptionElement($value, $label, array $attributes = [])
210  {
211  $attributes['value'] = $value;
212  $html = [
213  '<option ' . GeneralUtility::implodeAttributes($attributes, true, true) . '>',
214  htmlspecialchars($this->‪appendValueToLabelInDebugMode($label, $value), ENT_COMPAT, 'UTF-8', false),
215  '</option>',
216 
217  ];
218 
219  return implode('', $html);
220  }
221 }
‪TYPO3\CMS\Backend\Form\Element\SelectSingleBoxElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: SelectSingleBoxElement.php:53
‪TYPO3\CMS\Backend\Form\Element\SelectSingleBoxElement\renderSelectElement
‪renderSelectElement(array $optionElements, array $parameterArray, array $config, string $selectId, int $size)
Definition: SelectSingleBoxElement.php:171
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:73
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:104
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:37
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldControl
‪array renderFieldControl()
Definition: AbstractFormElement.php:89
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\getLanguageService
‪getLanguageService()
Definition: AbstractFormElement.php:456
‪TYPO3\CMS\Backend\Form\Element\SelectSingleBoxElement
Definition: SelectSingleBoxElement.php:28
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\formMaxWidth
‪int formMaxWidth($size=48)
Definition: AbstractFormElement.php:332
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\appendValueToLabelInDebugMode
‪appendValueToLabelInDebugMode(string|int $label, string|int $value)
Definition: AbstractFormElement.php:447
‪TYPO3\CMS\Backend\Form\Element\SelectSingleBoxElement\render
‪array render()
Definition: SelectSingleBoxElement.php:76
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:133
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderLabel
‪renderLabel(string $for)
Definition: AbstractFormElement.php:119
‪TYPO3\CMS\Backend\Form\Element\SelectSingleBoxElement\$defaultFieldControl
‪array $defaultFieldControl
Definition: SelectSingleBoxElement.php:43
‪TYPO3\CMS\Backend\Form\Element\SelectSingleBoxElement\renderOptionElement
‪string renderOptionElement($value, $label, array $attributes=[])
Definition: SelectSingleBoxElement.php:206
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange(mixed $theInt, int $min, int $max=2000000000, int $defaultValue=0)
Definition: MathUtility.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:105
‪TYPO3\CMS\Backend\Form\Element\SelectSingleBoxElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: SelectSingleBoxElement.php:33
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪initializeResultArray()
Definition: AbstractNode.php:77