[TASK] Remove unnecessary RTE default configuration
[Packages/TYPO3.CMS.git] / typo3 / sysext / rte_ckeditor / Classes / Form / Element / RichTextElement.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\RteCKEditor\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\Form\Element\AbstractFormElement;
19 use TYPO3\CMS\Backend\Routing\UriBuilder;
20 use TYPO3\CMS\Core\Localization\Locales;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Utility\PathUtility;
23
24 /**
25 * Render rich text editor in FormEngine
26 */
27 class RichTextElement extends AbstractFormElement
28 {
29 /**
30 * Default field wizards enabled for this element.
31 *
32 * @var array
33 */
34 protected $defaultFieldWizard = [
35 'otherLanguageContent' => [
36 'renderType' => 'otherLanguageContent',
37 ],
38 'defaultLanguageDifferences' => [
39 'renderType' => 'defaultLanguageDifferences',
40 'after' => [
41 'otherLanguageContent',
42 ],
43 ],
44 ];
45
46 /**
47 * This property contains configuration related to the RTE
48 * But only the .editor configuration part
49 *
50 * @var array
51 */
52 protected $rteConfiguration = [];
53
54 /**
55 * Renders the ckeditor element
56 *
57 * @return array
58 * @throws \InvalidArgumentException
59 */
60 public function render() : array
61 {
62 $resultArray = $this->initializeResultArray();
63 $parameterArray = $this->data['parameterArray'];
64 $config = $parameterArray['fieldConf']['config'];
65
66 $fieldId = $this->sanitizeFieldId($parameterArray['itemFormElName']);
67 $itemFormElementName = $this->data['parameterArray']['itemFormElName'];
68
69 $value = $this->data['parameterArray']['itemFormElValue'] ?? '';
70
71 $legacyWizards = $this->renderWizards();
72 $legacyFieldControlHtml = implode(LF, $legacyWizards['fieldControl']);
73 $legacyFieldWizardHtml = implode(LF, $legacyWizards['fieldWizard']);
74
75 $fieldInformationResult = $this->renderFieldInformation();
76 $fieldInformationHtml = $fieldInformationResult['html'];
77 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
78
79 $fieldControlResult = $this->renderFieldControl();
80 $fieldControlHtml = $legacyFieldControlHtml . $fieldControlResult['html'];
81 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
82
83 $fieldWizardResult = $this->renderFieldWizard();
84 $fieldWizardHtml = $legacyFieldWizardHtml . $fieldWizardResult['html'];
85 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
86
87 $attributes = [
88 'style' => 'display:none',
89 'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
90 'id' => $fieldId,
91 'name' => htmlspecialchars($itemFormElementName),
92 ];
93
94 $html = [];
95 $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
96 $html[] = $fieldInformationHtml;
97 $html[] = '<div class="form-control-wrap">';
98 $html[] = '<div class="form-wizards-wrap">';
99 $html[] = '<div class="form-wizards-element">';
100 $html[] = '<textarea ' . GeneralUtility::implodeAttributes($attributes, true) . '>';
101 $html[] = htmlspecialchars($value);
102 $html[] = '</textarea>';
103 $html[] = '</div>';
104 $html[] = '<div class="form-wizards-items-aside">';
105 $html[] = '<div class="btn-group">';
106 $html[] = $fieldControlHtml;
107 $html[] = '</div>';
108 $html[] = '</div>';
109 $html[] = '<div class="form-wizards-items-bottom">';
110 $html[] = $fieldWizardHtml;
111 $html[] = '</div>';
112 $html[] = '</div>';
113 $html[] = '</div>';
114 $html[] = '</div>';
115
116 $resultArray['html'] = implode(LF, $html);
117
118 $this->rteConfiguration = $config['richtextConfiguration']['editor'];
119 $resultArray['requireJsModules'] = [];
120 $resultArray['requireJsModules'][] = [
121 'ckeditor' => $this->getCkEditorRequireJsModuleCode($fieldId)
122 ];
123
124 return $resultArray;
125 }
126
127 /**
128 * Determine the contents language iso code
129 *
130 * @return string
131 */
132 protected function getLanguageIsoCodeOfContent(): string
133 {
134 $currentLanguageUid = $this->data['databaseRow']['sys_language_uid'];
135 if (is_array($currentLanguageUid)) {
136 $currentLanguageUid = $currentLanguageUid[0];
137 }
138 $contentLanguageUid = (int)max($currentLanguageUid, 0);
139 if ($contentLanguageUid) {
140 $contentLanguage = $this->data['systemLanguageRows'][$currentLanguageUid]['iso'];
141 } else {
142 $contentLanguage = $this->rteConfiguration['config']['defaultContentLanguage'] ?? 'en_US';
143 $languageCodeParts = explode('_', $contentLanguage);
144 $contentLanguage = strtolower($languageCodeParts[0]) . ($languageCodeParts[1] ? '_' . strtoupper($languageCodeParts[1]) : '');
145 // Find the configured language in the list of localization locales
146 $locales = GeneralUtility::makeInstance(Locales::class);
147 // If not found, default to 'en'
148 if (!in_array($contentLanguage, $locales->getLocales(), true)) {
149 $contentLanguage = 'en';
150 }
151 }
152 return $contentLanguage;
153 }
154
155 /**
156 * Gets the JavaScript code for CKEditor module
157 * Compiles the configuration, and then adds plugins
158 *
159 * @param string $fieldId
160 * @return string
161 */
162 protected function getCkEditorRequireJsModuleCode(string $fieldId) : string
163 {
164 $configuration = $this->prepareConfigurationForEditor();
165
166 $externalPlugins = '';
167 foreach ($this->getExtraPlugins() as $pluginName => $config) {
168 $configuration[$pluginName] = $config['config'];
169 $configuration['extraPlugins'] .= ',' . $pluginName;
170
171 $externalPlugins .= 'CKEDITOR.plugins.addExternal(';
172 $externalPlugins .= GeneralUtility::quoteJSvalue($pluginName) . ',';
173 $externalPlugins .= GeneralUtility::quoteJSvalue($config['resource']) . ',';
174 $externalPlugins .= '\'\');';
175 }
176
177 return 'function(CKEDITOR) {
178 ' . $externalPlugins . '
179 CKEDITOR.replace("' . $fieldId . '", ' . json_encode($configuration) . ');
180 }';
181 }
182
183 /**
184 * Get configuration of external/additional plugins
185 *
186 * @return array
187 */
188 protected function getExtraPlugins(): array
189 {
190 $urlParameters = [
191 'P' => [
192 'table' => $this->data['tableName'],
193 'uid' => $this->data['databaseRow']['uid'],
194 'fieldName' => $this->data['fieldName'],
195 'recordType' => $this->data['recordTypeValue'],
196 'pid' => $this->data['effectivePid'],
197 ]
198 ];
199
200 $pluginConfiguration = [];
201 if (isset($this->rteConfiguration['externalPlugins']) && is_array($this->rteConfiguration['externalPlugins'])) {
202 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
203 foreach ($this->rteConfiguration['externalPlugins'] as $pluginName => $configuration) {
204 $pluginConfiguration[$pluginName] = [
205 'resource' => $this->resolveUrlPath($configuration['resource'])
206 ];
207 unset($configuration['resource']);
208
209 if ($configuration['route']) {
210 $configuration['routeUrl'] = (string)$uriBuilder->buildUriFromRoute($configuration['route'], $urlParameters);
211 }
212
213 $pluginConfiguration[$pluginName]['config'] = $configuration;
214 }
215 }
216 return $pluginConfiguration;
217 }
218
219 /**
220 * Add configuration to replace absolute EXT: paths with relative ones
221 * @param array $configuration
222 *
223 * @return array
224 */
225 protected function replaceAbsolutePathsToRelativeResourcesPath(array $configuration): array
226 {
227 foreach ($configuration as $key => $value) {
228 if (is_array($value)) {
229 $configuration[$key] = $this->replaceAbsolutePathsToRelativeResourcesPath($value);
230 } elseif (is_string($value) && substr($value, 0, 4) === 'EXT:') {
231 $configuration[$key] = $this->resolveUrlPath($value);
232 }
233 }
234 return $configuration;
235 }
236
237 /**
238 * Resolves an EXT: syntax file to an absolute web URL
239 *
240 * @param string $value
241 * @return string
242 */
243 protected function resolveUrlPath(string $value): string
244 {
245 $value = GeneralUtility::getFileAbsFileName($value);
246 return PathUtility::getAbsoluteWebPath($value);
247 }
248
249 /**
250 * Compiles the configuration set from the outside
251 * to have it easily injected into the CKEditor.
252 *
253 * @return array the configuration
254 */
255 protected function prepareConfigurationForEditor(): array
256 {
257 // Ensure custom config is empty so nothing additional is loaded
258 // Of course this can be overriden by the editor configuration below
259 $configuration = [
260 'customConfig' => '',
261 ];
262
263 if (is_array($this->rteConfiguration['config'])) {
264 $configuration = array_replace_recursive($configuration, $this->rteConfiguration['config']);
265 }
266 $configuration['contentsLanguage'] = $this->getLanguageIsoCodeOfContent();
267
268 // replace all paths
269 $configuration = $this->replaceAbsolutePathsToRelativeResourcesPath($configuration);
270
271 // there are some places where we define an array, but it needs to be a list in order to work
272 if (is_array($configuration['extraPlugins'])) {
273 $configuration['extraPlugins'] = implode(',', $configuration['extraPlugins']);
274 }
275 if (is_array($configuration['removePlugins'])) {
276 $configuration['removePlugins'] = implode(',', $configuration['removePlugins']);
277 }
278 if (is_array($configuration['removeButtons'])) {
279 $configuration['removeButtons'] = implode(',', $configuration['removeButtons']);
280 }
281
282 return $configuration;
283 }
284
285 /**
286 * @param string $itemFormElementName
287 * @return string
288 */
289 protected function sanitizeFieldId(string $itemFormElementName): string
290 {
291 $fieldId = preg_replace('/[^a-zA-Z0-9_:.-]/', '_', $itemFormElementName);
292 return htmlspecialchars(preg_replace('/^[^a-zA-Z]/', 'x', $fieldId));
293 }
294 }