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