2 declare(strict_types
= 1);
4 namespace TYPO3\CMS\Core\PageTitle
;
7 * This file is part of the TYPO3 CMS project.
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
16 * The TYPO3 project - inspiring people to share!
19 use TYPO3\CMS\Core\Cache\CacheManager
;
20 use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
;
21 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
;
22 use TYPO3\CMS\Core\Service\DependencyOrderingService
;
23 use TYPO3\CMS\Core\SingletonInterface
;
24 use TYPO3\CMS\Core\TypoScript\TypoScriptService
;
25 use TYPO3\CMS\Core\Utility\GeneralUtility
;
26 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
;
29 * This class will take care of the different providers and returns the title with the highest priority
31 class PageTitleProviderManager
implements SingletonInterface
34 * @var FrontendInterface
38 public function __construct()
45 * @throws \TYPO3\CMS\Core\Cache\Exception
46 * @throws \TYPO3\CMS\Core\Cache\Exception\InvalidDataException
48 public function getTitle(): string
52 $titleProviders = $this->getPageTitleProviderConfiguration();
53 $titleProviders = $this->setProviderOrder($titleProviders);
55 $orderedTitleProviders = GeneralUtility
::makeInstance(DependencyOrderingService
::class)
56 ->orderByDependencies($titleProviders);
58 foreach ($orderedTitleProviders as $provider => $configuration) {
59 $cacheIdentifier = $this->getTypoScriptFrontendController()->newHash
. '-titleTag-' . $provider;
60 if ($this->pageCache
instanceof FrontendInterface
&&
61 $pageTitle = $this->pageCache
->get($cacheIdentifier)
65 if (class_exists($configuration['provider']) && is_subclass_of($configuration['provider'], PageTitleProviderInterface
::class)) {
66 /** @var PageTitleProviderInterface $titleProviderObject */
67 $titleProviderObject = GeneralUtility
::makeInstance($configuration['provider']);
68 if ($pageTitle = $titleProviderObject->getTitle()) {
69 $this->pageCache
->set(
72 ['pageId_' . $this->getTypoScriptFrontendController()->page
['uid']],
73 $this->getTypoScriptFrontendController()->get_cache_timeout()
84 * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
86 private function getTypoScriptFrontendController(): TypoScriptFrontendController
88 return $GLOBALS['TSFE'];
92 * Get the TypoScript configuration for pageTitleProviders
95 private function getPageTitleProviderConfiguration(): array
97 $typoscriptService = GeneralUtility
::makeInstance(TypoScriptService
::class);
98 $config = $typoscriptService->convertTypoScriptArrayToPlainArray(
99 $this->getTypoScriptFrontendController()->config
['config'] ??
[]
102 return $config['pageTitleProviders'] ??
[];
106 * Initializes the caching system.
108 protected function initCaches(): void
111 $this->pageCache
= GeneralUtility
::makeInstance(CacheManager
::class)->getCache('pages');
112 } catch (NoSuchCacheException
$e) {
113 // Intended fall-through
118 * @param array $orderInformation
120 * @throws \UnexpectedValueException
122 protected function setProviderOrder(array $orderInformation): array
124 foreach ($orderInformation as $provider => &$configuration) {
125 if (isset($configuration['before'])) {
126 if (is_string($configuration['before'])) {
127 $configuration['before'] = GeneralUtility
::trimExplode(',', $configuration['before'], true);
128 } elseif (!is_array($configuration['before'])) {
129 throw new \
UnexpectedValueException(
130 'The specified "before" order configuration for provider "' . $provider . '" is invalid.',
135 if (isset($configuration['after'])) {
136 if (is_string($configuration['after'])) {
137 $configuration['after'] = GeneralUtility
::trimExplode(',', $configuration['after'], true);
138 } elseif (!is_array($configuration['after'])) {
139 throw new \
UnexpectedValueException(
140 'The specified "after" order configuration for provider "' . $provider . '" is invalid.',
146 return $orderInformation;