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