‪TYPO3CMS  10.4
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()
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 values in an 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 $i => $item) {
96  $value = $item[1];
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[0], $attributes);
109  }
110 
111  $selectElement = $this->‪renderSelectElement($optionElements, $parameterArray, $config);
112 
113  $fieldInformationResult = $this->‪renderFieldInformation();
114  $fieldInformationHtml = $fieldInformationResult['html'];
115  $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  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
124 
125  $html = [];
126  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
127  $html[] = $fieldInformationHtml;
128  $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
129  $html[] = '<div class="form-wizards-wrap form-wizards-aside">';
130  $html[] = '<div class="form-wizards-element">';
131  if (!$disabled) {
132  // Add an empty hidden field which will send a blank value if all items are unselected.
133  $html[] = '<input type="hidden" name="' . htmlspecialchars($parameterArray['itemFormElName']) . '" value="">';
134  }
135  $html[] = $selectElement;
136  $html[] = '</div>';
137  if (!$disabled) {
138  if (!empty($fieldControlHtml)) {
139  $html[] = '<div class="form-wizards-items-aside">';
140  $html[] = $fieldControlHtml;
141  $html[] = '</div>';
142  }
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  if (!empty($fieldWizardHtml)) {
148  $html[] = '<div class="form-wizards-items-bottom">';
149  $html[] = $fieldWizardHtml;
150  $html[] = '</div>';
151  }
152  } else {
153  $html[] = '</div>'; // Close form-wizards-aside
154  }
155  $html[] = '</div>';
156  $html[] = '</div>';
157 
158  $resultArray['html'] = implode(LF, $html);
159  return $resultArray;
160  }
161 
170  protected function ‪renderSelectElement(array $optionElements, array $parameterArray, array $config)
171  {
172  $selectItems = $parameterArray['fieldConf']['config']['items'];
173  $size = (int)$config['size'];
174  $cssPrefix = $size === 1 ? 'tceforms-select' : 'tceforms-multiselect';
175 
176  if ($config['autoSizeMax']) {
178  count($selectItems) + 1,
180  $config['autoSizeMax']
181  );
182  }
183 
184  $attributes = [
185  'name' => $parameterArray['itemFormElName'] . '[]',
186  'multiple' => 'multiple',
187  'onchange' => implode('', $parameterArray['fieldChangeFunc']),
188  'id' => ‪StringUtility::getUniqueId($cssPrefix),
189  'class' => 'form-control ' . $cssPrefix,
190  'data-formengine-validation-rules' => $this->‪getValidationDataAsJsonString($config),
191  ];
192  if ($size) {
193  $attributes['size'] = (string)$size;
194  }
195  if ($config['readOnly']) {
196  $attributes['disabled'] = 'disabled';
197  }
198 
199  $html = [];
200  $html[] = '<select ' . GeneralUtility::implodeAttributes($attributes, true) . '>';
201  $html[] = implode(LF, $optionElements);
202  $html[] = '</select>';
203 
204  return implode(LF, $html);
205  }
206 
215  protected function ‪renderOptionElement($value, $label, array $attributes = [])
216  {
217  $attributes['value'] = $value;
218  $html = [
219  '<option ' . GeneralUtility::implodeAttributes($attributes, true) . '>',
220  htmlspecialchars($this->‪appendValueToLabelInDebugMode($label, $value), ENT_COMPAT, 'UTF-8', false),
221  '</option>'
222 
223  ];
224 
225  return implode('', $html);
226  }
227 }
‪TYPO3\CMS\Backend\Form\Element\SelectSingleBoxElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: SelectSingleBoxElement.php:53
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:72
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:116
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪array initializeResultArray()
Definition: AbstractNode.php:90
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:32
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:32
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Backend\Form\Element\SelectSingleBoxElement\renderSelectElement
‪string renderSelectElement(array $optionElements, array $parameterArray, array $config)
Definition: SelectSingleBoxElement.php:167
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪string getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:151
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldControl
‪array renderFieldControl()
Definition: AbstractFormElement.php:88
‪TYPO3\CMS\Backend\Form\Element\SelectSingleBoxElement
Definition: SelectSingleBoxElement.php:28
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\formMaxWidth
‪int formMaxWidth($size=48)
Definition: AbstractFormElement.php:298
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\appendValueToLabelInDebugMode
‪string appendValueToLabelInDebugMode($label, $value)
Definition: AbstractFormElement.php:378
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Backend\Form\Element\SelectSingleBoxElement\render
‪array render()
Definition: SelectSingleBoxElement.php:76
‪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:212
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\getLanguageService
‪LanguageService getLanguageService()
Definition: AbstractFormElement.php:390
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:104
‪TYPO3\CMS\Backend\Form\Element\SelectSingleBoxElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: SelectSingleBoxElement.php:33