cfbf13b7c442638066917042fa9e600dfc412b91
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / InputSlugElement.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Form\Element;
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
18 use TYPO3\CMS\Backend\Controller\FormSlugAjaxController;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Localization\LanguageService;
21 use TYPO3\CMS\Core\Site\Entity\SiteInterface;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Core\Utility\MathUtility;
24 use TYPO3\CMS\Core\Utility\StringUtility;
25
26 /**
27 * General type=input element with some additional value.
28 */
29 class InputSlugElement extends AbstractFormElement
30 {
31
32 /**
33 * Default field information enabled for this element.
34 *
35 * @var array
36 */
37 protected $defaultFieldInformation = [
38 'tcaDescription' => [
39 'renderType' => 'tcaDescription',
40 ],
41 ];
42
43 /**
44 * Default field wizards enabled for this element.
45 *
46 * @var array
47 */
48 protected $defaultFieldWizard = [
49 'localizationStateSelector' => [
50 'renderType' => 'localizationStateSelector',
51 ],
52 'otherLanguageContent' => [
53 'renderType' => 'otherLanguageContent',
54 'after' => [
55 'localizationStateSelector'
56 ],
57 ],
58 'defaultLanguageDifferences' => [
59 'renderType' => 'defaultLanguageDifferences',
60 'after' => [
61 'otherLanguageContent',
62 ],
63 ],
64 ];
65
66 /**
67 * This will render a single-line input form field, possibly with various control/validation features
68 *
69 * @return array As defined in initializeResultArray() of AbstractNode
70 */
71 public function render()
72 {
73 $table = $this->data['tableName'];
74 $row = $this->data['databaseRow'];
75 $parameterArray = $this->data['parameterArray'];
76 $resultArray = $this->initializeResultArray();
77
78 $languageField = $GLOBALS['TCA'][$table]['ctrl']['languageField'];
79 $languageId = (int)($row[$languageField] ?? 0);
80 $baseUrl = $this->getPrefix($this->data['site'], $languageId);
81
82 $itemValue = $parameterArray['itemFormElValue'];
83 $config = $parameterArray['fieldConf']['config'];
84 $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
85 $size = MathUtility::forceIntegerInRange($config['size'] ?? $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
86 $width = (int)$this->formMaxWidth($size);
87
88 // Convert UTF-8 characters back (that is important, see Slug class when sanitizing)
89 $itemValue = rawurldecode($itemValue);
90
91 $fieldInformationResult = $this->renderFieldInformation();
92 $fieldInformationHtml = $fieldInformationResult['html'];
93 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
94
95 $fieldControlResult = $this->renderFieldControl();
96 $fieldControlHtml = $fieldControlResult['html'];
97 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
98
99 $fieldWizardResult = $this->renderFieldWizard();
100 $fieldWizardHtml = $fieldWizardResult['html'];
101 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
102 $toggleButtonTitle = $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.toggleSlugExplanation');
103 $recreateButtonTitle = $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.recreateSlugExplanation');
104
105 $thisSlugId = 't3js-form-field-slug-id' . StringUtility::getUniqueId();
106 $mainFieldHtml = [];
107 $mainFieldHtml[] = '<div class="formengine-field-item t3js-formengine-field-item">';
108 $mainFieldHtml[] = $fieldInformationHtml;
109 $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px" id="' . htmlspecialchars($thisSlugId) . '">';
110 $mainFieldHtml[] = '<div class="form-wizards-wrap">';
111 $mainFieldHtml[] = '<div class="form-wizards-element">';
112 $mainFieldHtml[] = '<div class="input-group">';
113 $mainFieldHtml[] = ($baseUrl ? '<span class="input-group-addon">' . htmlspecialchars($baseUrl) . '</span>' : '');
114 // We deal with 3 fields here: a readonly field for current / default values, an input
115 // field to manipulate the value, and the final hidden field used to send the value
116 $mainFieldHtml[] = '<input';
117 $mainFieldHtml[] = ' class="form-control t3js-form-field-slug-readonly"';
118 $mainFieldHtml[] = ' data-toggle="tooltip"';
119 $mainFieldHtml[] = ' data-title="' . htmlspecialchars($itemValue) . '"';
120 $mainFieldHtml[] = ' value="' . htmlspecialchars($itemValue) . '"';
121 $mainFieldHtml[] = ' readonly';
122 $mainFieldHtml[] = ' />';
123 $mainFieldHtml[] = '<input type="text"';
124 $mainFieldHtml[] = ' id="' . htmlspecialchars(StringUtility::getUniqueId('formengine-input-')) . '"';
125 $mainFieldHtml[] = ' class="form-control t3js-form-field-slug-input hidden"';
126 $mainFieldHtml[] = ' placeholder="' . htmlspecialchars($row['slug'] ?? '/') . '"';
127 $mainFieldHtml[] = ' data-formengine-validation-rules="' . htmlspecialchars($this->getValidationDataAsJsonString($config)) . '"';
128 $mainFieldHtml[] = ' data-formengine-input-params="' . htmlspecialchars(json_encode(['field' => $parameterArray['itemFormElName'], 'evalList' => implode(',', $evalList)])) . '"';
129 $mainFieldHtml[] = ' data-formengine-input-name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
130 $mainFieldHtml[] = ' />';
131 $mainFieldHtml[] = '<span class="input-group-btn">';
132 $mainFieldHtml[] = '<button class="btn btn-default t3js-form-field-slug-toggle" type="button" title="' . htmlspecialchars($toggleButtonTitle) . '">';
133 $mainFieldHtml[] = $this->iconFactory->getIcon('actions-version-workspaces-preview-link', Icon::SIZE_SMALL)->render();
134 $mainFieldHtml[] = '</button>';
135 $mainFieldHtml[] = '<button class="btn btn-default t3js-form-field-slug-recreate" type="button" title="' . htmlspecialchars($recreateButtonTitle) . '">';
136 $mainFieldHtml[] = $this->iconFactory->getIcon('actions-refresh', Icon::SIZE_SMALL)->render();
137 $mainFieldHtml[] = '</button>';
138 $mainFieldHtml[] = '</span>';
139 $mainFieldHtml[] = '<input type="hidden"';
140 $mainFieldHtml[] = ' class="t3js-form-field-slug-hidden"';
141 $mainFieldHtml[] = ' name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
142 $mainFieldHtml[] = ' value="' . htmlspecialchars($itemValue) . '"';
143 $mainFieldHtml[] = ' />';
144 $mainFieldHtml[] = '</div>';
145 $mainFieldHtml[] = '</div>';
146 if (!empty($fieldControlHtml)) {
147 $mainFieldHtml[] = '<div class="form-wizards-items-aside">';
148 $mainFieldHtml[] = '<div class="btn-group">';
149 $mainFieldHtml[] = $fieldControlHtml;
150 $mainFieldHtml[] = '</div>';
151 $mainFieldHtml[] = '</div>';
152 }
153 $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
154 $mainFieldHtml[] = '<span class="t3js-form-proposal-accepted hidden label label-success">Congrats, this page will look like ' . htmlspecialchars($baseUrl) . '<span>/abc/</span></span>';
155 $mainFieldHtml[] = '<span class="t3js-form-proposal-different hidden label label-warning">Hmm, that is taken, how about ' . htmlspecialchars($baseUrl) . '<span>/abc/</span></span>';
156 $mainFieldHtml[] = $fieldWizardHtml;
157 $mainFieldHtml[] = '</div>';
158 $mainFieldHtml[] = '</div>';
159 $mainFieldHtml[] = '</div>';
160 $mainFieldHtml[] = '</div>';
161
162 $resultArray['html'] = implode(LF, $mainFieldHtml);
163
164 [$commonElementPrefix] = GeneralUtility::revExplode('[', $parameterArray['itemFormElName'], 2);
165 $validInputNamesToListenTo = [];
166 foreach ($config['generatorOptions']['fields'] ?? [] as $listenerFieldName) {
167 $validInputNamesToListenTo[$listenerFieldName] = $commonElementPrefix . '[' . htmlspecialchars($listenerFieldName) . ']';
168 }
169 $parentPageId = $this->data['parentPageRow']['uid'] ?? 0;
170 $signature = GeneralUtility::hmac(
171 implode(
172 '',
173 [
174 $table,
175 $this->data['effectivePid'],
176 $row['uid'],
177 $languageId,
178 $this->data['fieldName'],
179 $this->data['command'],
180 $parentPageId
181 ]
182 ),
183 FormSlugAjaxController::class
184 );
185 $optionsForModule = [
186 'pageId' => $this->data['effectivePid'],
187 'recordId' => $row['uid'],
188 'tableName' => $table,
189 'fieldName' => $this->data['fieldName'],
190 'config' => $config,
191 'listenerFieldNames' => $validInputNamesToListenTo,
192 'language' => $languageId,
193 'originalValue' => $itemValue,
194 'signature' => $signature,
195 'command' => $this->data['command'],
196 'parentPageId' => $parentPageId,
197 ];
198 $resultArray['requireJsModules'][] = ['TYPO3/CMS/Backend/FormEngine/Element/SlugElement' => '
199 function(SlugElement) {
200 new SlugElement(' . GeneralUtility::quoteJSvalue('#' . $thisSlugId) . ', ' . json_encode($optionsForModule) . ');
201 }'
202 ];
203 return $resultArray;
204 }
205
206 /**
207 * Render the prefix for the input field.
208 *
209 * @param SiteInterface $site
210 * @param int $requestLanguageId
211 * @return string
212 */
213 protected function getPrefix(SiteInterface $site, int $requestLanguageId = 0): string
214 {
215 $language = $site->getLanguageById($requestLanguageId);
216 $base = $language->getBase();
217 $baseUrl = (string)$base;
218 $baseUrl = rtrim($baseUrl, '/');
219 if (!empty($baseUrl) && empty($base->getScheme()) && $base->getHost() !== '') {
220 $baseUrl = 'http:' . $baseUrl;
221 }
222 return $baseUrl;
223 }
224
225 /**
226 * @return LanguageService
227 */
228 protected function getLanguageService(): LanguageService
229 {
230 return $GLOBALS['LANG'];
231 }
232 }