[!!!][FEATURE] FormEngine element level refactoring
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / InputColorPickerElement.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\Element;
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
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Core\Utility\MathUtility;
19 use TYPO3\CMS\Core\Utility\StringUtility;
20 use TYPO3\CMS\Lang\LanguageService;
21
22 /**
23 * Render an input field with a color picker
24 */
25 class InputColorPickerElement extends AbstractFormElement
26 {
27 /**
28 * Default field wizards enabled for this element.
29 *
30 * @var array
31 */
32 protected $defaultFieldWizard = [
33 'otherLanguageContent' => [
34 'renderType' => 'otherLanguageContent',
35 ],
36 'defaultLanguageDifferences' => [
37 'renderType' => 'defaultLanguageDifferences',
38 'after' => [
39 'otherLanguageContent',
40 ],
41 ],
42 ];
43
44 /**
45 * This will render a single-line input form field, possibly with various control/validation features
46 *
47 * @return array As defined in initializeResultArray() of AbstractNode
48 */
49 public function render()
50 {
51 $languageService = $this->getLanguageService();
52
53 $table = $this->data['tableName'];
54 $fieldName = $this->data['fieldName'];
55 $row = $this->data['databaseRow'];
56 $parameterArray = $this->data['parameterArray'];
57 $resultArray = $this->initializeResultArray();
58
59 $itemValue = $parameterArray['itemFormElValue'];
60 $config = $parameterArray['fieldConf']['config'];
61 $size = MathUtility::forceIntegerInRange($config['size'] ?: $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
62 $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
63 $width = (int)$this->formMaxWidth($size);
64 $nullControlNameAttribute = ' name="' . htmlspecialchars('control[active][' . $table . '][' . $row['uid'] . '][' . $fieldName . ']') . '"';
65
66 if ($config['readOnly']) {
67 $html = [];
68 $html[] = '<div class="t3js-formengine-field-item">';
69 $html[] = '<div class="form-wizards-wrap">';
70 $html[] = '<div class="form-wizards-element">';
71 $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
72 $html[] = '<input class="form-control" value="' . htmlspecialchars($itemValue) . '" type="text" disabled>';
73 $html[] = '</div>';
74 $html[] = '</div>';
75 $html[] = '</div>';
76 $html[] = '</div>';
77 $resultArray['html'] = implode(LF, $html);
78 return $resultArray;
79 }
80
81 // @todo: The whole eval handling is a mess and needs refactoring
82 foreach ($evalList as $func) {
83 // @todo: This is ugly: The code should find out on it's own whether a eval definition is a
84 // @todo: keyword like "date", or a class reference. The global registration could be dropped then
85 // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
86 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
87 if (class_exists($func)) {
88 $evalObj = GeneralUtility::makeInstance($func);
89 if (method_exists($evalObj, 'deevaluateFieldValue')) {
90 $_params = [
91 'value' => $itemValue
92 ];
93 $itemValue = $evalObj->deevaluateFieldValue($_params);
94 }
95 if (method_exists($evalObj, 'returnFieldJS')) {
96 $resultArray['additionalJavaScriptPost'][] = 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($evalData) . '] = function(value) {' . $evalObj->returnFieldJS() . '};';
97 }
98 }
99 }
100 }
101
102 // Load needed js library
103 $resultArray['requireJsModules'][] = [
104 'TYPO3/CMS/Backend/ColorPicker' => 'function(ColorPicker){ColorPicker.initialize()}'
105 ];
106
107 $attributes = [
108 'value' => $itemValue,
109 'id' => StringUtility::getUniqueId('formengine-input-'),
110 'class' => implode(' ', [
111 'form-control',
112 'hasDefaultValue',
113 't3js-clearable',
114 't3js-color-picker',
115 'formengine-colorpickerelement',
116 ]),
117 'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
118 'data-formengine-input-params' => json_encode([
119 'field' => $parameterArray['itemFormElName'],
120 'evalList' => implode(',', $evalList),
121 'is_in' => trim($config['is_in']),
122 ]),
123 'data-formengine-input-name' => $parameterArray['itemFormElName'],
124 ];
125
126 if (isset($config['max']) && (int)$config['max'] > 0) {
127 $attributes['maxlength'] = (int)$config['max'];
128 }
129 if (!empty($config['placeholder'])) {
130 $attributes['placeholder'] = trim($config['placeholder']);
131 }
132 if (isset($config['autocomplete'])) {
133 $attributes['autocomplete'] = empty($config['autocomplete']) ? 'new-' . $fieldName : 'on';
134 }
135
136 $legacyWizards = $this->renderWizards();
137 $legacyFieldControlHtml = implode(LF, $legacyWizards['fieldControl']);
138 $legacyFieldWizardHtml = implode(LF, $legacyWizards['fieldWizard']);
139
140 $fieldInformationResult = $this->renderFieldInformation();
141 $fieldInformationHtml = $fieldInformationResult['html'];
142 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
143
144 $fieldWizardResult = $this->renderFieldWizard();
145 $fieldWizardHtml = $legacyFieldWizardHtml . $fieldWizardResult['html'];
146 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
147
148 $fieldControlResult = $this->renderFieldControl();
149 $fieldControlHtml = $legacyFieldControlHtml . $fieldControlResult['html'];
150 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
151
152 $mainFieldHtml = [];
153 $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
154 $mainFieldHtml[] = '<div class="form-wizards-wrap">';
155 $mainFieldHtml[] = '<div class="form-wizards-element">';
156 $mainFieldHtml[] = '<input type="text"' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
157 $mainFieldHtml[] = '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($itemValue) . '" />';
158 $mainFieldHtml[] = '</div>';
159 $mainFieldHtml[] = '<div class="form-wizards-items-aside">';
160 $mainFieldHtml[] = '<div class="btn-group">';
161 $mainFieldHtml[] = $fieldControlHtml;
162 $mainFieldHtml[] = '</div>';
163 $mainFieldHtml[] = '</div>';
164 $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
165 $mainFieldHtml[] = $fieldWizardHtml;
166 $mainFieldHtml[] = '</div>';
167 $mainFieldHtml[] = '</div>';
168 $mainFieldHtml[] = '</div>';
169 $mainFieldHtml = implode(LF, $mainFieldHtml);
170
171 $fullElement = $mainFieldHtml;
172 if ($this->hasNullCheckboxButNoPlaceholder()) {
173 $checked = $itemValue !== null ? ' checked="checked"' : '';
174 $fullElement = [];
175 $fullElement[] = '<div class="t3-form-field-disable"></div>';
176 $fullElement[] = '<div class="checkbox t3-form-field-eval-null-checkbox">';
177 $fullElement[] = '<label>';
178 $fullElement[] = '<input type="hidden"' . $nullControlNameAttribute . ' value="0" />';
179 $fullElement[] = '<input type="checkbox"' . $nullControlNameAttribute . ' value="1"' . $checked . ' />';
180 $fullElement[] = $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
181 $fullElement[] = '</label>';
182 $fullElement[] = '</div>';
183 $fullElement[] = $mainFieldHtml;
184 $fullElement = implode(LF, $fullElement);
185 } elseif ($this->hasNullCheckboxWithPlaceholder()) {
186 $checked = $itemValue !== null ? ' checked="checked"' : '';
187 $placeholder = $shortenedPlaceholder = $config['placeholder'] ?? '';
188 $disabled = '';
189 $fallbackValue = 0;
190 if (strlen($placeholder) > 0) {
191 $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
192 if ($placeholder !== $shortenedPlaceholder) {
193 $overrideLabel = sprintf(
194 $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
195 '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
196 );
197 } else {
198 $overrideLabel = sprintf(
199 $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
200 htmlspecialchars($placeholder)
201 );
202 }
203 } else {
204 $fallbackValue = 1;
205 $checked = ' checked="checked"';
206 $disabled = ' disabled="disabled"';
207 $overrideLabel = $languageService->sL(
208 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
209 );
210 }
211 $fullElement = [];
212 $fullElement[] = '<div class="checkbox t3js-form-field-eval-null-placeholder-checkbox">';
213 $fullElement[] = '<label>';
214 $fullElement[] = '<input type="hidden"' . $nullControlNameAttribute . ' value="' . $fallbackValue . '" />';
215 $fullElement[] = '<input type="checkbox"' . $nullControlNameAttribute . ' value="1"' . $checked . $disabled . ' />';
216 $fullElement[] = $overrideLabel;
217 $fullElement[] = '</label>';
218 $fullElement[] = '</div>';
219 $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
220 $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
221 $fullElement[] = '<input type="text" class="form-control" disabled="disabled" value="' . $shortenedPlaceholder . '" />';
222 $fullElement[] = '</div>';
223 $fullElement[] = '</div>';
224 $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
225 $fullElement[] = $mainFieldHtml;
226 $fullElement[] = '</div>';
227 $fullElement = implode(LF, $fullElement);
228 }
229
230 $resultArray['html'] = '<div class="t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
231 return $resultArray;
232 }
233
234 /**
235 * @return LanguageService
236 */
237 protected function getLanguageService()
238 {
239 return $GLOBALS['LANG'];
240 }
241 }