‪TYPO3CMS  9.5
InputSlugElement.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
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 
25 
30 {
31 
37  protected ‪$defaultFieldInformation = [
38  'tcaDescription' => [
39  'renderType' => 'tcaDescription',
40  ],
41  ];
42 
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 
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  $languageId = 0;
79  if (isset(‪$GLOBALS['TCA'][$table]['ctrl']['languageField']) && !empty(‪$GLOBALS['TCA'][$table]['ctrl']['languageField'])) {
80  $languageField = ‪$GLOBALS['TCA'][$table]['ctrl']['languageField'];
81  $languageId = (int)((is_array($row[$languageField]) ? $row[$languageField][0] : $row[$languageField]) ?? 0);
82  }
83  $baseUrl = $this->‪getPrefix($this->data['site'], $languageId);
84 
85  $itemValue = $parameterArray['itemFormElValue'];
86  $config = $parameterArray['fieldConf']['config'];
87  $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
88  $size = ‪MathUtility::forceIntegerInRange($config['size'] ?? $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
89  $width = (int)$this->‪formMaxWidth($size);
90 
91  // Convert UTF-8 characters back (that is important, see Slug class when sanitizing)
92  $itemValue = rawurldecode($itemValue);
93 
94  $fieldInformationResult = $this->‪renderFieldInformation();
95  $fieldInformationHtml = $fieldInformationResult['html'];
96  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
97 
98  $fieldControlResult = $this->‪renderFieldControl();
99  $fieldControlHtml = $fieldControlResult['html'];
100  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
101 
102  $fieldWizardResult = $this->‪renderFieldWizard();
103  $fieldWizardHtml = $fieldWizardResult['html'];
104  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
105  $toggleButtonTitle = $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.toggleSlugExplanation');
106  $recreateButtonTitle = $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.recreateSlugExplanation');
107 
108  $successMessage = sprintf($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:slugCreation.success.' . ($table === 'pages' ? 'page' : 'record')), $baseUrl);
109  $errorMessage = sprintf($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:slugCreation.error'), $baseUrl);
110 
111  $thisSlugId = 't3js-form-field-slug-id' . ‪StringUtility::getUniqueId();
112  $mainFieldHtml = [];
113  $mainFieldHtml[] = '<div class="formengine-field-item t3js-formengine-field-item">';
114  $mainFieldHtml[] = $fieldInformationHtml;
115  $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px" id="' . htmlspecialchars($thisSlugId) . '">';
116  $mainFieldHtml[] = '<div class="form-wizards-wrap">';
117  $mainFieldHtml[] = '<div class="form-wizards-element">';
118  $mainFieldHtml[] = '<div class="input-group">';
119  $mainFieldHtml[] = ($baseUrl ? '<span class="input-group-addon">' . htmlspecialchars($baseUrl) . '</span>' : '');
120  // We deal with 3 fields here: a readonly field for current / default values, an input
121  // field to manipulate the value, and the final hidden field used to send the value
122  $mainFieldHtml[] = '<input';
123  $mainFieldHtml[] = ' class="form-control t3js-form-field-slug-readonly"';
124  $mainFieldHtml[] = ' data-toggle="tooltip"';
125  $mainFieldHtml[] = ' data-title="' . htmlspecialchars($itemValue) . '"';
126  $mainFieldHtml[] = ' value="' . htmlspecialchars($itemValue) . '"';
127  $mainFieldHtml[] = ' readonly';
128  $mainFieldHtml[] = ' />';
129  $mainFieldHtml[] = '<input type="text"';
130  $mainFieldHtml[] = ' id="' . htmlspecialchars(‪StringUtility::getUniqueId('formengine-input-')) . '"';
131  $mainFieldHtml[] = ' class="form-control t3js-form-field-slug-input hidden"';
132  $mainFieldHtml[] = ' placeholder="' . htmlspecialchars($row['slug'] ?? '/') . '"';
133  $mainFieldHtml[] = ' data-formengine-validation-rules="' . htmlspecialchars($this->‪getValidationDataAsJsonString($config)) . '"';
134  $mainFieldHtml[] = ' data-formengine-input-params="' . htmlspecialchars(json_encode(['field' => $parameterArray['itemFormElName'], 'evalList' => implode(',', $evalList)])) . '"';
135  $mainFieldHtml[] = ' data-formengine-input-name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
136  $mainFieldHtml[] = ' />';
137  $mainFieldHtml[] = '<span class="input-group-btn">';
138  $mainFieldHtml[] = '<button class="btn btn-default t3js-form-field-slug-toggle" type="button" title="' . htmlspecialchars($toggleButtonTitle) . '">';
139  $mainFieldHtml[] = $this->iconFactory->getIcon('actions-version-workspaces-preview-link', ‪Icon::SIZE_SMALL)->render();
140  $mainFieldHtml[] = '</button>';
141  $mainFieldHtml[] = '<button class="btn btn-default t3js-form-field-slug-recreate" type="button" title="' . htmlspecialchars($recreateButtonTitle) . '">';
142  $mainFieldHtml[] = $this->iconFactory->getIcon('actions-refresh', ‪Icon::SIZE_SMALL)->render();
143  $mainFieldHtml[] = '</button>';
144  $mainFieldHtml[] = '</span>';
145  $mainFieldHtml[] = '<input type="hidden"';
146  $mainFieldHtml[] = ' class="t3js-form-field-slug-hidden"';
147  $mainFieldHtml[] = ' name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
148  $mainFieldHtml[] = ' value="' . htmlspecialchars($itemValue) . '"';
149  $mainFieldHtml[] = ' />';
150  $mainFieldHtml[] = '</div>';
151  $mainFieldHtml[] = '</div>';
152  if (!empty($fieldControlHtml)) {
153  $mainFieldHtml[] = '<div class="form-wizards-items-aside">';
154  $mainFieldHtml[] = '<div class="btn-group">';
155  $mainFieldHtml[] = $fieldControlHtml;
156  $mainFieldHtml[] = '</div>';
157  $mainFieldHtml[] = '</div>';
158  }
159  $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
160  $mainFieldHtml[] = '<span class="t3js-form-proposal-accepted hidden label label-success">' . htmlspecialchars($successMessage) . '<span>/abc/</span></span>';
161  $mainFieldHtml[] = '<span class="t3js-form-proposal-different hidden label label-warning">' . htmlspecialchars($errorMessage) . '<span>/abc/</span></span>';
162  $mainFieldHtml[] = $fieldWizardHtml;
163  $mainFieldHtml[] = '</div>';
164  $mainFieldHtml[] = '</div>';
165  $mainFieldHtml[] = '</div>';
166  $mainFieldHtml[] = '</div>';
167 
168  $resultArray['html'] = implode(LF, $mainFieldHtml);
169 
170  [$commonElementPrefix] = GeneralUtility::revExplode('[', $parameterArray['itemFormElName'], 2);
171  $validInputNamesToListenTo = [];
172  $includeUidInValues = false;
173  foreach ($config['generatorOptions']['fields'] ?? [] as $fieldNameParts) {
174  if (is_string($fieldNameParts)) {
175  $fieldNameParts = GeneralUtility::trimExplode(',', $fieldNameParts);
176  }
177  foreach ($fieldNameParts as $listenerFieldName) {
178  if ($listenerFieldName === 'uid') {
179  $includeUidInValues = true;
180  continue;
181  }
182  $validInputNamesToListenTo[$listenerFieldName] = $commonElementPrefix . '[' . htmlspecialchars($listenerFieldName) . ']';
183  }
184  }
185  $parentPageId = $this->data['parentPageRow']['uid'] ?? 0;
186  $signature = GeneralUtility::hmac(
187  implode(
188  '',
189  [
190  $table,
191  $this->data['effectivePid'],
192  $row['uid'],
193  $languageId,
194  $this->data['fieldName'],
195  $this->data['command'],
196  $parentPageId
197  ]
198  ),
199  FormSlugAjaxController::class
200  );
201  $optionsForModule = [
202  'pageId' => $this->data['effectivePid'],
203  'recordId' => $row['uid'],
204  'tableName' => $table,
205  'fieldName' => $this->data['fieldName'],
206  'config' => $config,
207  'listenerFieldNames' => $validInputNamesToListenTo,
208  'language' => $languageId,
209  'originalValue' => $itemValue,
210  'signature' => $signature,
211  'command' => $this->data['command'],
212  'parentPageId' => $parentPageId,
213  'includeUidInValues' => $includeUidInValues,
214  ];
215  $resultArray['requireJsModules'][] = ['TYPO3/CMS/Backend/FormEngine/Element/SlugElement' => '
216  function(SlugElement) {
217  new SlugElement(' . GeneralUtility::quoteJSvalue('#' . $thisSlugId) . ', ' . json_encode($optionsForModule) . ');
218  }'
219  ];
220  return $resultArray;
221  }
222 
230  protected function ‪getPrefix(‪SiteInterface $site, int $requestLanguageId = 0): string
231  {
232  try {
233  $language = ($requestLanguageId < 0) ? $site->‪getDefaultLanguage() : $site->‪getLanguageById($requestLanguageId);
234  $base = $language->‪getBase();
235  $baseUrl = (string)$base;
236  $baseUrl = rtrim($baseUrl, '/');
237  if (!empty($baseUrl) && empty($base->getScheme()) && $base->getHost() !== '') {
238  $baseUrl = 'http:' . $baseUrl;
239  }
240  } catch (\InvalidArgumentException $e) {
241  // No site / language found
242  $baseUrl = '';
243  }
244  return $baseUrl;
245  }
246 
250  protected function ‪getLanguageService(): ‪LanguageService
251  {
252  return ‪$GLOBALS['LANG'];
253  }
254 }
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:29
‪TYPO3\CMS\Core\Site\Entity\SiteInterface
Definition: SiteInterface.php:25
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:71
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:115
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪array initializeResultArray()
Definition: AbstractNode.php:88
‪TYPO3\CMS\Backend\Form\Element\InputSlugElement\getPrefix
‪string getPrefix(SiteInterface $site, int $requestLanguageId=0)
Definition: InputSlugElement.php:228
‪TYPO3\CMS\Backend\Form\Element\InputSlugElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: InputSlugElement.php:36
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:25
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:31
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:158
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:2
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪string getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:153
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldControl
‪array renderFieldControl()
Definition: AbstractFormElement.php:87
‪TYPO3\CMS\Backend\Form\Element\InputSlugElement
Definition: InputSlugElement.php:30
‪TYPO3\CMS\Core\Site\Entity\SiteInterface\getDefaultLanguage
‪SiteLanguage getDefaultLanguage()
‪TYPO3\CMS\Core\Site\Entity\SiteInterface\getLanguageById
‪SiteLanguage getLanguageById(int $languageId)
‪TYPO3\CMS\Backend\Form\Element\InputSlugElement\render
‪array render()
Definition: InputSlugElement.php:69
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\formMaxWidth
‪int formMaxWidth($size=48)
Definition: AbstractFormElement.php:297
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:91
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage\getBase
‪UriInterface getBase()
Definition: SiteLanguage.php:204
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Backend\Form\Element\InputSlugElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: InputSlugElement.php:46
‪TYPO3\CMS\Backend\Form\Element\InputSlugElement\getLanguageService
‪LanguageService getLanguageService()
Definition: InputSlugElement.php:248
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:21
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:103
‪TYPO3\CMS\Backend\Controller\FormSlugAjaxController
Definition: FormSlugAjaxController.php:34