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