‪TYPO3CMS  10.4
Richtext.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 
25 
33 {
45  public function ‪getConfiguration(string $table, string $field, int $pid, string $recordType, array $tcaFieldConf): array
46  {
47  // create instance of NodeFactory, ask for "text" element
48  //
49  // As soon as the Data handler starts using FormDataProviders, this class can vanish again, and the hack to
50  // test for specific rich text instances can be dropped: Split the "TcaText" data provider into multiple parts, each
51  // RTE should register and own data provider that does the transformation / configuration providing. This way,
52  // the explicit check for different RTE classes is removed from core and "hooked in" by the RTE's.
53 
54  // The main problem here is that all parameters that the processing needs is handed over to as TSconfig
55  // "dotted array" syntax. We convert at least the processing information available under "processing"
56  // together with pageTS, this way it can be overridden and understood in RteHtmlParser.
57  // However, all other parts of the core will depend on the non-dotted syntax (coming from YAML directly)
58 
59  $pageTs = $this->‪getPageTsConfiguration($table, $field, $pid, $recordType);
60 
61  // determine which preset to use
62  $pageTs['preset'] = $pageTs['fieldSpecificPreset'] ?? $tcaFieldConf['richtextConfiguration'] ?? $pageTs['generalPreset'] ?? 'default';
63  unset($pageTs['fieldSpecificPreset']);
64  unset($pageTs['generalPreset']);
65 
66  // load configuration from preset
67  $configuration = $this->‪loadConfigurationFromPreset($pageTs['preset']);
68 
69  // overlay preset configuration with pageTs
71  $configuration,
72  $this->‪addFlattenedPageTsConfig($pageTs)
73  );
74 
75  // Handle "mode" / "transformation" config when overridden
76  if (!isset($configuration['proc.']['mode']) && !isset($configuration['proc.']['overruleMode'])) {
77  $configuration['proc.']['overruleMode'] = 'default';
78  }
79 
80  return $configuration;
81  }
82 
90  protected function ‪loadConfigurationFromPreset(string $presetName = ''): array
91  {
92  $configuration = [];
93  if (!empty($presetName) && isset(‪$GLOBALS['TYPO3_CONF_VARS']['RTE']['Presets'][$presetName])) {
94  $fileLoader = GeneralUtility::makeInstance(YamlFileLoader::class);
95  $configuration = $fileLoader->load(‪$GLOBALS['TYPO3_CONF_VARS']['RTE']['Presets'][$presetName]);
96  // For future versions, you should however rely on the "processing" key and not the "proc" key.
97  if (is_array($configuration['processing'])) {
98  $configuration['proc.'] = $this->‪convertPlainArrayToTypoScriptArray($configuration['processing']);
99  }
100  }
101  return $configuration;
102  }
103 
110  protected function ‪getRtePageTsConfigOfPid(int $pid): array
111  {
112  return ‪BackendUtility::getPagesTSconfig($pid)['RTE.'] ?? [];
113  }
114 
123  protected function ‪convertPlainArrayToTypoScriptArray(array $plainArray)
124  {
125  $typoScriptArray = [];
126  foreach ($plainArray as $key => $value) {
127  if (is_array($value)) {
128  if (!isset($typoScriptArray[$key])) {
129  $typoScriptArray[$key] = 1;
130  }
131  $typoScriptArray[$key . '.'] = $this->‪convertPlainArrayToTypoScriptArray($value);
132  } else {
133  $typoScriptArray[$key] = $value ?? '';
134  }
135  }
136  return $typoScriptArray;
137  }
138 
147  protected function ‪addFlattenedPageTsConfig(array $typoScriptArray): array
148  {
149  foreach ($typoScriptArray as $key => $data) {
150  if (substr($key, -1) !== '.') {
151  continue;
152  }
154  $typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
155  $typoScriptArray[substr($key, 0, -1)] = $typoScriptService->convertTypoScriptArrayToPlainArray($typoScriptArray[$key]);
156  }
157 
158  return $typoScriptArray;
159  }
160 
172  protected function ‪getPageTsConfiguration(string $table, string $field, int $pid, string $recordType): array
173  {
174  // Load PageTSconfig configuration
175  $fullPageTsConfig = $this->‪getRtePageTsConfigOfPid($pid);
176  $defaultPageTsConfigOverrides = $fullPageTsConfig['default.'] ?? null;
177 
178  $defaultPageTsConfigOverrides['generalPreset'] = $fullPageTsConfig['default.']['preset'] ?? null;
179 
180  $fieldSpecificPageTsConfigOverrides = $fullPageTsConfig['config.'][$table . '.'][$field . '.'] ?? null;
181  unset($fullPageTsConfig['default.'], $fullPageTsConfig['config.']);
182 
183  // First use RTE.*
184  $rtePageTsConfiguration = $fullPageTsConfig;
185 
186  // Then overload with RTE.default.*
187  if (is_array($defaultPageTsConfigOverrides)) {
188  ‪ArrayUtility::mergeRecursiveWithOverrule($rtePageTsConfiguration, $defaultPageTsConfigOverrides);
189  }
190 
191  $rtePageTsConfiguration['fieldSpecificPreset'] = $fieldSpecificPageTsConfigOverrides['types.'][$recordType . '.']['preset'] ??
192  $fieldSpecificPageTsConfigOverrides['preset'] ?? null;
193 
194  // Then overload with RTE.config.tt_content.bodytext
195  if (is_array($fieldSpecificPageTsConfigOverrides)) {
196  $fieldSpecificPageTsConfigOverridesWithoutType = $fieldSpecificPageTsConfigOverrides;
197  unset($fieldSpecificPageTsConfigOverridesWithoutType['types.']);
198  ‪ArrayUtility::mergeRecursiveWithOverrule($rtePageTsConfiguration, $fieldSpecificPageTsConfigOverridesWithoutType);
199 
200  // Then overload with RTE.config.tt_content.bodytext.types.textmedia
201  if (
202  $recordType
203  && isset($fieldSpecificPageTsConfigOverrides['types.'][$recordType . '.'])
204  && is_array($fieldSpecificPageTsConfigOverrides['types.'][$recordType . '.'])
205  ) {
207  $rtePageTsConfiguration,
208  $fieldSpecificPageTsConfigOverrides['types.'][$recordType . '.']
209  );
210  }
211  }
212 
213  unset($rtePageTsConfiguration['preset']);
214 
215  return $rtePageTsConfiguration;
216  }
217 }
‪TYPO3\CMS\Core\Configuration\Richtext\addFlattenedPageTsConfig
‪array addFlattenedPageTsConfig(array $typoScriptArray)
Definition: Richtext.php:147
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:654
‪TYPO3\CMS\Core\Configuration\Richtext\getRtePageTsConfigOfPid
‪array getRtePageTsConfigOfPid(int $pid)
Definition: Richtext.php:110
‪TYPO3\CMS\Core\Configuration\Richtext\convertPlainArrayToTypoScriptArray
‪array convertPlainArrayToTypoScriptArray(array $plainArray)
Definition: Richtext.php:123
‪TYPO3\CMS\Core\Configuration\Richtext\getConfiguration
‪array getConfiguration(string $table, string $field, int $pid, string $recordType, array $tcaFieldConf)
Definition: Richtext.php:45
‪TYPO3\CMS\Backend\Utility\BackendUtility\getPagesTSconfig
‪static array getPagesTSconfig($id)
Definition: BackendUtility.php:698
‪TYPO3\CMS\Core\Configuration\Richtext\getPageTsConfiguration
‪array getPageTsConfiguration(string $table, string $field, int $pid, string $recordType)
Definition: Richtext.php:172
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪TYPO3\CMS\Core\TypoScript\TypoScriptService
Definition: TypoScriptService.php:25
‪TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader
Definition: YamlFileLoader.php:48
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Configuration
Definition: ConfigurationManager.php:16
‪TYPO3\CMS\Core\Configuration\Richtext\loadConfigurationFromPreset
‪array loadConfigurationFromPreset(string $presetName='')
Definition: Richtext.php:90
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Configuration\Richtext
Definition: Richtext.php:33