[TASK] Add resolved site object to FormEngine
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / SiteResolving.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\Exception\SiteNotFoundException;
20 use TYPO3\CMS\Core\Site\PseudoSiteFinder;
21 use TYPO3\CMS\Core\Site\SiteFinder;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * This data provider is used in casual edit record / new record / edit page / new page
26 * scenarios: It find the site object for a page and adds it as 'site' in $result.
27 *
28 * Note this data provider has a loose dependency to DatabaseDefaultLanguagePageRow,
29 * it needs that to determine the correct base pid if localized pages are edited.
30 */
31 class SiteResolving implements FormDataProviderInterface
32 {
33 /**
34 * Find and add site object
35 *
36 * @param array $result
37 * @return array
38 */
39 public function addData(array $result): array
40 {
41 $pageIdDefaultLanguage = $result['defaultLanguagePageRow']['uid'] ?? $result['effectivePid'];
42 try {
43 $result['site'] = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($pageIdDefaultLanguage);
44 } catch (SiteNotFoundException $e) {
45 // Check for a pseudo site
46 $result['site'] = GeneralUtility::makeInstance(PseudoSiteFinder::class)->getSiteByPageId($pageIdDefaultLanguage);
47 }
48 return $result;
49 }
50 }