Skip to content

Commit

Permalink
feat: add breadcrumbTitleProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdmlln committed Aug 7, 2024
1 parent af27e1f commit 6a2c200
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 2 deletions.
17 changes: 17 additions & 0 deletions Classes/BreadcrumbTitle/AbstractBreadcrumbTitleProvider.php
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 Classes/BreadcrumbTitle/BreadcrumbTitleProviderInterface.php
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 Classes/BreadcrumbTitle/BreadcrumbTitleProviderManager.php
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'] ?? [];
}
}
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;
}
}
}
4 changes: 4 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ services:
tags:
-
name: event.listener
Remind\Headless\Event\Listener\AfterCacheableContentIsGeneratedEventListener:
tags:
-
name: event.listener
6 changes: 4 additions & 2 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@

$GLOBALS
['TYPO3_CONF_VARS']
['SYS']['features']
['SYS']
['features']
['headless.elementBodyResponse'] = true;

$GLOBALS
['TYPO3_CONF_VARS']
['SYS']['features']
['SYS']
['features']
['headless.frontendUrls'] = true;

$GLOBALS
Expand Down

0 comments on commit 6a2c200

Please sign in to comment.