-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
171 additions
and
2 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
Classes/BreadcrumbTitle/AbstractBreadcrumbTitleProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Remind\Headless\BreadcrumbTitle; | ||
|
||
use TYPO3\CMS\Core\SingletonInterface; | ||
|
||
class AbstractBreadcrumbTitleProvider implements BreadcrumbTitleProviderInterface, SingletonInterface | ||
{ | ||
protected $title = ''; | ||
|
||
public function getTitle(): string | ||
{ | ||
return $this->title; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
Classes/BreadcrumbTitle/BreadcrumbTitleProviderInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Remind\Headless\BreadcrumbTitle; | ||
|
||
interface BreadcrumbTitleProviderInterface | ||
{ | ||
public function getTitle(): string; | ||
} |
100 changes: 100 additions & 0 deletions
100
Classes/BreadcrumbTitle/BreadcrumbTitleProviderManager.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Remind\Headless\BreadcrumbTitle; | ||
|
||
use Psr\Log\LoggerAwareInterface; | ||
use Psr\Log\LoggerAwareTrait; | ||
use TYPO3\CMS\Core\Service\DependencyOrderingService; | ||
use TYPO3\CMS\Core\SingletonInterface; | ||
use TYPO3\CMS\Core\TypoScript\TypoScriptService; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use UnexpectedValueException; | ||
|
||
class BreadcrumbTitleProviderManager implements SingletonInterface, LoggerAwareInterface | ||
{ | ||
use LoggerAwareTrait; | ||
|
||
private $breadcrumbTitleCache = []; | ||
|
||
public function getTitle(): string | ||
{ | ||
$breadcrumbTitle = ''; | ||
|
||
$titleProviders = $this->getBreadcrumbTitleProviderConfiguration(); | ||
$titleProviders = $this->setProviderOrder($titleProviders); | ||
|
||
$orderedTitleProviders = GeneralUtility::makeInstance(DependencyOrderingService::class) | ||
->orderByDependencies($titleProviders); | ||
|
||
$this->logger->debug('Breadcrumb title providers ordered', [ | ||
'orderedTitleProviders' => $orderedTitleProviders, | ||
]); | ||
|
||
foreach ($orderedTitleProviders as $provider => $configuration) { | ||
if ( | ||
class_exists($configuration['provider']) && | ||
is_subclass_of($configuration['provider'], BreadcrumbTitleProviderInterface::class) | ||
) { | ||
/** @var BreadcrumbTitleProviderInterface $titleProviderObject */ | ||
$titleProviderObject = GeneralUtility::makeInstance($configuration['provider']); | ||
if ( | ||
($breadcrumbTitle = $titleProviderObject->getTitle()) | ||
|| ($breadcrumbTitle = $this->breadcrumbTitleCache[$configuration['provider']] ?? '') !== '' | ||
) { | ||
$this->logger->debug('Breadcrumb title provider {provider} used on page {title}', [ | ||
'title' => $breadcrumbTitle, | ||
'provider' => $configuration['provider'], | ||
]); | ||
$this->breadcrumbTitleCache[$configuration['provider']] = $breadcrumbTitle; | ||
break; | ||
} | ||
$this->logger->debug('Breadcrumb title provider {provider} skipped on page {title}', [ | ||
'title' => $breadcrumbTitle, | ||
'provider' => $configuration['provider'], | ||
'providerUsed' => $configuration['provider'], | ||
]); | ||
} | ||
} | ||
|
||
return $breadcrumbTitle; | ||
} | ||
|
||
protected function setProviderOrder(array $orderInformation): array | ||
{ | ||
foreach ($orderInformation as $provider => &$configuration) { | ||
if (isset($configuration['before'])) { | ||
if (is_string($configuration['before'])) { | ||
$configuration['before'] = GeneralUtility::trimExplode(',', $configuration['before'], true); | ||
} elseif (!is_array($configuration['before'])) { | ||
throw new UnexpectedValueException( | ||
'The specified "before" order configuration for provider "' . $provider . '" is invalid.', | ||
1535803185 | ||
); | ||
} | ||
} | ||
if (isset($configuration['after'])) { | ||
if (is_string($configuration['after'])) { | ||
$configuration['after'] = GeneralUtility::trimExplode(',', $configuration['after'], true); | ||
} elseif (!is_array($configuration['after'])) { | ||
throw new UnexpectedValueException( | ||
'The specified "after" order configuration for provider "' . $provider . '" is invalid.', | ||
1535803186 | ||
); | ||
} | ||
} | ||
} | ||
return $orderInformation; | ||
} | ||
|
||
private function getBreadcrumbTitleProviderConfiguration(): array | ||
{ | ||
$typoscriptService = GeneralUtility::makeInstance(TypoScriptService::class); | ||
$config = $typoscriptService->convertTypoScriptArrayToPlainArray( | ||
$GLOBALS['TSFE']->config['config'] ?? [] | ||
); | ||
|
||
return $config['breadcrumbTitleProviders'] ?? []; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Classes/Event/Listener/AfterCacheableContentIsGeneratedEventListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Remind\Headless\Event\Listener; | ||
|
||
use FriendsOfTYPO3\Headless\Json\JsonEncoder; | ||
use Remind\Headless\BreadcrumbTitle\BreadcrumbTitleProviderManager; | ||
use Throwable; | ||
use TYPO3\CMS\Frontend\Event\AfterCacheableContentIsGeneratedEvent; | ||
|
||
class AfterCacheableContentIsGeneratedEventListener | ||
{ | ||
public function __construct( | ||
private readonly JsonEncoder $encoder, | ||
private readonly BreadcrumbTitleProviderManager $breadcrumbTitleProviderManager, | ||
) { | ||
} | ||
|
||
public function __invoke(AfterCacheableContentIsGeneratedEvent $event) | ||
{ | ||
try { | ||
$content = json_decode($event->getController()->content, true, 512, JSON_THROW_ON_ERROR); | ||
|
||
$breadcrumbTitle = $this->breadcrumbTitleProviderManager->getTitle(); | ||
|
||
if ($breadcrumbTitle) { | ||
$content['breadcrumbs'][array_key_last($content['breadcrumbs'])]['title'] = $breadcrumbTitle; | ||
} | ||
|
||
$event->getController()->content = $this->encoder->encode($content); | ||
} catch (Throwable) { | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters