Last active
April 10, 2022 14:13
-
-
Save christophlehmann/48b6f5e752f3d2cf4c2c077f5ac5b9b1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['enhancers']['ExtbaseRequiringPluginOnPage'] = | |
\Lemming\SiteExtension\Routing\ExtbaseEnhancerRequiringPluginOnPage::class; |
This file contains hidden or 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
<?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