Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophlehmann/48b6f5e752f3d2cf4c2c077f5ac5b9b1 to your computer and use it in GitHub Desktop.
Save christophlehmann/48b6f5e752f3d2cf4c2c077f5ac5b9b1 to your computer and use it in GitHub Desktop.
<?php
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['enhancers']['ExtbaseRequiringPluginOnPage'] =
\Lemming\SiteExtension\Routing\ExtbaseEnhancerRequiringPluginOnPage::class;
<?php
namespace Lemming\SiteExtension\Routing;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Routing\RouteCollection;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Routing\ExtbasePluginEnhancer;
/**
* Records across multiple tables may have the same slug. An event record may not be shown when the earlier enhancer for news
* finds a news record having the same slug.
*
* We don't want to use the routing feature "limitToPages" with a static list of page ids.
*
* With this enhancer we check if there is a plugin on the page for the wanted record type. If not, this enhancer
* does nothing.
*/
class ExtbaseEnhancerRequiringPluginOnPage extends ExtbasePluginEnhancer
{
public function enhanceForMatching(RouteCollection $collection): void
{
if (!$this->isRoutePathFullyDecorated($collection) && $this->isPluginOnPage($collection)) {
parent::enhanceForMatching($collection);
}
}
/**
* Check if there is something to decorate. Given url path = page slug then nothing needs to be enhanced.
*
* This is called for
* - /anypath/news/news-title
* - /anypath/
* - /
*
* and only the first is interesting so we can cache the result.
*/
protected function isRoutePathFullyDecorated(RouteCollection $collection): bool
{
$runtimeCache = GeneralUtility::makeInstance(CacheManager::class)->getCache('runtime');
if ($runtimeCache->get('skipExtbaseEnhancerRequiringPluginOnPage')) {
return true;
}
$options = $collection->get('default')->getOptions();
$isRoutePathFullyDecorated = isset($options['_decoratedRoutePath']) && $options['_page']['slug'] === $options['_decoratedRoutePath'];
$runtimeCache->set('skipExtbaseEnhancerRequiringPluginOnPage', true);
return $isRoutePathFullyDecorated;
}
protected function isPluginOnPage(RouteCollection $collection): bool
{
$listType = strtolower(implode(
'_',
[
$this->configuration['extension'],
$this->configuration['plugin'],
]
));
$options = $collection->get('default')->getOptions();
$pageUid = $options['_page']['l10n_parent'] > 0 ? $options['_page']['l10n_parent'] : $options['_page']['uid'];
/** @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
$count = $queryBuilder
->count('*')
->from('tt_content')
->where(
$queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($pageUid, \PDO::PARAM_INT)),
$queryBuilder->expr()->eq('list_type', $queryBuilder->createNamedParameter($listType))
)
->execute()
->fetchOne();
return (bool)$count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment