2 declare(strict_types
=1);
3 namespace TYPO3\CMS\Core\Configuration
;
6 * This file is part of the TYPO3 CMS project.
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.
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
15 * The TYPO3 project - inspiring people to share!
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
;
26 * Prepare richtext configuration. Used in DataHandler and FormEngine
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
34 * This is an intermediate class / method to retrieve RTE
35 * configuration until all core places use data providers to do that.
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
44 public function getConfiguration(string $table, string $field, int $pid, string $recordType, array $tcaFieldConf): array
46 // create instance of NodeFactory, ask for "text" element
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.
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)
58 $pageTs = $this->getPageTsConfiguration($table, $field, $pid, $recordType);
60 // determine which preset to use
61 $usePreset = $pageTs['preset'] ??
$tcaFieldConf['richtextConfiguration'] ??
'default';
63 // load configuration from preset
64 $configuration = $this->loadConfigurationFromPreset($usePreset);
66 // overlay preset configuration with pageTs
67 ArrayUtility
::mergeRecursiveWithOverrule(
69 $this->cleanDotsFromEditorConfigKeys($pageTs)
72 // Handle "mode" / "transformation" config when overridden
73 if (!isset($configuration['proc.']['mode']) && !isset($configuration['proc.']['overruleMode'])) {
74 $configuration['proc.']['overruleMode'] = 'default';
77 return $configuration;
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.
84 * @param string $presetName
85 * @return array the parsed configuration
87 protected function loadConfigurationFromPreset(string $presetName = ''): array
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']);
98 return $configuration;
102 * Return RTE section of page TS
104 * @param int $pid Page ts of given pid
105 * @return array RTE section of pageTs of given pid
107 protected function getRtePageTsConfigOfPid(int $pid): array
109 // Override with pageTs if needed
110 $backendUser = $this->getBackendUser();
111 return $backendUser->getTSConfig('RTE', BackendUtility
::getPagesTSconfig($pid));
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
119 * @param array $plainArray An array
120 * @return array array with TypoScript as usual (with dot)
122 protected function convertPlainArrayToTypoScriptArray(array $plainArray)
124 $typoScriptArray = [];
125 foreach ($plainArray as $key => $value) {
126 if (is_array($value)) {
127 if (!isset($typoScriptArray[$key])) {
128 $typoScriptArray[$key] = 1;
130 $typoScriptArray[$key . '.'] = $this->convertPlainArrayToTypoScriptArray($value);
132 $typoScriptArray[$key] = is_null($value) ?
'' : $value;
135 return $typoScriptArray;
139 * @return BackendUserAuthentication
141 protected function getBackendUser() : BackendUserAuthentication
143 return $GLOBALS['BE_USER'];
147 * Strip dots from the 'editor.' part of a given TypoScript array
149 * @param array $typoScriptArray TypoScriptArray (with dots) that may contain an 'editor.' subarray
150 * @return array array with dots stripped from the editor subarray
152 protected function cleanDotsFromEditorConfigKeys(array $typoScriptArray): array
154 if (isset($typoScriptArray['editor.'])) {
155 /** @var TypoScriptService $typoScriptService */
156 $typoScriptService = GeneralUtility
::makeInstance(TypoScriptService
::class);
157 $typoScriptArray['editor'] = $typoScriptService->convertTypoScriptArrayToPlainArray($typoScriptArray['editor.']);
158 unset($typoScriptArray['editor.']);
161 return $typoScriptArray;
165 * Load PageTS configuration for the RTE
167 * Return RTE section of page TS, taking into account overloading via table, field and record type
169 * @param string $table The table the field is in
170 * @param string $field Field name
171 * @param int $pid Real page id
172 * @param string $recordType Record type value
175 protected function getPageTsConfiguration(string $table, string $field, int $pid, string $recordType) : array
177 // Load PageTSconfig configuration
178 $fullPageTsConfig = $this->getRtePageTsConfigOfPid($pid);
179 $fullPageTsConfig = !empty($fullPageTsConfig['properties']) ?
$fullPageTsConfig['properties'] : [];
180 $defaultPageTsConfigOverrides = isset($fullPageTsConfig['default.']) ?
$fullPageTsConfig['default.'] : null;
181 $fieldSpecificPageTsConfigOverrides = isset($fullPageTsConfig['config.'][$table . '.'][$field . '.']) ?
$fullPageTsConfig['config.'][$table . '.'][$field . '.'] : null;
182 unset($fullPageTsConfig['default.'], $fullPageTsConfig['config.']);
185 $rtePageTsConfiguration = $fullPageTsConfig;
187 // Then overload with RTE.default.*
188 if (is_array($defaultPageTsConfigOverrides)) {
189 ArrayUtility
::mergeRecursiveWithOverrule($rtePageTsConfiguration, $defaultPageTsConfigOverrides);
192 // Then overload with RTE.config.tt_content.bodytext
193 if (is_array($fieldSpecificPageTsConfigOverrides)) {
194 $fieldSpecificPageTsConfigOverridesWithoutType = $fieldSpecificPageTsConfigOverrides;
195 unset($fieldSpecificPageTsConfigOverridesWithoutType['types.']);
196 ArrayUtility
::mergeRecursiveWithOverrule($rtePageTsConfiguration, $fieldSpecificPageTsConfigOverridesWithoutType);
198 // Then overload with RTE.config.tt_content.bodytext.types.textmedia
201 && isset($fieldSpecificPageTsConfigOverrides['types.'][$recordType . '.'])
202 && is_array($fieldSpecificPageTsConfigOverrides['types.'][$recordType . '.'])
204 ArrayUtility
::mergeRecursiveWithOverrule(
205 $rtePageTsConfiguration,
206 $fieldSpecificPageTsConfigOverrides['types.'][$recordType . '.']
211 return $rtePageTsConfiguration;