‪TYPO3CMS  ‪main
ColorElement.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 
29 {
35  protected ‪$defaultFieldInformation = [
36  'tcaDescription' => [
37  'renderType' => 'tcaDescription',
38  ],
39  ];
40 
46  protected ‪$defaultFieldWizard = [
47  'localizationStateSelector' => [
48  'renderType' => 'localizationStateSelector',
49  ],
50  'otherLanguageContent' => [
51  'renderType' => 'otherLanguageContent',
52  'after' => [
53  'localizationStateSelector',
54  ],
55  ],
56  'defaultLanguageDifferences' => [
57  'renderType' => 'defaultLanguageDifferences',
58  'after' => [
59  'otherLanguageContent',
60  ],
61  ],
62  ];
63 
69  public function ‪render(): array
70  {
71  $table = $this->data['tableName'];
72  $fieldName = $this->data['fieldName'];
73  $parameterArray = $this->data['parameterArray'];
74  $resultArray = $this->‪initializeResultArray();
75  $config = $parameterArray['fieldConf']['config'];
76  $tsConfig = $this->data['pageTsConfig'];
77 
78  $itemValue = $parameterArray['itemFormElValue'];
79  $width = $this->‪formMaxWidth(
80  ‪MathUtility::forceIntegerInRange($config['size'] ?? $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth)
81  );
82  $fieldId = ‪StringUtility::getUniqueId('formengine-input-');
83  $renderedLabel = $this->‪renderLabel($fieldId);
84 
85  $fieldInformationResult = $this->‪renderFieldInformation();
86  $fieldInformationHtml = $fieldInformationResult['html'];
87  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
88 
89  if ($config['readOnly'] ?? false) {
90  $html = [];
91  $html[] = $renderedLabel;
92  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
93  $html[] = $fieldInformationHtml;
94  $html[] = '<div class="form-wizards-wrap">';
95  $html[] = '<div class="form-wizards-element">';
96  $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
97  $html[] = '<input class="form-control" id="' . htmlspecialchars($fieldId) . '" value="' . htmlspecialchars((string)$itemValue) . '" type="text" disabled>';
98  $html[] = '</div>';
99  $html[] = '</div>';
100  $html[] = '</div>';
101  $html[] = '</div>';
102  $resultArray['html'] = implode(LF, $html);
103  return $resultArray;
104  }
105 
106  $languageService = $this->‪getLanguageService();
107  $itemName = (string)$parameterArray['itemFormElName'];
108 
109  // Always add "trim".
110  $evalList = ['trim'];
111  if ($config['nullable'] ?? false) {
112  $evalList[] = 'null';
113  }
114 
115  $attributes = [
116  'value' => '',
117  'id' => $fieldId,
118  'class' => implode(' ', [
119  'form-control',
120  't3js-color-picker',
121  'hasDefaultValue',
122  ]),
123  'maxlength' => '7', // #XXXXXX (/#[0-9a-fA-F]{3,6}/)
124  'data-formengine-validation-rules' => $this->‪getValidationDataAsJsonString($config),
125  'data-formengine-input-params' => (string)json_encode([
126  'field' => $itemName,
127  'evalList' => implode(',', $evalList),
128  ], JSON_THROW_ON_ERROR),
129  'data-formengine-input-name' => $itemName,
130  ];
131 
132  if (!empty($config['placeholder'])) {
133  $attributes['placeholder'] = trim($config['placeholder']);
134  }
135 
136  $valuePickerHtml = [];
137  if (is_array($config['valuePicker']['items'] ?? false)) {
138  $valuePickerConfiguration = [
139  'mode' => 'replace',
140  'linked-field' => '[data-formengine-input-name="' . $itemName . '"]',
141  ];
142  $valuePickerHtml[] = '<typo3-formengine-valuepicker ' . GeneralUtility::implodeAttributes($valuePickerConfiguration, true) . '>';
143  $valuePickerHtml[] = '<select class="form-select form-control-adapt">';
144  $valuePickerHtml[] = '<option></option>';
145  foreach ($config['valuePicker']['items'] as $item) {
146  $valuePickerHtml[] = '<option value="' . htmlspecialchars($item[1]) . '">' . htmlspecialchars($languageService->sL($item[0])) . '</option>';
147  }
148  $valuePickerHtml[] = '</select>';
149  $valuePickerHtml[] = '</typo3-formengine-valuepicker>';
150 
151  $resultArray['javaScriptModules'][] = ‪JavaScriptModuleInstruction::create('@typo3/backend/form-engine/field-wizard/value-picker.js');
152  }
153 
154  $fieldWizardResult = $this->‪renderFieldWizard();
155  $fieldWizardHtml = $fieldWizardResult['html'];
156  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
157 
158  $fieldControlResult = $this->‪renderFieldControl();
159  $fieldControlHtml = $fieldControlResult['html'];
160  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
161 
162  $mainFieldHtml = [];
163  $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
164  $mainFieldHtml[] = '<div class="form-wizards-wrap">';
165  $mainFieldHtml[] = '<div class="form-wizards-element">';
166  $mainFieldHtml[] = '<input type="text" ' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
167  $mainFieldHtml[] = '<input type="hidden" name="' . $itemName . '" value="' . htmlspecialchars((string)$itemValue) . '" />';
168  $mainFieldHtml[] = '</div>';
169  $mainFieldHtml[] = '<div class="form-wizards-items-aside form-wizards-items-aside--field-control">';
170  $mainFieldHtml[] = '<div class="btn-group">';
171  $mainFieldHtml[] = $fieldControlHtml;
172  $mainFieldHtml[] = implode(LF, $valuePickerHtml);
173  $mainFieldHtml[] = '</div>';
174  $mainFieldHtml[] = '</div>';
175  if (!empty($fieldWizardHtml)) {
176  $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
177  $mainFieldHtml[] = $fieldWizardHtml;
178  $mainFieldHtml[] = '</div>';
179  }
180  $mainFieldHtml[] = '</div>';
181  $mainFieldHtml[] = '</div>';
182  $mainFieldHtml = implode(LF, $mainFieldHtml);
183 
184  $nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $this->data['databaseRow']['uid'] . '][' . $fieldName . ']');
185 
186  $fullElement = $mainFieldHtml;
187  if ($this->‪hasNullCheckboxButNoPlaceholder()) {
188  $checked = $itemValue !== null ? ' checked="checked"' : '';
189  $fullElement = [];
190  $fullElement[] = '<div class="t3-form-field-disable"></div>';
191  $fullElement[] = '<div class="form-check t3-form-field-eval-null-checkbox">';
192  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
193  $fullElement[] = '<input type="checkbox" class="form-check-input" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
194  $fullElement[] = '<label class="form-check-label" for="' . $nullControlNameEscaped . '">';
195  $fullElement[] = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
196  $fullElement[] = '</label>';
197  $fullElement[] = '</div>';
198  $fullElement[] = $mainFieldHtml;
199  $fullElement = implode(LF, $fullElement);
200  } elseif ($this->‪hasNullCheckboxWithPlaceholder()) {
201  $checked = $itemValue !== null ? ' checked="checked"' : '';
202  $placeholder = $shortenedPlaceholder = (string)($config['placeholder'] ?? '');
203  if ($placeholder !== '') {
204  $shortenedPlaceholder = ‪GeneralUtility::fixed_lgd_cs($placeholder, 20);
205  if ($placeholder !== $shortenedPlaceholder) {
206  $overrideLabel = sprintf(
207  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
208  '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
209  );
210  } else {
211  $overrideLabel = sprintf(
212  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
213  htmlspecialchars($placeholder)
214  );
215  }
216  } else {
217  $overrideLabel = $languageService->sL(
218  'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
219  );
220  }
221  $fullElement = [];
222  $fullElement[] = '<div class="form-check t3js-form-field-eval-null-placeholder-checkbox">';
223  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
224  $fullElement[] = '<input type="checkbox" class="form-check-input" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
225  $fullElement[] = '<label class="form-check-label" for="' . $nullControlNameEscaped . '">';
226  $fullElement[] = $overrideLabel;
227  $fullElement[] = '</label>';
228  $fullElement[] = '</div>';
229  $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
230  $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
231  $fullElement[] = '<input type="text" class="form-control" disabled="disabled" value="' . htmlspecialchars($shortenedPlaceholder) . '" />';
232  $fullElement[] = '</div>';
233  $fullElement[] = '</div>';
234  $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
235  $fullElement[] = $mainFieldHtml;
236  $fullElement[] = '</div>';
237  $fullElement = implode(LF, $fullElement);
238  }
239 
240  $configuredPalette =
241  $tsConfig['TCEFORM.'][$table . '.'][$fieldName . '.']['colorPalette']
242  ?? $tsConfig['TCEFORM.'][$table . '.']['colorPalette']
243  ?? $tsConfig['TCEFORM.']['colorPalette']
244  ?? null;
245  if ($configuredPalette === null) {
246  // No palette defined in TCEFORM, fall back to all colors
247  $colorDefinitions = array_map(static function (array $colorDefinition): string {
248  return $colorDefinition['value'] ?? '';
249  }, array_values($tsConfig['colorPalettes.']['colors.'] ?? []));
250  } else {
251  $colorsInPalette = ‪GeneralUtility::trimExplode(',', $tsConfig['colorPalettes.']['palettes.'][$configuredPalette] ?? '', true);
252  $colorDefinitions = array_map(static function (string $colorIdentifier) use ($tsConfig): string {
253  return $tsConfig['colorPalettes.']['colors.'][$colorIdentifier . '.']['value'] ?? '';
254  }, $colorsInPalette);
255  }
256  $attributes = [
257  'recordFieldId' => $fieldId,
258  'colorPalette' => implode(';', array_unique(array_filter($colorDefinitions))),
259  ];
260 
261  $resultArray['html'] = $renderedLabel . '
262  <typo3-formengine-element-color ' . GeneralUtility::implodeAttributes($attributes, true) . '>
263  <div class="formengine-field-item t3js-formengine-field-item">
264  ' . $fieldInformationHtml . $fullElement . '
265  </div>
266  </typo3-formengine-element-color>';
267 
268  $resultArray['javaScriptModules'][] = ‪JavaScriptModuleInstruction::create('@typo3/backend/form-engine/element/color-element.js');
269 
270  return $resultArray;
271  }
272 }
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:73
‪TYPO3\CMS\Core\Utility\GeneralUtility\fixed_lgd_cs
‪static string fixed_lgd_cs(string $string, int $chars, string $appendString='...')
Definition: GeneralUtility.php:92
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:104
‪TYPO3\CMS\Backend\Form\Element\ColorElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: ColorElement.php:34
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\create
‪static create(string $name, string $exportName=null)
Definition: JavaScriptModuleInstruction.php:47
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:37
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪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\AbstractFormElement\formMaxWidth
‪int formMaxWidth($size=48)
Definition: AbstractFormElement.php:332
‪TYPO3\CMS\Backend\Form\Element\ColorElement
Definition: ColorElement.php:29
‪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\ColorElement\render
‪array render()
Definition: ColorElement.php:67
‪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\AbstractFormElement\hasNullCheckboxWithPlaceholder
‪hasNullCheckboxWithPlaceholder()
Definition: AbstractFormElement.php:195
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:817
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\hasNullCheckboxButNoPlaceholder
‪hasNullCheckboxButNoPlaceholder()
Definition: AbstractFormElement.php:163
‪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
‪TYPO3\CMS\Backend\Form\Element\ColorElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: ColorElement.php:44