‪TYPO3CMS  10.4
InputSlugElement.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
26 
31 {
32 
38  protected ‪$defaultFieldInformation = [
39  'tcaDescription' => [
40  'renderType' => 'tcaDescription',
41  ],
42  ];
43 
49  protected ‪$defaultFieldWizard = [
50  'localizationStateSelector' => [
51  'renderType' => 'localizationStateSelector',
52  ],
53  'otherLanguageContent' => [
54  'renderType' => 'otherLanguageContent',
55  'after' => [
56  'localizationStateSelector'
57  ],
58  ],
59  'defaultLanguageDifferences' => [
60  'renderType' => 'defaultLanguageDifferences',
61  'after' => [
62  'otherLanguageContent',
63  ],
64  ],
65  ];
66 
72  public function ‪render()
73  {
74  $table = $this->data['tableName'];
75  $row = $this->data['databaseRow'];
76  $parameterArray = $this->data['parameterArray'];
77  $resultArray = $this->‪initializeResultArray();
78 
79  $languageId = 0;
80  if (isset(‪$GLOBALS['TCA'][$table]['ctrl']['languageField']) && !empty(‪$GLOBALS['TCA'][$table]['ctrl']['languageField'])) {
81  $languageField = ‪$GLOBALS['TCA'][$table]['ctrl']['languageField'];
82  $languageId = (int)((is_array($row[$languageField]) ? $row[$languageField][0] : $row[$languageField]) ?? 0);
83  }
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  $baseUrl = $this->data['customData'][$this->data['fieldName']]['slugPrefix'] ?? '';
91 
92  // Convert UTF-8 characters back (that is important, see Slug class when sanitizing)
93  $itemValue = rawurldecode($itemValue);
94 
95  $fieldInformationResult = $this->‪renderFieldInformation();
96  $fieldInformationHtml = $fieldInformationResult['html'];
97  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
98 
99  $fieldControlResult = $this->‪renderFieldControl();
100  $fieldControlHtml = $fieldControlResult['html'];
101  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
102 
103  $fieldWizardResult = $this->‪renderFieldWizard();
104  $fieldWizardHtml = $fieldWizardResult['html'];
105  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
106  $toggleButtonTitle = $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.toggleSlugExplanation');
107  $recreateButtonTitle = $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.recreateSlugExplanation');
108 
109  $successMessage = $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:slugCreation.success.' . ($table === 'pages' ? 'page' : 'record'));
110  $errorMessage = $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:slugCreation.error');
111 
112  $thisSlugId = 't3js-form-field-slug-id' . ‪StringUtility::getUniqueId();
113  $mainFieldHtml = [];
114  $mainFieldHtml[] = '<div class="formengine-field-item t3js-formengine-field-item">';
115  $mainFieldHtml[] = $fieldInformationHtml;
116  $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px" id="' . htmlspecialchars($thisSlugId) . '">';
117  $mainFieldHtml[] = '<div class="form-wizards-wrap">';
118  $mainFieldHtml[] = '<div class="form-wizards-element">';
119  $mainFieldHtml[] = '<div class="input-group">';
120  $mainFieldHtml[] = ($baseUrl ? '<span class="input-group-addon">' . htmlspecialchars($baseUrl) . '</span>' : '');
121  // We deal with 3 fields here: a readonly field for current / default values, an input
122  // field to manipulate the value, and the final hidden field used to send the value
123  $mainFieldHtml[] = '<input';
124  $mainFieldHtml[] = ' class="form-control t3js-form-field-slug-readonly"';
125  $mainFieldHtml[] = ' data-toggle="tooltip"';
126  $mainFieldHtml[] = ' data-title="' . htmlspecialchars($itemValue) . '"';
127  $mainFieldHtml[] = ' value="' . htmlspecialchars($itemValue) . '"';
128  $mainFieldHtml[] = ' readonly';
129  $mainFieldHtml[] = ' />';
130  $mainFieldHtml[] = '<input type="text"';
131  $mainFieldHtml[] = ' id="' . htmlspecialchars(‪StringUtility::getUniqueId('formengine-input-')) . '"';
132  $mainFieldHtml[] = ' class="form-control t3js-form-field-slug-input hidden"';
133  $mainFieldHtml[] = ' placeholder="' . htmlspecialchars($row['slug'] ?? '/') . '"';
134  $mainFieldHtml[] = ' data-formengine-validation-rules="' . htmlspecialchars($this->‪getValidationDataAsJsonString($config)) . '"';
135  $mainFieldHtml[] = ' data-formengine-input-params="' . htmlspecialchars((string)json_encode(['field' => $parameterArray['itemFormElName'], 'evalList' => implode(',', $evalList)])) . '"';
136  $mainFieldHtml[] = ' data-formengine-input-name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
137  $mainFieldHtml[] = ' />';
138  $mainFieldHtml[] = '<span class="input-group-btn">';
139  $mainFieldHtml[] = '<button class="btn btn-default t3js-form-field-slug-toggle" type="button" title="' . htmlspecialchars($toggleButtonTitle) . '">';
140  $mainFieldHtml[] = $this->iconFactory->getIcon('actions-version-workspaces-preview-link', ‪Icon::SIZE_SMALL)->render();
141  $mainFieldHtml[] = '</button>';
142  $mainFieldHtml[] = '<button class="btn btn-default t3js-form-field-slug-recreate" type="button" title="' . htmlspecialchars($recreateButtonTitle) . '">';
143  $mainFieldHtml[] = $this->iconFactory->getIcon('actions-refresh', ‪Icon::SIZE_SMALL)->render();
144  $mainFieldHtml[] = '</button>';
145  $mainFieldHtml[] = '</span>';
146  $mainFieldHtml[] = '<input type="hidden"';
147  $mainFieldHtml[] = ' class="t3js-form-field-slug-hidden"';
148  $mainFieldHtml[] = ' name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
149  $mainFieldHtml[] = ' value="' . htmlspecialchars($itemValue) . '"';
150  $mainFieldHtml[] = ' />';
151  $mainFieldHtml[] = '</div>';
152  $mainFieldHtml[] = '</div>';
153  if (!empty($fieldControlHtml)) {
154  $mainFieldHtml[] = '<div class="form-wizards-items-aside">';
155  $mainFieldHtml[] = '<div class="btn-group">';
156  $mainFieldHtml[] = $fieldControlHtml;
157  $mainFieldHtml[] = '</div>';
158  $mainFieldHtml[] = '</div>';
159  }
160  $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
161  $mainFieldHtml[] = '<span class="t3js-form-proposal-accepted hidden label label-success" style="margin-top: 1em; padding: 0.75em 1em; display: inline-block; font-size: 90%; font-weight: 400;">' . sprintf(htmlspecialchars($successMessage), '<samp class="text-nowrap">' . htmlspecialchars($baseUrl) . '<span style="font-weight: 800;">/abc/</span></samp>') . '</span>';
162  $mainFieldHtml[] = '<span class="t3js-form-proposal-different hidden label label-warning" style="margin-top: 1em; padding: 0.75em 1em; display: inline-block; font-size: 90%; font-weight: 400;">' . sprintf(htmlspecialchars($errorMessage), '<samp class="text-nowrap">' . htmlspecialchars($baseUrl) . '<span style="font-weight: 800;">/abc/</span></samp>') . '</span>';
163  $mainFieldHtml[] = $fieldWizardHtml;
164  $mainFieldHtml[] = '</div>';
165  $mainFieldHtml[] = '</div>';
166  $mainFieldHtml[] = '</div>';
167  $mainFieldHtml[] = '</div>';
168 
169  $resultArray['html'] = implode(LF, $mainFieldHtml);
170 
171  [$commonElementPrefix] = ‪GeneralUtility::revExplode('[', $parameterArray['itemFormElName'], 2);
172  $validInputNamesToListenTo = [];
173  $includeUidInValues = false;
174  foreach ($config['generatorOptions']['fields'] ?? [] as $fieldNameParts) {
175  if (is_string($fieldNameParts)) {
176  $fieldNameParts = ‪GeneralUtility::trimExplode(',', $fieldNameParts);
177  }
178  foreach ($fieldNameParts as $listenerFieldName) {
179  if ($listenerFieldName === 'uid') {
180  $includeUidInValues = true;
181  continue;
182  }
183  $validInputNamesToListenTo[$listenerFieldName] = $commonElementPrefix . '[' . htmlspecialchars($listenerFieldName) . ']';
184  }
185  }
186  $parentPageId = $this->data['parentPageRow']['uid'] ?? 0;
187  $signature = GeneralUtility::hmac(
188  implode(
189  '',
190  [
191  $table,
192  $this->data['effectivePid'],
193  $row['uid'],
194  $languageId,
195  $this->data['fieldName'],
196  $this->data['command'],
197  $parentPageId
198  ]
199  ),
200  FormSlugAjaxController::class
201  );
202  $optionsForModule = [
203  'pageId' => $this->data['effectivePid'],
204  'recordId' => $row['uid'],
205  'tableName' => $table,
206  'fieldName' => $this->data['fieldName'],
207  'config' => $config,
208  'listenerFieldNames' => $validInputNamesToListenTo,
209  'language' => $languageId,
210  'originalValue' => $itemValue,
211  'signature' => $signature,
212  'command' => $this->data['command'],
213  'parentPageId' => $parentPageId,
214  'includeUidInValues' => $includeUidInValues,
215  ];
216  $resultArray['requireJsModules'][] = ['TYPO3/CMS/Backend/FormEngine/Element/SlugElement' => '
217  function(SlugElement) {
218  new SlugElement(' . GeneralUtility::quoteJSvalue('#' . $thisSlugId) . ', ' . json_encode($optionsForModule) . ');
219  }'
220  ];
221  return $resultArray;
222  }
223 
227  protected function ‪getLanguageService(): ‪LanguageService
228  {
229  return ‪$GLOBALS['LANG'];
230  }
231 }
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:72
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:116
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪array initializeResultArray()
Definition: AbstractNode.php:90
‪TYPO3\CMS\Backend\Form\Element\InputSlugElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: InputSlugElement.php:37
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:32
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:32
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:194
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪string getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:151
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldControl
‪array renderFieldControl()
Definition: AbstractFormElement.php:88
‪TYPO3\CMS\Backend\Form\Element\InputSlugElement
Definition: InputSlugElement.php:31
‪TYPO3\CMS\Backend\Form\Element\InputSlugElement\render
‪array render()
Definition: InputSlugElement.php:70
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\formMaxWidth
‪int formMaxWidth($size=48)
Definition: AbstractFormElement.php:298
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\GeneralUtility\revExplode
‪static string[] revExplode($delimiter, $string, $count=0)
Definition: GeneralUtility.php:1025
‪TYPO3\CMS\Backend\Form\Element\InputSlugElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: InputSlugElement.php:47
‪TYPO3\CMS\Backend\Form\Element\InputSlugElement\getLanguageService
‪LanguageService getLanguageService()
Definition: InputSlugElement.php:225
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:104
‪TYPO3\CMS\Backend\Controller\FormSlugAjaxController
Definition: FormSlugAjaxController.php:35