‪TYPO3CMS  ‪main
TemplatePaths.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\Fluid\View;
19 
20 use Psr\Http\Message\ServerRequestInterface;
28 
36 class ‪TemplatePaths extends \TYPO3Fluid\Fluid\View\TemplatePaths
37 {
41  protected ‪$templateSource;
42 
47 
48  protected function ‪getExtensionPrivateResourcesPath(string $extensionKey): ?string
49  {
50  $extensionKey = trim($extensionKey);
51  if ($extensionKey && ‪ExtensionManagementUtility::isLoaded($extensionKey)) {
52  return ‪ExtensionManagementUtility::extPath($extensionKey) . 'Resources/Private/';
53  }
54  return null;
55  }
56 
58  {
59  return GeneralUtility::makeInstance(ConfigurationManager::class);
60  }
61 
62  protected function ‪getContextSpecificViewConfiguration(string $extensionKey): array
63  {
64  if (empty($extensionKey)) {
65  return [];
66  }
67 
68  $resources = $this->‪getExtensionPrivateResourcesPath($extensionKey);
69  $paths = [
70  self::CONFIG_TEMPLATEROOTPATHS => [$resources . 'Templates/'],
71  self::CONFIG_PARTIALROOTPATHS => [$resources . 'Partials/'],
72  self::CONFIG_LAYOUTROOTPATHS => [$resources . 'Layouts/'],
73  ];
74 
75  $configuredPaths = [];
76  if (!empty($this->templateRootPaths) || !empty($this->partialRootPaths) || !empty($this->layoutRootPaths)) {
77  // The view was configured already
78  $configuredPaths = [
79  self::CONFIG_TEMPLATEROOTPATHS => $this->templateRootPaths,
80  self::CONFIG_PARTIALROOTPATHS => $this->partialRootPaths,
81  self::CONFIG_LAYOUTROOTPATHS => $this->layoutRootPaths,
82  ];
83  } else {
85  $signature = str_replace('_', '', $extensionKey);
86  if ($this->‪isBackendMode() && isset($typoScript['module.']['tx_' . $signature . '.']['view.'])) {
87  $configuredPaths = (array)$typoScript['module.']['tx_' . $signature . '.']['view.'];
88  $configuredPaths = GeneralUtility::removeDotsFromTS($configuredPaths);
89  } elseif ($this->‪isFrontendMode() && isset($typoScript['plugin.']['tx_' . $signature . '.']['view.'])) {
90  $configuredPaths = (array)$typoScript['plugin.']['tx_' . $signature . '.']['view.'];
91  $configuredPaths = GeneralUtility::removeDotsFromTS($configuredPaths);
92  }
93  }
94 
95  if (empty($configuredPaths)) {
96  return $paths;
97  }
98 
99  foreach ($paths as ‪$name => $defaultPaths) {
100  if (!empty($configuredPaths[‪$name])) {
101  $configured = ArrayUtility::sortArrayWithIntegerKeys((array)$configuredPaths[‪$name]);
102  // calculate implicit default paths which have not been explicitly configured
103  $implicitDefaultPaths = array_diff($defaultPaths, $configured);
104  // prepend implicit default paths (which have not been found in configured paths), as fallbacks
105  $paths[‪$name] = array_merge($implicitDefaultPaths, $configured);
106  }
107  }
108 
109  return $paths;
110  }
111 
119  public function ‪fillDefaultsByPackageName($packageName): void
120  {
121  $this->fillFromConfigurationArray($this->‪getContextSpecificViewConfiguration($packageName));
122  }
123 
127  public function ‪setTemplateRootPaths(array $templateRootPaths): void
128  {
129  parent::setTemplateRootPaths(
130  ArrayUtility::sortArrayWithIntegerKeys($templateRootPaths)
131  );
132  }
133 
137  public function ‪setLayoutRootPaths(array $layoutRootPaths): void
138  {
139  parent::setLayoutRootPaths(
140  ArrayUtility::sortArrayWithIntegerKeys($layoutRootPaths)
141  );
142  }
143 
147  public function ‪setPartialRootPaths(array $partialRootPaths): void
148  {
149  parent::setPartialRootPaths(
150  ArrayUtility::sortArrayWithIntegerKeys($partialRootPaths)
151  );
152  }
153 
159  public function ‪getTemplatePathAndFilename(): string
160  {
162  }
163 
172  protected function ‪ensureAbsolutePath($reference): array|string
173  {
174  if (!is_array($reference)) {
175  return ‪PathUtility::isAbsolutePath($reference) ? $reference : GeneralUtility::getFileAbsFileName($reference);
176  }
177  foreach ($reference as &$subValue) {
178  $subValue = $this->‪ensureAbsolutePath($subValue);
179  }
180  return $reference;
181  }
182 
183  protected function ‪isBackendMode(): bool
184  {
185  return (‪$GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
186  && ‪ApplicationType::fromRequest(‪$GLOBALS['TYPO3_REQUEST'])->isBackend();
187  }
188 
189  protected function ‪isFrontendMode(): bool
190  {
191  return (‪$GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
192  && ‪ApplicationType::fromRequest(‪$GLOBALS['TYPO3_REQUEST'])->isFrontend();
193  }
194 }
‪TYPO3\CMS\Fluid\View\TemplatePaths\ensureAbsolutePath
‪ensureAbsolutePath($reference)
Definition: TemplatePaths.php:170
‪TYPO3\CMS\Fluid\View\TemplatePaths
Definition: TemplatePaths.php:37
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Core\Utility\PathUtility\isAbsolutePath
‪static isAbsolutePath(string $path)
Definition: PathUtility.php:287
‪TYPO3\CMS\Fluid\View\TemplatePaths\getConfigurationManager
‪getConfigurationManager()
Definition: TemplatePaths.php:55
‪TYPO3\CMS\Fluid\View\TemplatePaths\getExtensionPrivateResourcesPath
‪getExtensionPrivateResourcesPath(string $extensionKey)
Definition: TemplatePaths.php:46
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:27
‪TYPO3\CMS\Fluid\View\TemplatePaths\getTemplatePathAndFilename
‪string getTemplatePathAndFilename()
Definition: TemplatePaths.php:157
‪TYPO3\CMS\Fluid\View\TemplatePaths\isFrontendMode
‪isFrontendMode()
Definition: TemplatePaths.php:187
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\isLoaded
‪static isLoaded(string $key)
Definition: ExtensionManagementUtility.php:55
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:32
‪TYPO3\CMS\Fluid\View\TemplatePaths\setTemplateRootPaths
‪setTemplateRootPaths(array $templateRootPaths)
Definition: TemplatePaths.php:125
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager
Definition: ConfigurationManager.php:32
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\extPath
‪static extPath(string $key, string $script='')
Definition: ExtensionManagementUtility.php:82
‪$name
‪$name
Definition: phpIntegrityChecker.php:235
‪TYPO3\CMS\Fluid\View\TemplatePaths\fillDefaultsByPackageName
‪fillDefaultsByPackageName($packageName)
Definition: TemplatePaths.php:117
‪TYPO3\CMS\Fluid\View\TemplatePaths\isBackendMode
‪isBackendMode()
Definition: TemplatePaths.php:181
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_FULL_TYPOSCRIPT
‪const CONFIGURATION_TYPE_FULL_TYPOSCRIPT
Definition: ConfigurationManagerInterface.php:30
‪TYPO3\CMS\Fluid\View\TemplatePaths\$templatePathAndFilename
‪string $templatePathAndFilename
Definition: TemplatePaths.php:44
‪TYPO3\CMS\Fluid\View\TemplatePaths\getContextSpecificViewConfiguration
‪getContextSpecificViewConfiguration(string $extensionKey)
Definition: TemplatePaths.php:60
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪TYPO3\CMS\Fluid\View\TemplatePaths\$templateSource
‪string $templateSource
Definition: TemplatePaths.php:40
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Http\fromRequest
‪@ fromRequest
Definition: ApplicationType.php:66
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Fluid\View\TemplatePaths\setLayoutRootPaths
‪setLayoutRootPaths(array $layoutRootPaths)
Definition: TemplatePaths.php:135
‪TYPO3\CMS\Fluid\View\TemplatePaths\setPartialRootPaths
‪setPartialRootPaths(array $partialRootPaths)
Definition: TemplatePaths.php:145
‪TYPO3\CMS\Fluid\View
Definition: AbstractTemplateView.php:16
‪TYPO3\CMS\Core\Http\ApplicationType
‪ApplicationType
Definition: ApplicationType.php:55