‪TYPO3CMS  ‪main
PageViewContentObject.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 
45 {
46  protected array ‪$reservedVariables = ['site', 'language', 'page'];
47 
48  public function ‪__construct(
49  protected readonly ‪ContentDataProcessor $contentDataProcessor,
50  protected readonly ‪StandaloneView $view,
51  protected readonly ‪TypoScriptService $typoScriptService,
52  protected readonly ‪PageLayoutResolver $pageLayoutResolver,
53  ) {}
54 
74  public function ‪render($conf = []): string
75  {
76  if (!is_array($conf)) {
77  $conf = [];
78  }
79  $this->view->setRequest($this->request);
80 
81  $this->‪setTemplate($conf);
82  $this->‪assignSettings();
83  $variables = $this->‪getContentObjectVariables($conf);
84  $variables = $this->contentDataProcessor->process($this->cObj, $conf, $variables);
85 
86  $this->view->assignMultiple($variables);
87 
88  return (string)$this->view->render();
89  }
90 
91  protected function ‪setTemplate(array $conf): void
92  {
93  if (is_array($conf['paths.'] ?? false) && $conf['paths.'] !== []) {
94  $this->view->setTemplateRootPaths($conf['paths.']);
95  $this->‪setLayoutPaths();
96  $this->‪setPartialPaths();
97  }
98  // Fetch the Fluid template by the name of the Page Layout and underneath "Pages"
99  $pageInformationObject = $this->request->getAttribute('frontend.page.information');
100  $pageLayoutName = $this->pageLayoutResolver->getLayoutIdentifierForPageWithoutPrefix(
101  $pageInformationObject->getPageRecord(),
102  $pageInformationObject->getRootLine()
103  );
104 
105  $this->view->getRenderingContext()->setControllerAction($pageLayoutName);
106  $this->view->getRenderingContext()->setControllerName('pages');
107  // Also allow an upper case folder as fallback
108  if (!$this->view->hasTemplate()) {
109  $this->view->getRenderingContext()->setControllerName('Pages');
110  }
111  // If template still does not exist, rendering is not possible.
112  if (!$this->view->hasTemplate()) {
113  $configuredTemplateRootPaths = implode(', ', $this->view->getTemplateRootPaths());
115  'Could not find template source for "pages/' . $pageLayoutName . '".'
116  . ' Configured templateRootPaths: ' . $configuredTemplateRootPaths,
117  1711797936
118  );
119  }
120  }
121 
125  protected function ‪setLayoutPaths(): void
126  {
127  // Define the default root paths to be located in the base paths under "layouts/" subfolder
128  // Handle unix paths to allow upper-case folders as well
129  $templateRootPathsLowerCase = array_map(static fn(string $path): string => $path . 'layouts/', $this->view->getTemplateRootPaths());
130  $templateRootPathsUpperCase = array_map(static fn(string $path): string => $path . 'Layouts/', $this->view->getTemplateRootPaths());
131  $layoutPaths = array_merge($templateRootPathsUpperCase, $templateRootPathsLowerCase);
132  if ($layoutPaths !== []) {
133  $this->view->setLayoutRootPaths($layoutPaths);
134  }
135  }
136 
140  protected function ‪setPartialPaths(): void
141  {
142  // Define the default root paths to be located in the base paths under "partials/" subfolder
143  // Handle unix paths to allow upper-case folders as well
144  $templateRootPathsLowerCase = array_map(static fn(string $path): string => $path . 'partials/', $this->view->getTemplateRootPaths());
145  $templateRootPathsUpperCase = array_map(static fn(string $path): string => $path . 'Partials/', $this->view->getTemplateRootPaths());
146  $partialPaths = array_merge($templateRootPathsUpperCase, $templateRootPathsLowerCase);
147  if ($partialPaths !== []) {
148  $this->view->setPartialRootPaths($partialPaths);
149  }
150  }
151 
159  protected function ‪getContentObjectVariables(array $conf): array
160  {
161  $variables = [
162  'site' => $this->request->getAttribute('site'),
163  'language' => $this->request->getAttribute('language'),
164  'page' => new ‪Page($this->request->getAttribute('frontend.page.information')->getPageRecord()),
165  ];
166  // Accumulate the variables to be process and loop them through cObjGetSingle
167  if (is_array($conf['variables.'] ?? false) && $conf['variables.'] !== []) {
168  foreach ($conf['variables.'] as $variableName => $cObjType) {
169  if (!is_string($cObjType)) {
170  continue;
171  }
172  if (in_array($variableName, $this->reservedVariables, true)) {
173  throw new \InvalidArgumentException(
174  'Cannot use reserved name "' . $variableName . '" as variable name in PAGEVIEW.',
175  1711748615
176  );
177  }
178  $cObjConf = $conf['variables.'][$variableName . '.'] ?? [];
179  $variables[$variableName] = $this->cObj->cObjGetSingle($cObjType, $cObjConf, 'variables.' . $variableName);
180  }
181  }
182  return $variables;
183  }
184 
188  protected function ‪assignSettings(): void
189  {
190  $pageSettings = $this->request->getAttribute('frontend.typoscript')->getSettingsTree()->toArray();
191  $pageSettings = $this->typoScriptService->convertTypoScriptArrayToPlainArray($pageSettings);
192  $this->view->assign('settings', $pageSettings);
193  }
194 }
‪TYPO3\CMS\Frontend\ContentObject\PageViewContentObject\assignSettings
‪assignSettings()
Definition: PageViewContentObject.php:188
‪TYPO3\CMS\Core\Domain\Page
Definition: Page.php:24
‪TYPO3\CMS\Frontend\ContentObject\PageViewContentObject\setTemplate
‪setTemplate(array $conf)
Definition: PageViewContentObject.php:91
‪TYPO3\CMS\Frontend\ContentObject\PageViewContentObject\setPartialPaths
‪setPartialPaths()
Definition: PageViewContentObject.php:140
‪TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor
Definition: ContentDataProcessor.php:27
‪TYPO3\CMS\Frontend\ContentObject
Definition: AbstractContentObject.php:18
‪TYPO3\CMS\Core\Page\PageLayoutResolver
Definition: PageLayoutResolver.php:42
‪TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException
Definition: ContentRenderingException.php:24
‪TYPO3\CMS\Frontend\ContentObject\PageViewContentObject\$reservedVariables
‪array $reservedVariables
Definition: PageViewContentObject.php:46
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject
Definition: AbstractContentObject.php:31
‪TYPO3\CMS\Frontend\ContentObject\PageViewContentObject\getContentObjectVariables
‪array getContentObjectVariables(array $conf)
Definition: PageViewContentObject.php:159
‪TYPO3\CMS\Core\TypoScript\TypoScriptService
Definition: TypoScriptService.php:27
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:30
‪TYPO3\CMS\Frontend\ContentObject\PageViewContentObject\render
‪string render($conf=[])
Definition: PageViewContentObject.php:74
‪TYPO3\CMS\Frontend\ContentObject\PageViewContentObject\__construct
‪__construct(protected readonly ContentDataProcessor $contentDataProcessor, protected readonly StandaloneView $view, protected readonly TypoScriptService $typoScriptService, protected readonly PageLayoutResolver $pageLayoutResolver,)
Definition: PageViewContentObject.php:48
‪TYPO3\CMS\Frontend\ContentObject\PageViewContentObject\setLayoutPaths
‪setLayoutPaths()
Definition: PageViewContentObject.php:125
‪TYPO3\CMS\Frontend\ContentObject\PageViewContentObject
Definition: PageViewContentObject.php:45