TYPO3 CMS  TYPO3_8-7
Richtext.php
Go to the documentation of this file.
1 <?php
2 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 
24 
31 class Richtext
32 {
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
68  $configuration,
69  $this->addFlattenedPageTsConfig($pageTs)
70  );
71 
72  // Handle "mode" / "transformation" config when overridden
73  if (isset($configuration['proc.']['overruleMode']) && $configuration['proc.']['overruleMode'] === 'ts_css') {
74  // Change legacy 'ts_css' to 'default'
75  $configuration['proc.']['overruleMode'] = 'default';
76  } elseif (!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  // Override with pageTs if needed
113  $backendUser = $this->getBackendUser();
114  return $backendUser->getTSConfig('RTE', BackendUtility::getPagesTSconfig($pid));
115  }
116 
125  protected function convertPlainArrayToTypoScriptArray(array $plainArray)
126  {
127  $typoScriptArray = [];
128  foreach ($plainArray as $key => $value) {
129  if (is_array($value)) {
130  if (!isset($typoScriptArray[$key])) {
131  $typoScriptArray[$key] = 1;
132  }
133  $typoScriptArray[$key . '.'] = $this->convertPlainArrayToTypoScriptArray($value);
134  } else {
135  $typoScriptArray[$key] = is_null($value) ? '' : $value;
136  }
137  }
138  return $typoScriptArray;
139  }
140 
145  {
146  return $GLOBALS['BE_USER'];
147  }
148 
157  protected function addFlattenedPageTsConfig(array $typoScriptArray): array
158  {
159  foreach ($typoScriptArray as $key => $data) {
160  if (substr($key, -1) !== '.') {
161  continue;
162  }
164  $typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
165  $typoScriptArray[substr($key, 0, -1)] = $typoScriptService->convertTypoScriptArrayToPlainArray($typoScriptArray[$key]);
166  }
167 
168  return $typoScriptArray;
169  }
170 
182  protected function getPageTsConfiguration(string $table, string $field, int $pid, string $recordType): array
183  {
184  // Load PageTSconfig configuration
185  $fullPageTsConfig = $this->getRtePageTsConfigOfPid($pid);
186  $fullPageTsConfig = !empty($fullPageTsConfig['properties']) ? $fullPageTsConfig['properties'] : [];
187  $defaultPageTsConfigOverrides = isset($fullPageTsConfig['default.']) ? $fullPageTsConfig['default.'] : null;
188  $fieldSpecificPageTsConfigOverrides = isset($fullPageTsConfig['config.'][$table . '.'][$field . '.']) ? $fullPageTsConfig['config.'][$table . '.'][$field . '.'] : null;
189  unset($fullPageTsConfig['default.'], $fullPageTsConfig['config.']);
190 
191  // First use RTE.*
192  $rtePageTsConfiguration = $fullPageTsConfig;
193 
194  // Then overload with RTE.default.*
195  if (is_array($defaultPageTsConfigOverrides)) {
196  ArrayUtility::mergeRecursiveWithOverrule($rtePageTsConfiguration, $defaultPageTsConfigOverrides);
197  }
198 
199  // Then overload with RTE.config.tt_content.bodytext
200  if (is_array($fieldSpecificPageTsConfigOverrides)) {
201  $fieldSpecificPageTsConfigOverridesWithoutType = $fieldSpecificPageTsConfigOverrides;
202  unset($fieldSpecificPageTsConfigOverridesWithoutType['types.']);
203  ArrayUtility::mergeRecursiveWithOverrule($rtePageTsConfiguration, $fieldSpecificPageTsConfigOverridesWithoutType);
204 
205  // Then overload with RTE.config.tt_content.bodytext.types.textmedia
206  if (
207  $recordType
208  && isset($fieldSpecificPageTsConfigOverrides['types.'][$recordType . '.'])
209  && is_array($fieldSpecificPageTsConfigOverrides['types.'][$recordType . '.'])
210  ) {
212  $rtePageTsConfiguration,
213  $fieldSpecificPageTsConfigOverrides['types.'][$recordType . '.']
214  );
215  }
216  }
217 
218  return $rtePageTsConfiguration;
219  }
220 }
static getPagesTSconfig($id, $rootLine=null, $returnPartArray=false)
getConfiguration(string $table, string $field, int $pid, string $recordType, array $tcaFieldConf)
Definition: Richtext.php:44
static makeInstance($className,... $constructorArguments)
getPageTsConfiguration(string $table, string $field, int $pid, string $recordType)
Definition: Richtext.php:182
loadConfigurationFromPreset(string $presetName='')
Definition: Richtext.php:90
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
convertPlainArrayToTypoScriptArray(array $plainArray)
Definition: Richtext.php:125
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']