TYPO3 CMS  TYPO3_8-7
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 $defaultFieldControl = [
34  'resetSelection' => [
35  'renderType' => 'resetSelection',
36  ],
37  ];
38 
44  protected $defaultFieldWizard = [
45  'localizationStateSelector' => [
46  'renderType' => 'localizationStateSelector',
47  ],
48  'otherLanguageContent' => [
49  'renderType' => 'otherLanguageContent',
50  'after' => [
51  'localizationStateSelector'
52  ],
53  ],
54  'defaultLanguageDifferences' => [
55  'renderType' => 'defaultLanguageDifferences',
56  'after' => [
57  'otherLanguageContent',
58  ],
59  ],
60  ];
61 
67  public function render()
68  {
69  $languageService = $this->getLanguageService();
70  $resultArray = $this->initializeResultArray();
71 
72  $parameterArray = $this->data['parameterArray'];
73  // Field configuration from TCA:
74  $config = $parameterArray['fieldConf']['config'];
75  $selectItems = $parameterArray['fieldConf']['config']['items'];
76  $disabled = !empty($config['readOnly']);
77 
78  // Get values in an array (and make unique, which is fine because there can be no duplicates anyway):
79  $itemArray = array_flip($parameterArray['itemFormElValue']);
80  $width = $this->formMaxWidth($this->defaultInputWidth);
81 
82  $optionElements = [];
83  foreach ($selectItems as $i => $item) {
84  $value = $item[1];
85  $attributes = [];
86  // Selected or not by default
87  if (isset($itemArray[$value])) {
88  $attributes['selected'] = 'selected';
89  unset($itemArray[$value]);
90  }
91  // Non-selectable element
92  if ((string)$value === '--div--') {
93  $attributes['disabled'] = 'disabled';
94  $attributes['class'] = 'formcontrol-select-divider';
95  }
96  $optionElements[] = $this->renderOptionElement($value, $item[0], $attributes);
97  }
98 
99  $selectElement = $this->renderSelectElement($optionElements, $parameterArray, $config);
100 
101  $legacyWizards = $this->renderWizards();
102  $legacyFieldControlHtml = implode(LF, $legacyWizards['fieldControl']);
103  $legacyFieldWizardHtml = implode(LF, $legacyWizards['fieldWizard']);
104 
105  $fieldInformationResult = $this->renderFieldInformation();
106  $fieldInformationHtml = $fieldInformationResult['html'];
107  $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
108 
109  $fieldControlResult = $this->renderFieldControl();
110  $fieldControlHtml = $legacyFieldControlHtml . $fieldControlResult['html'];
111  $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
112 
113  $fieldWizardResult = $this->renderFieldWizard();
114  $fieldWizardHtml = $legacyFieldWizardHtml . $fieldWizardResult['html'];
115  $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
116 
117  $html = [];
118  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
119  if (!$disabled) {
120  $html[] = $fieldInformationHtml;
121  }
122  $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
123  $html[] = '<div class="form-wizards-wrap form-wizards-aside">';
124  $html[] = '<div class="form-wizards-element">';
125  if (!$disabled) {
126  // Add an empty hidden field which will send a blank value if all items are unselected.
127  $html[] = '<input type="hidden" name="' . htmlspecialchars($parameterArray['itemFormElName']) . '" value="">';
128  }
129  $html[] = $selectElement;
130  $html[] = '</div>';
131  if (!$disabled) {
132  $html[] = '<div class="form-wizards-items-aside">';
133  $html[] = $fieldControlHtml;
134  $html[] = '</div>';
135  $html[] = '</div>';
136 
137  $html[] = '<p>';
138  $html[] = '<em>' . htmlspecialchars($languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.holdDownCTRL')) . '</em>';
139  $html[] = '</p>';
140  $html[] = '<div class="form-wizards-items-bottom">';
141  $html[] = $fieldWizardHtml;
142  $html[] = '</div>';
143  }
144  $html[] = '</div>';
145  $html[] = '</div>';
146 
147  $resultArray['html'] = implode(LF, $html);
148  return $resultArray;
149  }
150 
159  protected function renderSelectElement(array $optionElements, array $parameterArray, array $config)
160  {
161  $selectItems = $parameterArray['fieldConf']['config']['items'];
162  $size = (int)$config['size'];
163  $cssPrefix = $size === 1 ? 'tceforms-select' : 'tceforms-multiselect';
164 
165  if ($config['autoSizeMax']) {
167  count($selectItems) + 1,
169  $config['autoSizeMax']
170  );
171  }
172 
173  $attributes = [
174  'name' => $parameterArray['itemFormElName'] . '[]',
175  'multiple' => 'multiple',
176  'onchange' => implode('', $parameterArray['fieldChangeFunc']),
177  'id' => StringUtility::getUniqueId($cssPrefix),
178  'class' => 'form-control ' . $cssPrefix,
179  'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
180  ];
181  if ($size) {
182  $attributes['size'] = $size;
183  }
184  if ($config['readOnly']) {
185  $attributes['disabled'] = 'disabled';
186  }
187  if (isset($config['itemListStyle'])) {
188  GeneralUtility::deprecationLog('TCA property itemListStyle is deprecated since TYPO3 v8 and will be removed in v9');
189  $attributes['style'] = $config['itemListStyle'];
190  }
191 
192  $html = [];
193  $html[] = '<select ' . GeneralUtility::implodeAttributes($attributes, true) . '>';
194  $html[] = implode(LF, $optionElements);
195  $html[] = '</select>';
196 
197  return implode(LF, $html);
198  }
199 
208  protected function renderOptionElement($value, $label, array $attributes = [])
209  {
210  $attributes['value'] = $value;
211  $html = [
212  '<option ' . GeneralUtility::implodeAttributes($attributes, true) . '>',
213  htmlspecialchars($label, ENT_COMPAT, 'UTF-8', false),
214  '</option>'
215 
216  ];
217 
218  return implode('', $html);
219  }
220 }
static implodeAttributes(array $arr, $xhtmlSafe=false, $dontOmitBlankAttribs=false)
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
renderWizards( $itemKinds=null, $wizConf=null, $table=null, $row=null, $fieldName=null, $PA=null, $itemName=null, $specConf=null, $RTE=null)
renderSelectElement(array $optionElements, array $parameterArray, array $config)
renderOptionElement($value, $label, array $attributes=[])
mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)