Skip to content

Instantly share code, notes, and snippets.

@garbast
Last active August 25, 2021 12:41
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 garbast/181cfe7f672c455ca9f771c9583a97dd to your computer and use it in GitHub Desktop.
Save garbast/181cfe7f672c455ca9f771c9583a97dd to your computer and use it in GitHub Desktop.
Generate link to single news with date in path segment and ignore it in resolve
<?php
namespace Cp\CpSitepackage\Routing\Aspect;
class DateFormatter
{
public function format($value, $format = 'd.m.Y'): string
{
return date($format, $value);
}
}
<?php
defined('TYPO3_MODE') || die('Access denied.');
call_user_func(function () {
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects']['EvowebPersistedPatternMapper'] =
\Evoweb\CpSitepackage\Routing\Aspect\PersistedPatternMapper::class;
});
routeEnhancers:
NewsDetail:
type: Extbase
limitToPages:
- 334
extension: News
plugin: Pi1
routes:
- routePath: '/{news_title}'
_controller: 'News::detail'
_arguments:
news_title: news
- routePath: '/{page}'
_controller: 'News::list'
_arguments:
page: '@widget_0/currentPage'
defaultController: 'News::detail'
defaults:
page: '0'
requirements:
news_title: '.+'
page: '\d+'
aspects:
news_title:
type: EvowebPersistedPatternMapper
tableName: 'tx_news_domain_model_news'
# no capture group for date as we dont use it for record resolving
routeFieldPattern: '^(\d+-\d+-\d+)-(?P<path_segment>.+)$'
routeFieldResult: '{datetime}-{path_segment}'
formattedFields:
datetime:
formatter: Evoweb\CpSitepackage\Routing\Aspect\DateFormatter
format: Y-m-d
<?php
namespace Evoweb\CpSitepackage\Routing\Aspect;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class PersistedPatternMapper extends \TYPO3\CMS\Core\Routing\Aspect\PersistedPatternMapper
{
/**
* @param array|null $result
* @return string|null
* @throws \InvalidArgumentException
*/
protected function createRouteResult(?array $result): ?string
{
if ($result === null) {
return $result;
}
$substitutes = [];
foreach ($this->routeFieldResultNames as $fieldName) {
if (!isset($result[$fieldName])) {
return null;
}
$routeFieldName = '{' . $fieldName . '}';
if (isset($this->settings['formattedFields']) && isset($this->settings['formattedFields'][$fieldName])) {
$formatConfig = $this->settings['formattedFields'][$fieldName];
$formatter = GeneralUtility::makeInstance($formatConfig['formatter']);
$substitutes[$routeFieldName] = $formatter->format($result[$fieldName], $formatConfig['format']);
} else {
$substitutes[$routeFieldName] = $result[$fieldName];
}
}
return str_replace(
array_keys($substitutes),
array_values($substitutes),
$this->routeFieldResult
);
}
}
@klodeckl
Copy link

klodeckl commented Feb 4, 2021

This is what I’m looking for. Can you also post DateFormatter.php?

@garbast
Copy link
Author

garbast commented Feb 4, 2021

Never thought, that a simple date() call would be too difficulty to figure it out on your own.

@klodeckl
Copy link

klodeckl commented Feb 4, 2021

I figured it out but something is missing cause I get a page not found when accessing a generated link.

@klodeckl
Copy link

klodeckl commented Feb 4, 2021

Solved, a character was missing in the pattern.

@derBoogie
Copy link

Thank's a lot for this!

I spent hours looking for a way to get the date in the news url, because the routing via plugin.tx_news.link.hrDate and the GET parameters year, month and day results in this error:
"Possible range of all mappers is larger than 10000 items
Using the StaticRangeMapper is strictly limited to 1000 items per a single range and 10000 items per routing enhancer."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment