[FEATURE] Allow multiple domain variants per site
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / SiteDatabaseEditRow.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Form\FormDataProvider;
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\Form\FormDataProviderInterface;
19 use TYPO3\CMS\Core\Site\SiteFinder;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * Special data provider for the sites configuration module.
24 *
25 * Fetch "row" data from yml file and set as 'databaseRow'
26 */
27 class SiteDatabaseEditRow implements FormDataProviderInterface
28 {
29 /**
30 * First level of ['customData']['siteData'] to ['databaseRow']
31 *
32 * @param array $result
33 * @return array
34 * @throws \RuntimeException
35 */
36 public function addData(array $result): array
37 {
38 if ($result['command'] !== 'edit' || !empty($result['databaseRow'])) {
39 return $result;
40 }
41
42 $tableName = $result['tableName'];
43 $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
44 if ($tableName === 'site') {
45 $siteConfigurationForPageUid = (int)$result['vanillaUid'];
46 $rowData = $siteFinder->getSiteByRootPageId($siteConfigurationForPageUid)->getConfiguration();
47 $result['databaseRow']['uid'] = $rowData['rootPageId'];
48 $result['databaseRow']['identifier'] = $result['customData']['siteIdentifier'];
49 } elseif (in_array($tableName, ['site_errorhandling', 'site_language', 'site_route', 'site_base_variant'], true)) {
50 $siteConfigurationForPageUid = (int)($result['inlineTopMostParentUid'] ?? $result['inlineParentUid']);
51 $rowData = $siteFinder->getSiteByRootPageId($siteConfigurationForPageUid)->getConfiguration();
52 $parentFieldName = $result['inlineParentFieldName'];
53 if (!isset($rowData[$parentFieldName])) {
54 throw new \RuntimeException('Field "' . $parentFieldName . '" not found', 1520886092);
55 }
56 $rowData = $rowData[$parentFieldName][$result['vanillaUid']];
57 $result['databaseRow']['uid'] = $result['vanillaUid'];
58 } else {
59 throw new \RuntimeException('Other tables not implemented', 1520886234);
60 }
61
62 foreach ($rowData as $fieldName => $value) {
63 // Flat values only - databaseRow has no "tree"
64 if (!is_array($value)) {
65 $result['databaseRow'][$fieldName] = $value;
66 }
67 }
68 // All "records" are always on pid 0
69 $result['databaseRow']['pid'] = 0;
70 return $result;
71 }
72 }