‪TYPO3CMS  9.5
SelectSingleElement.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 
21 
29 {
35  protected ‪$defaultFieldInformation = [
36  'tcaDescription' => [
37  'renderType' => 'tcaDescription',
38  ],
39  ];
40 
46  protected ‪$defaultFieldWizard = [
47  'selectIcons' => [
48  'renderType' => 'selectIcons',
49  'disabled' => true,
50  ],
51  'localizationStateSelector' => [
52  'renderType' => 'localizationStateSelector',
53  'after' => [
54  'selectIcons',
55  ],
56  ],
57  'otherLanguageContent' => [
58  'renderType' => 'otherLanguageContent',
59  'after' => [ 'localizationStateSelector' ],
60  ],
61  'defaultLanguageDifferences' => [
62  'renderType' => 'defaultLanguageDifferences',
63  'after' => [ 'otherLanguageContent' ],
64  ],
65  ];
66 
72  public function ‪render()
73  {
74  $resultArray = $this->‪initializeResultArray();
75 
76  $table = $this->data['tableName'];
77  $field = $this->data['fieldName'];
78  $row = $this->data['databaseRow'];
79  $parameterArray = $this->data['parameterArray'];
80  $config = $parameterArray['fieldConf']['config'];
81 
82  $selectItems = $parameterArray['fieldConf']['config']['items'];
83 
84  // Check against inline uniqueness
86  $inlineStackProcessor = GeneralUtility::makeInstance(InlineStackProcessor::class);
87  $inlineStackProcessor->initializeByGivenStructure($this->data['inlineStructure']);
88  $uniqueIds = null;
89  if ($this->data['isInlineChild'] && $this->data['inlineParentUid']) {
90  // @todo: At least parts of this if is dead and/or broken: $uniqueIds is filled but never used.
91  // See InlineControlContainer where 'inlineData' 'unique' 'used' is set. What exactly is
92  // this if supposed to do and when should it kick in and what for?
93  $inlineObjectName = $inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($this->data['inlineFirstPid']);
94  $inlineFormName = $inlineStackProcessor->getCurrentStructureFormPrefix();
95  if ($this->data['inlineParentConfig']['foreign_table'] === $table
96  && $this->data['inlineParentConfig']['foreign_unique'] === $field
97  ) {
98  $uniqueIds = $this->data['inlineData']['unique'][$inlineObjectName . '-' . $table]['used'];
99  $parameterArray['fieldChangeFunc']['inlineUnique'] = 'inline.updateUnique(this,'
100  . GeneralUtility::quoteJSvalue($inlineObjectName . '-' . $table) . ','
101  . GeneralUtility::quoteJSvalue($inlineFormName) . ','
102  . GeneralUtility::quoteJSvalue($row['uid']) . ');';
103  }
104  // hide uid of parent record for symmetric relations
105  if ($this->data['inlineParentConfig']['foreign_table'] === $table
106  && (
107  $this->data['inlineParentConfig']['foreign_field'] === $field
108  || $this->data['inlineParentConfig']['symmetric_field'] === $field
109  )
110  ) {
111  $uniqueIds[] = $this->data['inlineParentUid'];
112  }
113  }
114 
115  // Initialization:
116  $selectId = ‪StringUtility::getUniqueId('tceforms-select-');
117  $selectedIcon = '';
118  $size = (int)$config['size'];
119 
120  // Style set on <select/>
121  $options = '';
122  $disabled = false;
123  if (!empty($config['readOnly'])) {
124  $disabled = true;
125  }
126 
127  // Prepare groups
128  $selectItemCounter = 0;
129  $selectItemGroupCount = 0;
130  $selectItemGroups = [];
131  $selectedValue = '';
132  $hasIcons = false;
133 
134  // In case e.g. "l10n_display" is set to "defaultAsReadonly" only one value (as string) could be handed in
135  if (!empty($parameterArray['itemFormElValue'])) {
136  if (is_array($parameterArray['itemFormElValue'])) {
137  $selectedValue = (string)$parameterArray['itemFormElValue'][0];
138  } else {
139  $selectedValue = (string)$parameterArray['itemFormElValue'];
140  }
141  }
142 
143  foreach ($selectItems as $item) {
144  if ($item[1] === '--div--') {
145  // IS OPTGROUP
146  if ($selectItemCounter !== 0) {
147  $selectItemGroupCount++;
148  }
149  $selectItemGroups[$selectItemGroupCount]['header'] = [
150  'title' => $item[0],
151  ];
152  } else {
153  // IS ITEM
154  $icon = !empty($item[2]) ? ‪FormEngineUtility::getIconHtml($item[2], $item[0], $item[0]) : '';
155  $selected = $selectedValue === (string)$item[1];
156 
157  if ($selected) {
158  $selectedIcon = $icon;
159  }
160 
161  $selectItemGroups[$selectItemGroupCount]['items'][] = [
162  'title' => $this->‪appendValueToLabelInDebugMode($item[0], $item[1]),
163  'value' => $item[1],
164  'icon' => $icon,
165  'selected' => $selected,
166  ];
167  $selectItemCounter++;
168  }
169  }
170 
171  // Fallback icon
172  // @todo: assign a special icon for non matching values?
173  if (!$selectedIcon && $selectItemGroups[0]['items'][0]['icon']) {
174  $selectedIcon = $selectItemGroups[0]['items'][0]['icon'];
175  }
176 
177  // Process groups
178  foreach ($selectItemGroups as $selectItemGroup) {
179  // suppress groups without items
180  if (empty($selectItemGroup['items'])) {
181  continue;
182  }
183 
184  $optionGroup = is_array($selectItemGroup['header']);
185  $options .= ($optionGroup ? '<optgroup label="' . htmlspecialchars($selectItemGroup['header']['title'], ENT_COMPAT, 'UTF-8', false) . '">' : '');
186 
187  if (is_array($selectItemGroup['items'])) {
188  foreach ($selectItemGroup['items'] as $item) {
189  $options .= '<option value="' . htmlspecialchars($item['value']) . '" data-icon="' .
190  htmlspecialchars($item['icon']) . '"'
191  . ($item['selected'] ? ' selected="selected"' : '') . '>' . htmlspecialchars($item['title'], ENT_COMPAT, 'UTF-8', false) . '</option>';
192  }
193  $hasIcons = !empty($item['icon']);
194  }
195 
196  $options .= ($optionGroup ? '</optgroup>' : '');
197  }
198 
199  $selectAttributes = [
200  'id' => $selectId,
201  'name' => $parameterArray['itemFormElName'],
202  'data-formengine-validation-rules' => $this->‪getValidationDataAsJsonString($config),
203  'class' => 'form-control form-control-adapt',
204  ];
205  if ($size) {
206  $selectAttributes['size'] = $size;
207  }
208  if ($disabled) {
209  $selectAttributes['disabled'] = 'disabled';
210  }
211 
212  $fieldInformationResult = $this->‪renderFieldInformation();
213  $fieldInformationHtml = $fieldInformationResult['html'];
214  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
215 
216  $fieldWizardResult = $this->‪renderFieldWizard();
217  $fieldWizardHtml = $fieldWizardResult['html'];
218  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
219 
220  $html = [];
221  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
222  $html[] = $fieldInformationHtml;
223  $html[] = '<div class="form-control-wrap">';
224  $html[] = '<div class="form-wizards-wrap">';
225  $html[] = '<div class="form-wizards-element">';
226  if ($hasIcons) {
227  $html[] = '<div class="input-group">';
228  $html[] = '<span class="input-group-addon input-group-icon">';
229  $html[] = $selectedIcon;
230  $html[] = '</span>';
231  }
232  $html[] = '<select ' . GeneralUtility::implodeAttributes($selectAttributes, true) . '>';
233  $html[] = $options;
234  $html[] = '</select>';
235  if ($hasIcons) {
236  $html[] = '</div>';
237  }
238  $html[] = '</div>';
239  if (!$disabled && !empty($fieldWizardHtml)) {
240  $html[] = '<div class="form-wizards-items-bottom">';
241  $html[] = $fieldWizardHtml;
242  $html[] = '</div>';
243  }
244  $html[] = '</div>';
245  $html[] = '</div>';
246  $html[] = '</div>';
247 
248  $resultArray['requireJsModules'][] = ['TYPO3/CMS/Backend/FormEngine/Element/SelectSingleElement' => implode(LF, [
249  'function(SelectSingleElement) {',
250  'require([\'jquery\'], function($) {',
251  '$(function() {',
252  'SelectSingleElement.initialize(',
253  GeneralUtility::quoteJSvalue('#' . $selectId) . ',',
254  '{',
255  'onChange: function() {',
256  implode('', $parameterArray['fieldChangeFunc']),
257  '}',
258  '}',
259  ');',
260  '});',
261  '});',
262  '}',
263  ])];
264 
265  $resultArray['html'] = implode(LF, $html);
266  return $resultArray;
267  }
268 }
‪TYPO3\CMS\Backend\Form\Element\SelectSingleElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: SelectSingleElement.php:44
‪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\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:31
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:2
‪TYPO3\CMS\Backend\Form\Utility\FormEngineUtility
Definition: FormEngineUtility.php:38
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪string getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:153
‪TYPO3\CMS\Backend\Form\Element\SelectSingleElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: SelectSingleElement.php:34
‪TYPO3\CMS\Backend\Form\Utility\FormEngineUtility\getIconHtml
‪static string getIconHtml($icon, $alt='', $title='')
Definition: FormEngineUtility.php:120
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:91
‪TYPO3\CMS\Backend\Form\InlineStackProcessor
Definition: InlineStackProcessor.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Form\Element\SelectSingleElement
Definition: SelectSingleElement.php:29
‪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\SelectSingleElement\render
‪array render()
Definition: SelectSingleElement.php:70