‪TYPO3CMS  ‪main
PageLayoutResolver.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 
18 namespace ‪TYPO3\CMS\Core\Page;
19 
26 
42 {
43  public function ‪__construct(
44  protected readonly ‪DataProviderCollection $dataProviderCollection,
45  protected readonly ‪SiteFinder $siteFinder,
46  protected readonly ‪PageTsConfigFactory $pageTsConfigFactory
47  ) {
48  $this->dataProviderCollection->add('default', DefaultDataProvider::class);
49  foreach ((array)(‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['BackendLayoutDataProvider'] ?? []) as ‪$identifier => $className) {
50  $this->dataProviderCollection->add(‪$identifier, $className);
51  }
52  }
53 
54  public function ‪getLayoutForPage(array $pageRecord, array $rootLine): ?‪PageLayout
55  {
56  $pageId = (int)$pageRecord['uid'];
57  $site = $this->siteFinder->getSiteByPageId($pageId, $rootLine);
58  $pageTsConfig = $this->pageTsConfigFactory->create($rootLine, $site);
59 
60  $dataProviderContext = GeneralUtility::makeInstance(DataProviderContext::class);
61  $dataProviderContext
62  ->setPageId($pageId)
63  ->setData($pageRecord)
64  ->setTableName('pages')
65  ->setFieldName('backend_layout')
66  ->setPageTsConfig($pageTsConfig->getPageTsConfigArray());
67 
68  $selectedPageLayout = $this->‪getLayoutIdentifierForPage($pageRecord, $rootLine);
69  $layout = $this->dataProviderCollection->getBackendLayout($selectedPageLayout, $pageId);
70 
71  if ($layout === null) {
72  return null;
73  }
74 
75  $fullStructure = $layout->getStructure()['__config'];
76  $contentAreas = [];
77  // find all arrays recursively from , where one of the columns within the array is called "colPos"
78  $findColPos = function (array $structure) use (&$findColPos, &$contentAreas) {
79  if (isset($structure['colPos'])) {
80  unset($structure['colspan'], $structure['rowspan']);
81  $contentAreas[] = $structure;
82  }
83  foreach ($structure as $value) {
84  if (is_array($value)) {
85  $findColPos($value);
86  }
87  }
88  };
89  $findColPos($fullStructure);
90 
91  return new ‪PageLayout($layout->getIdentifier(), $layout->getTitle(), $contentAreas, $layout->getStructure());
92  }
93 
102  public function ‪getLayoutIdentifierForPage(array $page, array $rootLine): string
103  {
104  $selectedLayout = $page['backend_layout'] ?? '';
105 
106  // If it is set to "none" - don't use any
107  if ($selectedLayout === '-1') {
108  return 'none';
109  }
110 
111  if ($selectedLayout === '' || $selectedLayout === '0') {
112  // If it not set check the root-line for a layout on next level and use this
113  // Remove first element, which is the current page
114  // See also \TYPO3\CMS\Backend\View\BackendLayoutView::getSelectedCombinedIdentifier()
115  array_shift($rootLine);
116  foreach ($rootLine as $rootLinePage) {
117  $selectedLayout = (string)($rootLinePage['backend_layout_next_level'] ?? '');
118  // If layout for "next level" is set to "none" - don't use any and stop searching
119  if ($selectedLayout === '-1') {
120  $selectedLayout = 'none';
121  break;
122  }
123  if ($selectedLayout !== '' && $selectedLayout !== '0') {
124  // Stop searching if a layout for "next level" is set
125  break;
126  }
127  }
128  }
129  if ($selectedLayout === '0' || $selectedLayout === '') {
130  $selectedLayout = 'default';
131  }
132  return $selectedLayout;
133  }
134 
135  public function ‪getLayoutIdentifierForPageWithoutPrefix(array $page, array $rootLine): string
136  {
137  $selectedLayout = $this->‪getLayoutIdentifierForPage($page, $rootLine);
138  if (str_contains($selectedLayout, '__')) {
139  return explode('__', $selectedLayout, 2)[1] ?? '';
140  }
141  return $selectedLayout;
142  }
143 }
‪TYPO3\CMS\Core\Page\PageLayoutResolver\getLayoutIdentifierForPage
‪getLayoutIdentifierForPage(array $page, array $rootLine)
Definition: PageLayoutResolver.php:102
‪TYPO3\CMS\Core\Page\PageLayout
Definition: PageLayout.php:27
‪TYPO3\CMS\Core\Page\PageLayoutResolver\getLayoutIdentifierForPageWithoutPrefix
‪getLayoutIdentifierForPageWithoutPrefix(array $page, array $rootLine)
Definition: PageLayoutResolver.php:135
‪TYPO3\CMS\Core\Page\PageLayoutResolver
Definition: PageLayoutResolver.php:42
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Core\Page\PageLayoutResolver\__construct
‪__construct(protected readonly DataProviderCollection $dataProviderCollection, protected readonly SiteFinder $siteFinder, protected readonly PageTsConfigFactory $pageTsConfigFactory)
Definition: PageLayoutResolver.php:43
‪TYPO3\CMS\Core\Page
Definition: AssetCollector.php:18
‪TYPO3\CMS\Backend\View\BackendLayout\DataProviderCollection
Definition: DataProviderCollection.php:27
‪TYPO3\CMS\Core\Page\PageLayoutResolver\getLayoutForPage
‪getLayoutForPage(array $pageRecord, array $rootLine)
Definition: PageLayoutResolver.php:54
‪TYPO3\CMS\Core\TypoScript\PageTsConfigFactory
Definition: PageTsConfigFactory.php:44
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\View\BackendLayout\DataProviderContext
Definition: DataProviderContext.php:26
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Backend\View\BackendLayout\DefaultDataProvider
Definition: DefaultDataProvider.php:34