[BUGFIX] Set opt-in for override checkbox in TCA input elements
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / InputLinkElement.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\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Imaging\Icon;
19 use TYPO3\CMS\Core\LinkHandling\Exception\UnknownLinkHandlerException;
20 use TYPO3\CMS\Core\LinkHandling\LinkService;
21 use TYPO3\CMS\Core\Localization\LanguageService;
22 use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
23 use TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException;
24 use TYPO3\CMS\Core\Resource\Exception\InvalidPathException;
25 use TYPO3\CMS\Core\Resource\File;
26 use TYPO3\CMS\Core\Resource\Folder;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28 use TYPO3\CMS\Core\Utility\MathUtility;
29 use TYPO3\CMS\Core\Utility\StringUtility;
30 use TYPO3\CMS\Frontend\Service\TypoLinkCodecService;
31
32 /**
33 * Link input element.
34 *
35 * Shows current link and the link popup.
36 */
37 class InputLinkElement extends AbstractFormElement
38 {
39 /**
40 * Default field controls render the link icon
41 *
42 * @var array
43 */
44 protected $defaultFieldControl = [
45 'linkPopup' => [
46 'renderType' => 'linkPopup',
47 'options' => []
48 ],
49 ];
50
51 /**
52 * Default field wizards enabled for this element.
53 *
54 * @var array
55 */
56 protected $defaultFieldWizard = [
57 'localizationStateSelector' => [
58 'renderType' => 'localizationStateSelector',
59 ],
60 'otherLanguageContent' => [
61 'renderType' => 'otherLanguageContent',
62 'after' => [
63 'localizationStateSelector'
64 ],
65 ],
66 'defaultLanguageDifferences' => [
67 'renderType' => 'defaultLanguageDifferences',
68 'after' => [
69 'otherLanguageContent',
70 ],
71 ],
72 ];
73
74 /**
75 * This will render a single-line input form field, possibly with various control/validation features
76 *
77 * @return array As defined in initializeResultArray() of AbstractNode
78 */
79 public function render()
80 {
81 $languageService = $this->getLanguageService();
82
83 $table = $this->data['tableName'];
84 $fieldName = $this->data['fieldName'];
85 $row = $this->data['databaseRow'];
86 $parameterArray = $this->data['parameterArray'];
87 $resultArray = $this->initializeResultArray();
88 $config = $parameterArray['fieldConf']['config'];
89
90 $itemValue = $parameterArray['itemFormElValue'];
91 $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
92 $size = MathUtility::forceIntegerInRange($config['size'] ?? $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
93 $width = (int)$this->formMaxWidth($size);
94 $nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $row['uid'] . '][' . $fieldName . ']');
95
96 if ($config['readOnly']) {
97 // Early return for read only fields
98 $html = [];
99 $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
100 $html[] = '<div class="form-wizards-wrap">';
101 $html[] = '<div class="form-wizards-element">';
102 $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
103 $html[] = '<input class="form-control" value="' . htmlspecialchars($itemValue) . '" type="text" disabled>';
104 $html[] = '</div>';
105 $html[] = '</div>';
106 $html[] = '</div>';
107 $html[] = '</div>';
108 $resultArray['html'] = implode(LF, $html);
109 return $resultArray;
110 }
111
112 // @todo: The whole eval handling is a mess and needs refactoring
113 foreach ($evalList as $func) {
114 // @todo: This is ugly: The code should find out on it's own whether a eval definition is a
115 // @todo: keyword like "date", or a class reference. The global registration could be dropped then
116 // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
117 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
118 if (class_exists($func)) {
119 $evalObj = GeneralUtility::makeInstance($func);
120 if (method_exists($evalObj, 'deevaluateFieldValue')) {
121 $_params = [
122 'value' => $itemValue
123 ];
124 $itemValue = $evalObj->deevaluateFieldValue($_params);
125 }
126 if (method_exists($evalObj, 'returnFieldJS')) {
127 $resultArray['additionalJavaScriptPost'][] = 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($func) . ']'
128 . ' = function(value) {' . $evalObj->returnFieldJS() . '};';
129 }
130 }
131 }
132 }
133
134 $attributes = [
135 'value' => '',
136 'id' => StringUtility::getUniqueId('formengine-input-'),
137 'class' => implode(' ', [
138 'form-control',
139 't3js-clearable',
140 't3js-form-field-inputlink-input',
141 'hidden',
142 'hasDefaultValue',
143 ]),
144 'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
145 'data-formengine-input-params' => json_encode([
146 'field' => $parameterArray['itemFormElName'],
147 'evalList' => implode(',', $evalList)
148 ]),
149 'data-formengine-input-name' => $parameterArray['itemFormElName'],
150 ];
151
152 $maxLength = $config['max'] ?? 0;
153 if ((int)$maxLength > 0) {
154 $attributes['maxlength'] = (int)$maxLength;
155 }
156 if (!empty($config['placeholder'])) {
157 $attributes['placeholder'] = trim($config['placeholder']);
158 }
159 if (isset($config['autocomplete'])) {
160 $attributes['autocomplete'] = empty($config['autocomplete']) ? 'new-' . $fieldName : 'on';
161 }
162
163 $valuePickerHtml = [];
164 if (isset($config['valuePicker']['items']) && is_array($config['valuePicker']['items'])) {
165 $mode = $config['valuePicker']['mode'] ?? '';
166 $itemName = $parameterArray['itemFormElName'];
167 $fieldChangeFunc = $parameterArray['fieldChangeFunc'];
168 if ($mode === 'append') {
169 $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
170 . '.value=\'\'+this.options[this.selectedIndex].value+document.editform[' . GeneralUtility::quoteJSvalue($itemName) . '].value';
171 } elseif ($mode === 'prepend') {
172 $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
173 . '.value+=\'\'+this.options[this.selectedIndex].value';
174 } else {
175 $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
176 . '.value=this.options[this.selectedIndex].value';
177 }
178 $valuePickerHtml[] = '<select';
179 $valuePickerHtml[] = ' class="form-control tceforms-select tceforms-wizardselect"';
180 $valuePickerHtml[] = ' onchange="' . htmlspecialchars($assignValue . ';this.blur();this.selectedIndex=0;' . implode('', $fieldChangeFunc)) . '"';
181 $valuePickerHtml[] = '>';
182 $valuePickerHtml[] = '<option></option>';
183 foreach ($config['valuePicker']['items'] as $item) {
184 $valuePickerHtml[] = '<option value="' . htmlspecialchars($item[1]) . '">' . htmlspecialchars($languageService->sL($item[0])) . '</option>';
185 }
186 $valuePickerHtml[] = '</select>';
187 }
188
189 $fieldInformationResult = $this->renderFieldInformation();
190 $fieldInformationHtml = $fieldInformationResult['html'];
191 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
192
193 $fieldWizardResult = $this->renderFieldWizard();
194 $fieldWizardHtml = $fieldWizardResult['html'];
195 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
196
197 $fieldControlResult = $this->renderFieldControl();
198 $fieldControlHtml = $fieldControlResult['html'];
199 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
200
201 $linkExplanation = $this->getLinkExplanation($itemValue ?: '');
202 $explanation = htmlspecialchars($linkExplanation['text']);
203 $toggleButtonTitle = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.toggleLinkExplanation');
204
205 $expansionHtml = [];
206 $expansionHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
207 $expansionHtml[] = '<div class="form-wizards-wrap">';
208 $expansionHtml[] = '<div class="form-wizards-element">';
209 $expansionHtml[] = '<div class="input-group t3js-form-field-inputlink">';
210 $expansionHtml[] = '<span class="input-group-addon">' . $linkExplanation['icon'] . '</span>';
211 $expansionHtml[] = '<input class="form-control form-field-inputlink-explanation t3js-form-field-inputlink-explanation" data-toggle="tooltip" data-title="' . $explanation . '" value="' . $explanation . '" readonly>';
212 $expansionHtml[] = '<input type="text"' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
213 $expansionHtml[] = '<span class="input-group-btn">';
214 $expansionHtml[] = '<button class="btn btn-default t3js-form-field-inputlink-explanation-toggle" type="button" title="' . htmlspecialchars($toggleButtonTitle) . '">';
215 $expansionHtml[] = $this->iconFactory->getIcon('actions-version-workspaces-preview-link', Icon::SIZE_SMALL)->render();
216 $expansionHtml[] = '</button>';
217 $expansionHtml[] = '</span>';
218 $expansionHtml[] = '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($itemValue) . '" />';
219 $expansionHtml[] = '</div>';
220 $expansionHtml[] = '</div>';
221 $expansionHtml[] = '<div class="form-wizards-items-aside">';
222 $expansionHtml[] = '<div class="btn-group">';
223 $expansionHtml[] = implode(LF, $valuePickerHtml);
224 $expansionHtml[] = $fieldControlHtml;
225 $expansionHtml[] = '</div>';
226 $expansionHtml[] = '</div>';
227 $expansionHtml[] = '<div class="form-wizards-items-bottom">';
228 $expansionHtml[] = $linkExplanation['additionalAttributes'];
229 $expansionHtml[] = $fieldWizardHtml;
230 $expansionHtml[] = '</div>';
231 $expansionHtml[] = '</div>';
232 $expansionHtml[] = '</div>';
233 $expansionHtml = implode(LF, $expansionHtml);
234
235 $fullElement = $expansionHtml;
236 if ($this->hasNullCheckboxButNoPlaceholder()) {
237 $checked = $itemValue !== null ? ' checked="checked"' : '';
238 $fullElement = [];
239 $fullElement[] = '<div class="t3-form-field-disable"></div>';
240 $fullElement[] = '<div class="checkbox t3-form-field-eval-null-checkbox">';
241 $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
242 $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
243 $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
244 $fullElement[] = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
245 $fullElement[] = '</label>';
246 $fullElement[] = '</div>';
247 $fullElement[] = $expansionHtml;
248 $fullElement = implode(LF, $fullElement);
249 } elseif ($this->hasNullCheckboxWithPlaceholder()) {
250 $checked = $itemValue !== null ? ' checked="checked"' : '';
251 $placeholder = $shortenedPlaceholder = $config['placeholder'] ?? '';
252 $disabled = '';
253 $fallbackValue = 0;
254 if (strlen($placeholder) > 0) {
255 $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
256 if ($placeholder !== $shortenedPlaceholder) {
257 $overrideLabel = sprintf(
258 $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
259 '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
260 );
261 } else {
262 $overrideLabel = sprintf(
263 $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
264 htmlspecialchars($placeholder)
265 );
266 }
267 } else {
268 $overrideLabel = $languageService->sL(
269 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
270 );
271 }
272 $fullElement = [];
273 $fullElement[] = '<div class="checkbox t3js-form-field-eval-null-placeholder-checkbox">';
274 $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
275 $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="' . $fallbackValue . '" />';
276 $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . $disabled . ' />';
277 $fullElement[] = $overrideLabel;
278 $fullElement[] = '</label>';
279 $fullElement[] = '</div>';
280 $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
281 $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
282 $fullElement[] = '<input type="text" class="form-control" disabled="disabled" value="' . $shortenedPlaceholder . '" />';
283 $fullElement[] = '</div>';
284 $fullElement[] = '</div>';
285 $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
286 $fullElement[] = $expansionHtml;
287 $fullElement[] = '</div>';
288 $fullElement = implode(LF, $fullElement);
289 }
290
291 $resultArray['html'] = '<div class="formengine-field-item t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
292 return $resultArray;
293 }
294
295 /**
296 * @param string $itemValue
297 * @return array
298 */
299 protected function getLinkExplanation(string $itemValue): array
300 {
301 if (empty($itemValue)) {
302 return [];
303 }
304 $data = ['text' => '', 'icon' => ''];
305 $typolinkService = GeneralUtility::makeInstance(TypoLinkCodecService::class);
306 $linkParts = $typolinkService->decode($itemValue);
307 $linkService = GeneralUtility::makeInstance(LinkService::class);
308
309 try {
310 $linkData = $linkService->resolve($linkParts['url']);
311 } catch (FileDoesNotExistException $e) {
312 return $data;
313 } catch (FolderDoesNotExistException $e) {
314 return $data;
315 } catch (UnknownLinkHandlerException $e) {
316 return $data;
317 } catch (InvalidPathException $e) {
318 return $data;
319 }
320
321 // Resolving the TypoLink parts (class, title, params)
322 $additionalAttributes = [];
323 foreach ($linkParts as $key => $value) {
324 if ($key === 'url') {
325 continue;
326 }
327 if ($value) {
328 switch ($key) {
329 case 'class':
330 $label = $this->getLanguageService()->sL('LLL:EXT:recordlist/Resources/Private/Language/locallang_browse_links.xlf:class');
331 break;
332 case 'title':
333 $label = $this->getLanguageService()->sL('LLL:EXT:recordlist/Resources/Private/Language/locallang_browse_links.xlf:title');
334 break;
335 case 'additionalParams':
336 $label = $this->getLanguageService()->sL('LLL:EXT:recordlist/Resources/Private/Language/locallang_browse_links.xlf:params');
337 break;
338 default:
339 $label = $key;
340 }
341
342 $additionalAttributes[] = '<span><strong>' . htmlspecialchars($label) . ': </strong> ' . htmlspecialchars($value) . '</span>';
343 }
344 }
345
346 // Resolve the actual link
347 switch ($linkData['type']) {
348 case LinkService::TYPE_PAGE:
349 $pageRecord = BackendUtility::readPageAccess($linkData['pageuid'], '1=1');
350 // Is this a real page
351 if ($pageRecord['uid']) {
352 $data = [
353 'text' => $pageRecord['_thePathFull'] . '[' . $pageRecord['uid'] . ']',
354 'icon' => $this->iconFactory->getIconForRecord('pages', $pageRecord, Icon::SIZE_SMALL)->render()
355 ];
356 }
357 break;
358 case LinkService::TYPE_EMAIL:
359 $data = [
360 'text' => $linkData['email'],
361 'icon' => $this->iconFactory->getIcon('content-elements-mailform', Icon::SIZE_SMALL)->render()
362 ];
363 break;
364 case LinkService::TYPE_URL:
365 $data = [
366 'text' => $this->getDomainByUrl($linkData['url']),
367 'icon' => $this->iconFactory->getIcon('apps-pagetree-page-shortcut-external', Icon::SIZE_SMALL)->render()
368
369 ];
370 break;
371 case LinkService::TYPE_FILE:
372 /** @var File $file */
373 $file = $linkData['file'];
374 if ($file) {
375 $data = [
376 'text' => $file->getPublicUrl(),
377 'icon' => $this->iconFactory->getIconForFileExtension($file->getExtension(), Icon::SIZE_SMALL)->render()
378 ];
379 }
380 break;
381 case LinkService::TYPE_FOLDER:
382 /** @var Folder $folder */
383 $folder = $linkData['folder'];
384 if ($folder) {
385 $data = [
386 'text' => $folder->getPublicUrl(),
387 'icon' => $this->iconFactory->getIcon('apps-filetree-folder-default', Icon::SIZE_SMALL)->render()
388 ];
389 }
390 break;
391 case LinkService::TYPE_RECORD:
392 $table = $this->data['pageTsConfig']['TCEMAIN.']['linkHandler.'][$linkData['identifier'] . '.']['configuration.']['table'];
393 $record = BackendUtility::getRecord($table, $linkData['uid']);
394 if ($record) {
395 $recordTitle = BackendUtility::getRecordTitle($table, $record);
396 $tableTitle = $this->getLanguageService()->sL($GLOBALS['TCA'][$table]['ctrl']['title']);
397 $data = [
398 'text' => sprintf('%s [%s:%d]', $recordTitle, $tableTitle, $linkData['uid']),
399 'icon' => $this->iconFactory->getIconForRecord($table, $record, Icon::SIZE_SMALL)->render(),
400 ];
401 } else {
402 $icon = $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes']['default'];
403 if (empty($icon)) {
404 $icon = 'tcarecords-' . $table . '-default';
405 }
406 $data = [
407 'text' => sprintf('%s', $linkData['uid']),
408 'icon' => $this->iconFactory->getIcon('tcarecords-' . $table . '-default', Icon::SIZE_SMALL, 'overlay-missing')->render(),
409 ];
410 }
411 break;
412 default:
413 // Please note that this hook is preliminary and might change, as this element could become its own
414 // TCA type in the future
415 if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['linkHandler'][$linkData['type']])) {
416 $linkBuilder = GeneralUtility::makeInstance($GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['linkHandler'][$linkData['type']]);
417 $data = $linkBuilder->getFormData($linkData, $linkParts, $this->data, $this);
418 } else {
419 $data = [
420 'text' => 'not implemented type ' . $linkData['type'],
421 'icon' => ''
422 ];
423 }
424 }
425
426 $data['additionalAttributes'] = '<div class="help-block">' . implode(' - ', $additionalAttributes) . '</div>';
427 return $data;
428 }
429
430 /**
431 * @param string $uriString
432 *
433 * @return string
434 */
435 protected function getDomainByUrl(string $uriString): string
436 {
437 $data = parse_url($uriString);
438 return $data['host'] ?? '';
439 }
440
441 /**
442 * @return LanguageService
443 */
444 protected function getLanguageService()
445 {
446 return $GLOBALS['LANG'];
447 }
448 }