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