Skip to content

Instantly share code, notes, and snippets.

@calien666
Last active November 5, 2020 09:48
Show Gist options
  • Save calien666/1a0e6c32b3896c73e150a5b87403fe4e to your computer and use it in GitHub Desktop.
Save calien666/1a0e6c32b3896c73e150a5b87403fe4e to your computer and use it in GitHub Desktop.
Allow same file ending on multiple PageType definitions and remove default index.html in TYPO3 Page configuration
routeEnhancers:
PageTypeSuffix:
type: CustomPageType
default: .html
index: index
map:
sitemap.xml: 1533906435
product/|.html: 1603119111937
<?php
declare(strict_types=1);
namespace VENDOR\Extension\Routing\Enhancer;
use TYPO3\CMS\Core\Routing\Enhancer\PageTypeDecorator;
use TYPO3\CMS\Core\Routing\RouteCollection;
class CustomPageTypeDecorator extends PageTypeDecorator
{
public const IGNORE_INDEX = [
'/index.html',
'/index/',
];
public const ROUTE_PATH_DELIMITERS = ['.', '-', '_', '/'];
/**
* @param \TYPO3\CMS\Core\Routing\RouteCollection $collection
* @param array $parameters
*/
public function decorateForGeneration(RouteCollection $collection, array $parameters): void
{
parent::decorateForGeneration($collection, $parameters);
/**
* @var string $routeName
* @var \TYPO3\CMS\Core\Routing\Route $route
*/
foreach ($collection->all() as $routeName => $route) {
$path = $route->getPath();
if (array_key_exists('type', $parameters)) {
$mappings = array_flip($this->map);
if (array_key_exists($parameters['type'], $mappings)) {
$splitPath = explode('|', str_replace(['.', '/'], ['\.', '\/'], $mappings[$parameters['type']]));
if (count($splitPath) == 2) {
$regEx = sprintf('/(.*)(\{.+\})(\/%s)(\|)(%s)/', $splitPath[0], $splitPath[1]);
$path = preg_replace($regEx, '$1$3$2$5', $path);
$route->setPath($path);
}
}
}
if (true === \in_array($path, self::IGNORE_INDEX, true)) {
$route->setPath('/');
}
}
}
public function decorateForMatching(RouteCollection $collection, string $routePath): void
{
foreach ($this->configuration['map'] as $checkPath => $typeNum) {
$counts = 0;
$regEx = str_replace('|', ')(.+)(', sprintf('/(.*)(%s)/', str_replace(['.', '/'], ['\.', '\/'], $checkPath)));
$replaced = preg_replace($regEx, '$1$3/$2|$4', $routePath, -1, $counts);
if ($counts > 0) {
$routePath = $replaced;
}
}
parent::decorateForMatching($collection, $routePath);
}
}
<?php
// register own PageTypeEnhancer
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['enhancers']['CustomPageType'] = \VENDOR\Extension\Routing\Enhancer\CustomPageTypeDecorator::class;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment