‪TYPO3CMS  11.5
InputColorPickerElement.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 
23 
28 {
34  protected ‪$defaultFieldInformation = [
35  'tcaDescription' => [
36  'renderType' => 'tcaDescription',
37  ],
38  ];
39 
45  protected ‪$defaultFieldWizard = [
46  'localizationStateSelector' => [
47  'renderType' => 'localizationStateSelector',
48  ],
49  'otherLanguageContent' => [
50  'renderType' => 'otherLanguageContent',
51  'after' => [
52  'localizationStateSelector',
53  ],
54  ],
55  'defaultLanguageDifferences' => [
56  'renderType' => 'defaultLanguageDifferences',
57  'after' => [
58  'otherLanguageContent',
59  ],
60  ],
61  ];
62 
68  public function ‪render()
69  {
70  $languageService = $this->‪getLanguageService();
71 
72  $table = $this->data['tableName'];
73  $fieldName = $this->data['fieldName'];
74  $row = $this->data['databaseRow'];
75  $parameterArray = $this->data['parameterArray'];
76  $resultArray = $this->‪initializeResultArray();
77 
78  $itemValue = $parameterArray['itemFormElValue'];
79  $config = $parameterArray['fieldConf']['config'];
80  $size = ‪MathUtility::forceIntegerInRange(($config['size'] ?? false) ?: $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
81  $evalList = ‪GeneralUtility::trimExplode(',', $config['eval'] ?? '', true);
82  $width = $this->‪formMaxWidth($size);
83  $nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $row['uid'] . '][' . $fieldName . ']');
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[] = '<div class="formengine-field-item t3js-formengine-field-item">';
92  $html[] = $fieldInformationHtml;
93  $html[] = '<div class="form-wizards-wrap">';
94  $html[] = '<div class="form-wizards-element">';
95  $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
96  $html[] = '<input class="form-control" value="' . htmlspecialchars($itemValue) . '" type="text" disabled>';
97  $html[] = '</div>';
98  $html[] = '</div>';
99  $html[] = '</div>';
100  $html[] = '</div>';
101  $resultArray['html'] = implode(LF, $html);
102  return $resultArray;
103  }
104 
105  // @todo: The whole eval handling is a mess and needs refactoring
106  foreach ($evalList as $func) {
107  // @todo: This is ugly: The code should find out on it's own whether an eval definition is a
108  // @todo: keyword like "date", or a class reference. The global registration could be dropped then
109  // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
110  if (isset(‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
111  if (class_exists($func)) {
112  $evalObj = GeneralUtility::makeInstance($func);
113  if (method_exists($evalObj, 'deevaluateFieldValue')) {
114  $_params = [
115  'value' => $itemValue,
116  ];
117  $itemValue = $evalObj->deevaluateFieldValue($_params);
118  }
119  $resultArray = $this->‪resolveJavaScriptEvaluation($resultArray, $func, $evalObj);
120  }
121  }
122  }
123 
124  // Load needed js library
125  $resultArray['requireJsModules'][] = ‪JavaScriptModuleInstruction::forRequireJS(
126  'TYPO3/CMS/Backend/ColorPicker'
127  )->invoke('initialize');
128 
129  $attributes = [
130  'value' => $itemValue,
131  'id' => ‪StringUtility::getUniqueId('formengine-input-'),
132  'class' => implode(' ', [
133  'form-control',
134  'hasDefaultValue',
135  't3js-clearable',
136  't3js-color-picker',
137  'formengine-colorpickerelement',
138  ]),
139  'data-formengine-validation-rules' => $this->‪getValidationDataAsJsonString($config),
140  'data-formengine-input-params' => (string)json_encode([
141  'field' => $parameterArray['itemFormElName'],
142  'evalList' => implode(',', $evalList),
143  'is_in' => trim($config['is_in'] ?? ''),
144  ]),
145  'data-formengine-input-name' => $parameterArray['itemFormElName'],
146  ];
147 
148  if (isset($config['max']) && (int)$config['max'] > 0) {
149  $attributes['maxlength'] = (string)(int)$config['max'];
150  }
151  if (!empty($config['placeholder'])) {
152  $attributes['placeholder'] = trim($config['placeholder']);
153  }
154  if (isset($config['autocomplete'])) {
155  $attributes['autocomplete'] = empty($config['autocomplete']) ? 'new-' . $fieldName : 'on';
156  }
157 
158  $valuePickerHtml = [];
159  if (isset($config['valuePicker']['items']) && is_array($config['valuePicker']['items'])) {
160  $valuePickerHtml[] = '<select class="t3js-colorpicker-value-trigger form-select form-control-adapt">';
161  $valuePickerHtml[] = '<option></option>';
162  foreach ($config['valuePicker']['items'] as $item) {
163  $valuePickerHtml[] = '<option value="' . htmlspecialchars($item[1]) . '">' . htmlspecialchars($languageService->sL($item[0])) . '</option>';
164  }
165  $valuePickerHtml[] = '</select>';
166  }
167 
168  $fieldWizardResult = $this->‪renderFieldWizard();
169  $fieldWizardHtml = $fieldWizardResult['html'];
170  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
171 
172  $fieldControlResult = $this->‪renderFieldControl();
173  $fieldControlHtml = $fieldControlResult['html'];
174  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
175 
176  $mainFieldHtml = [];
177  $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
178  $mainFieldHtml[] = '<div class="form-wizards-wrap">';
179  $mainFieldHtml[] = '<div class="form-wizards-element">';
180  $mainFieldHtml[] = '<input type="text" ' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
181  $mainFieldHtml[] = '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($itemValue) . '" />';
182  $mainFieldHtml[] = '</div>';
183  $mainFieldHtml[] = '<div class="form-wizards-items-aside form-wizards-items-aside--field-control">';
184  $mainFieldHtml[] = '<div class="btn-group">';
185  $mainFieldHtml[] = $fieldControlHtml;
186  $mainFieldHtml[] = implode(LF, $valuePickerHtml);
187  $mainFieldHtml[] = '</div>';
188  $mainFieldHtml[] = '</div>';
189  if (!empty($fieldWizardHtml)) {
190  $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
191  $mainFieldHtml[] = $fieldWizardHtml;
192  $mainFieldHtml[] = '</div>';
193  }
194  $mainFieldHtml[] = '</div>';
195  $mainFieldHtml[] = '</div>';
196  $mainFieldHtml = implode(LF, $mainFieldHtml);
197 
198  $fullElement = $mainFieldHtml;
199  if ($this->‪hasNullCheckboxButNoPlaceholder()) {
200  $checked = $itemValue !== null ? ' checked="checked"' : '';
201  $fullElement = [];
202  $fullElement[] = '<div class="t3-form-field-disable"></div>';
203  $fullElement[] = '<div class="form-check t3-form-field-eval-null-checkbox">';
204  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
205  $fullElement[] = '<input type="checkbox" class="form-check-input" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
206  $fullElement[] = '<label class="form-check-label" for="' . $nullControlNameEscaped . '">';
207  $fullElement[] = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
208  $fullElement[] = '</label>';
209  $fullElement[] = '</div>';
210  $fullElement[] = $mainFieldHtml;
211  $fullElement = implode(LF, $fullElement);
212  } elseif ($this->‪hasNullCheckboxWithPlaceholder()) {
213  $checked = $itemValue !== null ? ' checked="checked"' : '';
214  $placeholder = $shortenedPlaceholder = $config['placeholder'] ?? '';
215  $disabled = '';
216  $fallbackValue = 0;
217  if (strlen($placeholder) > 0) {
218  $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
219  if ($placeholder !== $shortenedPlaceholder) {
220  $overrideLabel = sprintf(
221  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
222  '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
223  );
224  } else {
225  $overrideLabel = sprintf(
226  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
227  htmlspecialchars($placeholder)
228  );
229  }
230  } else {
231  $overrideLabel = $languageService->sL(
232  'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
233  );
234  }
235  $fullElement = [];
236  $fullElement[] = '<div class="form-check t3js-form-field-eval-null-placeholder-checkbox">';
237  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="' . $fallbackValue . '" />';
238  $fullElement[] = '<input type="checkbox" class="form-check-input" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . $disabled . ' />';
239  $fullElement[] = '<label class="form-check-label" for="' . $nullControlNameEscaped . '">';
240  $fullElement[] = $overrideLabel;
241  $fullElement[] = '</label>';
242  $fullElement[] = '</div>';
243  $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
244  $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
245  $fullElement[] = '<input type="text" class="form-control" disabled="disabled" value="' . htmlspecialchars($shortenedPlaceholder) . '" />';
246  $fullElement[] = '</div>';
247  $fullElement[] = '</div>';
248  $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
249  $fullElement[] = $mainFieldHtml;
250  $fullElement[] = '</div>';
251  $fullElement = implode(LF, $fullElement);
252  }
253 
254  $resultArray['html'] = '<div class="formengine-field-item t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
255  return $resultArray;
256  }
257 
261  protected function ‪getLanguageService()
262  {
263  return ‪$GLOBALS['LANG'];
264  }
265 }
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:999
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:76
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:120
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪array initializeResultArray()
Definition: AbstractNode.php:91
‪TYPO3\CMS\Backend\Form\Element\InputColorPickerElement\render
‪array render()
Definition: InputColorPickerElement.php:66
‪TYPO3\CMS\Backend\Form\Element\InputColorPickerElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: InputColorPickerElement.php:43
‪TYPO3\CMS\Backend\Form\Element\InputColorPickerElement\getLanguageService
‪LanguageService getLanguageService()
Definition: InputColorPickerElement.php:259
‪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:35
‪TYPO3\CMS\Backend\Form\Element\InputColorPickerElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: InputColorPickerElement.php:33
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\forRequireJS
‪static self forRequireJS(string $name, string $exportName=null)
Definition: JavaScriptModuleInstruction.php:49
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\resolveJavaScriptEvaluation
‪array resolveJavaScriptEvaluation(array $resultArray, string $name, ?object $evalObject)
Definition: AbstractFormElement.php:325
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪string getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:156
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldControl
‪array renderFieldControl()
Definition: AbstractFormElement.php:92
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\hasNullCheckboxWithPlaceholder
‪bool hasNullCheckboxWithPlaceholder()
Definition: AbstractFormElement.php:171
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\hasNullCheckboxButNoPlaceholder
‪bool hasNullCheckboxButNoPlaceholder()
Definition: AbstractFormElement.php:136
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\formMaxWidth
‪int formMaxWidth($size=48)
Definition: AbstractFormElement.php:304
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:128
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:108
‪TYPO3\CMS\Backend\Form\Element\InputColorPickerElement
Definition: InputColorPickerElement.php:28