Generate link to single news with date in path segment and ignore it in resolve
This file contains 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 Cp\CpSitepackage\Routing\Aspect; | |
class DateFormatter | |
{ | |
public function format($value, $format = 'd.m.Y'): string | |
{ | |
return date($format, $value); | |
} | |
} |
This file contains 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 | |
defined('TYPO3_MODE') || die('Access denied.'); | |
call_user_func(function () { | |
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects']['EvowebPersistedPatternMapper'] = | |
\Evoweb\CpSitepackage\Routing\Aspect\PersistedPatternMapper::class; | |
}); |
This file contains 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
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 |
This file contains 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 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 | |
); | |
} | |
} |
Never thought, that a simple date() call would be too difficulty to figure it out on your own.
I figured it out but something is missing cause I get a page not found when accessing a generated link.
Solved, a character was missing in the pattern.
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
This is what I’m looking for. Can you also post DateFormatter.php?