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