[BUGFIX] Allow global RTE config again by PageTSConfig
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Configuration / Richtext.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Configuration;
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\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader;
20 use TYPO3\CMS\Core\TypoScript\TypoScriptService;
21 use TYPO3\CMS\Core\Utility\ArrayUtility;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * Prepare richtext configuration. Used in DataHandler and FormEngine
26 *
27 * @internal Internal class for the time being - may change / vanish any time
28 * @todo When I grow up, I want to become a data provider
29 */
30 class Richtext
31 {
32 /**
33 * This is an intermediate class / method to retrieve RTE
34 * configuration until all core places use data providers to do that.
35 *
36 * @param string $table The table the field is in
37 * @param string $field Field name
38 * @param int $pid Real page id
39 * @param string $recordType Record type value
40 * @param array $tcaFieldConf ['config'] section of TCA field
41 * @return array
42 */
43 public function getConfiguration(string $table, string $field, int $pid, string $recordType, array $tcaFieldConf): array
44 {
45 // create instance of NodeFactory, ask for "text" element
46 //
47 // As soon an the Data handler starts using FormDataProviders, this class can vanish again, and the hack to
48 // test for specific rich text instances can be dropped: Split the "TcaText" data provider into multiple parts, each
49 // RTE should register and own data provider that does the transformation / configuration providing. This way,
50 // the explicit check for different RTE classes is removed from core and "hooked in" by the RTE's.
51
52 // The main problem here is that all parameters that the processing needs is handed over to as TSconfig
53 // "dotted array" syntax. We convert at least the processing information available under "processing"
54 // together with pageTS, this way it can be overridden and understood in RteHtmlParser.
55 // However, all other parts of the core will depend on the non-dotted syntax (coming from YAML directly)
56
57 $pageTs = $this->getPageTsConfiguration($table, $field, $pid, $recordType);
58
59 // determine which preset to use
60 $usePreset = $pageTs['preset'] ?? $tcaFieldConf['richtextConfiguration'] ?? 'default';
61
62 // load configuration from preset
63 $configuration = $this->loadConfigurationFromPreset($usePreset);
64
65 // overlay preset configuration with pageTs
66 ArrayUtility::mergeRecursiveWithOverrule(
67 $configuration,
68 $this->addFlattenedPageTsConfig($pageTs)
69 );
70
71 // Handle "mode" / "transformation" config when overridden
72 if (!isset($configuration['proc.']['mode']) && !isset($configuration['proc.']['overruleMode'])) {
73 $configuration['proc.']['overruleMode'] = 'default';
74 }
75
76 return $configuration;
77 }
78
79 /**
80 * Load a configuration preset from an external resource (currently only YAML is supported).
81 * This is the default behaviour and can be overridden by pageTSconfig.
82 *
83 * @param string $presetName
84 * @return array the parsed configuration
85 */
86 protected function loadConfigurationFromPreset(string $presetName = ''): array
87 {
88 $configuration = [];
89 if (!empty($presetName) && isset($GLOBALS['TYPO3_CONF_VARS']['RTE']['Presets'][$presetName])) {
90 $fileLoader = GeneralUtility::makeInstance(YamlFileLoader::class);
91 $configuration = $fileLoader->load($GLOBALS['TYPO3_CONF_VARS']['RTE']['Presets'][$presetName]);
92 // For future versions, you should however rely on the "processing" key and not the "proc" key.
93 if (is_array($configuration['processing'])) {
94 $configuration['proc.'] = $this->convertPlainArrayToTypoScriptArray($configuration['processing']);
95 }
96 }
97 return $configuration;
98 }
99
100 /**
101 * Return RTE section of page TS
102 *
103 * @param int $pid Page ts of given pid
104 * @return array RTE section of pageTs of given pid
105 */
106 protected function getRtePageTsConfigOfPid(int $pid): array
107 {
108 return BackendUtility::getPagesTSconfig($pid)['RTE.'] ?? [];
109 }
110
111 /**
112 * Returns an array with Typoscript the old way (with dot)
113 * Since the functionality in YAML is without the dots, but the new configuration is used without the dots
114 * this functionality adds also an explicit = 1 to the arrays
115 *
116 * @param array $plainArray An array
117 * @return array array with TypoScript as usual (with dot)
118 */
119 protected function convertPlainArrayToTypoScriptArray(array $plainArray)
120 {
121 $typoScriptArray = [];
122 foreach ($plainArray as $key => $value) {
123 if (is_array($value)) {
124 if (!isset($typoScriptArray[$key])) {
125 $typoScriptArray[$key] = 1;
126 }
127 $typoScriptArray[$key . '.'] = $this->convertPlainArrayToTypoScriptArray($value);
128 } else {
129 $typoScriptArray[$key] = $value === null ? '' : $value;
130 }
131 }
132 return $typoScriptArray;
133 }
134
135 /**
136 * Add all PageTS.RTE options keys to configuration without dots
137 *
138 * We need to keep the dotted keys for backwards compatibility like ext:rtehtmlarea
139 *
140 * @param array $typoScriptArray TypoScriptArray
141 * @return array array with config without dots added
142 */
143 protected function addFlattenedPageTsConfig(array $typoScriptArray): array
144 {
145 foreach ($typoScriptArray as $key => $data) {
146 if (substr($key, -1) !== '.') {
147 continue;
148 }
149 /** @var TypoScriptService $typoScriptService */
150 $typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
151 $typoScriptArray[substr($key, 0, -1)] = $typoScriptService->convertTypoScriptArrayToPlainArray($typoScriptArray[$key]);
152 }
153
154 return $typoScriptArray;
155 }
156
157 /**
158 * Load PageTS configuration for the RTE
159 *
160 * Return RTE section of page TS, taking into account overloading via table, field and record type
161 *
162 * @param string $table The table the field is in
163 * @param string $field Field name
164 * @param int $pid Real page id
165 * @param string $recordType Record type value
166 * @return array
167 */
168 protected function getPageTsConfiguration(string $table, string $field, int $pid, string $recordType): array
169 {
170 // Load PageTSconfig configuration
171 $fullPageTsConfig = $this->getRtePageTsConfigOfPid($pid);
172 $defaultPageTsConfigOverrides = $fullPageTsConfig['default.'] ?? null;
173 $fieldSpecificPageTsConfigOverrides = $fullPageTsConfig['config.'][$table . '.'][$field . '.'] ?? null;
174 unset($fullPageTsConfig['default.'], $fullPageTsConfig['config.']);
175
176 // First use RTE.*
177 $rtePageTsConfiguration = $fullPageTsConfig;
178
179 // Then overload with RTE.default.*
180 if (is_array($defaultPageTsConfigOverrides)) {
181 ArrayUtility::mergeRecursiveWithOverrule($rtePageTsConfiguration, $defaultPageTsConfigOverrides);
182 }
183
184 // Then overload with RTE.config.tt_content.bodytext
185 if (is_array($fieldSpecificPageTsConfigOverrides)) {
186 $fieldSpecificPageTsConfigOverridesWithoutType = $fieldSpecificPageTsConfigOverrides;
187 unset($fieldSpecificPageTsConfigOverridesWithoutType['types.']);
188 ArrayUtility::mergeRecursiveWithOverrule($rtePageTsConfiguration, $fieldSpecificPageTsConfigOverridesWithoutType);
189
190 // Then overload with RTE.config.tt_content.bodytext.types.textmedia
191 if (
192 $recordType
193 && isset($fieldSpecificPageTsConfigOverrides['types.'][$recordType . '.'])
194 && is_array($fieldSpecificPageTsConfigOverrides['types.'][$recordType . '.'])
195 ) {
196 ArrayUtility::mergeRecursiveWithOverrule(
197 $rtePageTsConfiguration,
198 $fieldSpecificPageTsConfigOverrides['types.'][$recordType . '.']
199 );
200 }
201 }
202
203 return $rtePageTsConfiguration;
204 }
205 }