‪TYPO3CMS  ‪main
BackendViewFactory.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 
20 use Psr\Http\Message\ServerRequestInterface;
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
23 use TYPO3\CMS\Core\Package\PackageManager;
27 use ‪TYPO3\CMS\Core\View\ViewInterface as CoreViewInterface;
29 use TYPO3Fluid\Fluid\View\TemplateView as FluidTemplateView;
30 
35 {
36  public function ‪__construct(
37  protected readonly ‪RenderingContextFactory $renderingContextFactory,
38  protected readonly PackageManager $packageManager,
39  ) {}
40 
45  public function ‪create(ServerRequestInterface $request, array $packageNames = []): CoreViewInterface
46  {
47  if (empty($packageNames)) {
48  // Extensions *may* provide path lookup package names as second argument. In most cases, this is not
49  // needed, and the package name will be fetched from current route. However, there are scenarios
50  // where extensions 'hook' into existing functionality of a different extension that defined a
51  // route, and then deliver own templates from the own extension. In those cases, they need to
52  // supply an additional base package name.
53  // Examples are backend toolbar items: The toolbar items are rendered through a typo3/cms-backend
54  // route, so this is picked as base from the route. workspaces delivers an additional toolbar item,
55  // so 'typo3/cms-workspaces' needs to be added as additional path to look up. The dashboard extension
56  // and FormEngine have similar cases.
58  $route = $request->getAttribute('route');
59  $packageNameFromRoute = $route->getOption('packageName');
60  if (!empty($packageNameFromRoute)) {
61  $packageNames[] = $packageNameFromRoute;
62  }
63  }
64  // Always add EXT:backend/Resources/Private/ as first default path to resolve
65  // default Layouts/Module.html and its partials.
66  if (!in_array('typo3/cms-backend', $packageNames, true)) {
67  array_unshift($packageNames, 'typo3/cms-backend');
68  }
69 
70  // @todo: This assumes the pageId is *always* given as 'id' in request.
71  // @todo: It would be cool if a middleware adds final pageTS - already overlayed by userTS - as attribute to request, to use it here.
72  $pageTs = [];
73  $pageId = $request->getParsedBody()['id'] ?? $request->getQueryParams()['id'] ?? 0;
75  // Some BE controllers misuse the 'id' argument for something else than the page-uid (especially filelist module).
76  // We check if 'id' is an integer here to skip pageTsConfig calculation if that is the case.
77  // @todo: Mid-term, misuses should vanish, making 'id' a Backend convention. Affected is
78  // at least ext:filelist, plus record linking modals that use 'pid'.
79  $pageTs = BackendUtility::getPagesTSconfig((int)$pageId);
80  }
81 
82  $templatePaths = [
83  'templateRootPaths' => [],
84  'layoutRootPaths' => [],
85  'partialRootPaths' => [],
86  ];
87  foreach ($packageNames as $packageName) {
88  // Add paths for package.
89  $packagePath = $this->packageManager->getPackage($packageName)->getPackagePath();
90  $templatePaths['templateRootPaths'][] = $packagePath . 'Resources/Private/Templates';
91  $templatePaths['layoutRootPaths'][] = $packagePath . 'Resources/Private/Layouts';
92  $templatePaths['partialRootPaths'][] = $packagePath . 'Resources/Private/Partials';
93  // Add possible overrides.
94  if (is_array($pageTs['templates.'][$packageName . '.'] ?? false)) {
95  $overrides = $pageTs['templates.'][$packageName . '.'];
96  ksort($overrides);
97  foreach ($overrides as $override) {
98  $pathParts = GeneralUtility::trimExplode(':', $override, true);
99  if (count($pathParts) < 2) {
100  throw new \RuntimeException(
101  'When overriding template paths, the syntax is "composer-package-name:path", example: "typo3/cms-seo:Resources/Private/TemplateOverrides/typo3/cms-backend"',
102  1643798660
103  );
104  }
105  $composerPackageName = $pathParts[0];
106  $overridePackagePath = $this->packageManager->getPackage($composerPackageName)->getPackagePath();
107  $overridePath = rtrim($pathParts[1], '/');
108  $templatePaths['templateRootPaths'][] = $overridePackagePath . $overridePath . '/Templates';
109  $templatePaths['layoutRootPaths'][] = $overridePackagePath . $overridePath . '/Layouts';
110  $templatePaths['partialRootPaths'][] = $overridePackagePath . $overridePath . '/Partials';
111  }
112  }
113  }
114 
115  $renderingContext = $this->renderingContextFactory->create($templatePaths);
116  $renderingContext->setRequest($request);
117  $fluidView = new FluidTemplateView($renderingContext);
118  return new ‪FluidViewAdapter($fluidView);
119  }
120 }
‪TYPO3\CMS\Core\View\ViewInterface
Definition: ViewInterface.php:24
‪TYPO3\CMS\Backend\View\BackendViewFactory
Definition: BackendViewFactory.php:35
‪TYPO3\CMS\Backend\View
Definition: AuthenticationStyleInformation.php:18
‪TYPO3\CMS\Core\View\FluidViewAdapter
Definition: FluidViewAdapter.php:28
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Backend\View\BackendViewFactory\create
‪create(ServerRequestInterface $request, array $packageNames=[])
Definition: BackendViewFactory.php:45
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory
Definition: RenderingContextFactory.php:51
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\View\BackendViewFactory\__construct
‪__construct(protected readonly RenderingContextFactory $renderingContextFactory, protected readonly PackageManager $packageManager,)
Definition: BackendViewFactory.php:36