‪TYPO3CMS  ‪main
PageRendererBackendSetupTrait.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;
27 
45 {
50  ‪PageRenderer $pageRenderer,
51  ‪ExtensionConfiguration $extensionConfiguration,
52  ServerRequestInterface $request,
53  ‪LanguageService $languageService,
54  ): void {
55  $pageRenderer->‪setLanguage($languageService->‪getLocale());
56  $pageRenderer->‪setMetaTag('name', 'viewport', 'width=device-width, initial-scale=1');
57  $pageRenderer->‪setFavIcon($this->getBackendFavicon($extensionConfiguration, $request));
58  $nonce = $request->getAttribute('nonce');
59  if ($nonce !== null) {
60  $pageRenderer->‪setNonce($nonce);
61  $pageRenderer->‪setApplyNonceHint(true);
62  }
63  $this->loadStylesheets($pageRenderer);
64  }
65 
69  protected function ‪loadStylesheets(‪PageRenderer $pageRenderer): void
70  {
71  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['BE']['stylesheets'] ?? [] as $path) {
72  $path = GeneralUtility::getFileAbsFileName($path);
73  if (!$path) {
74  continue;
75  }
76  if (is_dir($path)) {
77  // Path like 'EXT:my_extension/Resources/Public/Css/Backend'
78  foreach (GeneralUtility::getFilesInDir($path, 'css', true) as $cssFile) {
79  $pageRenderer->‪addCssFile($cssFile);
80  }
81  } elseif (file_exists($path)) {
82  // A single file 'EXT:my_extension/Resources/Public/Css/Backend/main.css' or just a single file
83  $pageRenderer->‪addCssFile($path);
84  }
85  }
86  }
87 
91  protected function ‪getBackendFavicon(‪ExtensionConfiguration $extensionConfiguration, ServerRequestInterface $request): string
92  {
93  $backendFavicon = $extensionConfiguration->‪get('backend', 'backendFavicon');
94  if (!empty($backendFavicon)) {
95  return $this->getUriForFileName($request, $backendFavicon);
96  }
97  return ‪PathUtility::getPublicResourceWebPath('EXT:backend/Resources/Public/Icons/favicon.ico');
98  }
99 
108  protected function ‪getUriForFileName(ServerRequestInterface $request, string $filename): string
109  {
110  if (‪PathUtility::hasProtocolAndScheme($filename)) {
111  return $filename;
112  }
113  $urlPrefix = '';
114  if (‪PathUtility::isExtensionPath($filename)) {
115  $filename = ‪PathUtility::getPublicResourceWebPath($filename);
116  } elseif (!str_starts_with($filename, '/')) {
117  $urlPrefix = $this->getNormalizedParams($request)->getSitePath();
118  }
119  return $urlPrefix . $filename;
120  }
121 
122  protected function ‪getNormalizedParams(ServerRequestInterface $request): ‪NormalizedParams
123  {
124  return $request->getAttribute('normalizedParams');
125  }
126 }
‪TYPO3\CMS\Core\Utility\PathUtility\isExtensionPath
‪static isExtensionPath(string $path)
Definition: PathUtility.php:118
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Backend\Template\PageRendererBackendSetupTrait\loadStylesheets
‪loadStylesheets(PageRenderer $pageRenderer)
Definition: PageRendererBackendSetupTrait.php:69
‪TYPO3\CMS\Core\Page\PageRenderer\setFavIcon
‪setFavIcon($favIcon)
Definition: PageRenderer.php:315
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration
Definition: ExtensionConfiguration.php:47
‪TYPO3\CMS\Backend\Template\PageRendererBackendSetupTrait
Definition: PageRendererBackendSetupTrait.php:45
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration\get
‪mixed get(string $extension, string $path='')
Definition: ExtensionConfiguration.php:84
‪TYPO3\CMS\Backend\Template\PageRendererBackendSetupTrait\getUriForFileName
‪string getUriForFileName(ServerRequestInterface $request, string $filename)
Definition: PageRendererBackendSetupTrait.php:108
‪TYPO3\CMS\Core\Page\PageRenderer\setApplyNonceHint
‪setApplyNonceHint(bool $applyNonceHint)
Definition: PageRenderer.php:350
‪TYPO3\CMS\Backend\Template\PageRendererBackendSetupTrait\getNormalizedParams
‪getNormalizedParams(ServerRequestInterface $request)
Definition: PageRendererBackendSetupTrait.php:122
‪TYPO3\CMS\Core\Localization\LanguageService\getLocale
‪getLocale()
Definition: LanguageService.php:286
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:45
‪TYPO3\CMS\Core\Utility\PathUtility\getPublicResourceWebPath
‪static getPublicResourceWebPath(string $resourcePath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:98
‪TYPO3\CMS\Core\Page\PageRenderer\setNonce
‪setNonce(?ConsumableNonce $nonce)
Definition: PageRenderer.php:465
‪TYPO3\CMS\Core\Page\PageRenderer\setLanguage
‪setLanguage(Locale $locale)
Definition: PageRenderer.php:267
‪TYPO3\CMS\Backend\Template
‪TYPO3\CMS\Core\Page\PageRenderer\addCssFile
‪addCssFile($file, $rel='stylesheet', $media='all', $title='', $compress=true, $forceOnTop=false, $allWrap='', $excludeFromConcatenation=false, $splitChar='|', $inline=false, array $tagAttributes=[])
Definition: PageRenderer.php:943
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Template\PageRendererBackendSetupTrait\setUpBasicPageRendererForBackend
‪setUpBasicPageRendererForBackend(PageRenderer $pageRenderer, ExtensionConfiguration $extensionConfiguration, ServerRequestInterface $request, LanguageService $languageService,)
Definition: PageRendererBackendSetupTrait.php:49
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Backend\Template\PageRendererBackendSetupTrait\getBackendFavicon
‪getBackendFavicon(ExtensionConfiguration $extensionConfiguration, ServerRequestInterface $request)
Definition: PageRendererBackendSetupTrait.php:91
‪TYPO3\CMS\Core\Utility\PathUtility\hasProtocolAndScheme
‪static hasProtocolAndScheme(string $path)
Definition: PathUtility.php:446
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Page\PageRenderer\setMetaTag
‪setMetaTag(string $type, string $name, string $content, array $subProperties=[], $replace=true)
Definition: PageRenderer.php:629
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:38