Reputation: 21
I am diving into the configuration of routes in quite a complex project and I struggle to get my head around one aspect of it. In the examples here https://docs.typo3.org/typo3cms/extensions/core/latest/Changelog/9.5/Feature-86365-RoutingEnhancersAndAspects.html the "limitToPages" parameter seems to be quite important. But how do I manage to get a route for all pages on which the plugin is put, without producing a conflict with other plugins or extensions? One example: I have a plugin for events and pois. Both have a list view with a pagination. My configuration so far is:
PoiPlugin:
type: Extbase
extension: supervendor
plugin: pois
routes:
- { routePath: '/{page}', _controller: 'POI::list', _arguments: {'page': '@widget_0/currentPage'} }
- { routePath: '/{poi_title}', _controller: 'POI::show', _arguments: {'poi_title': 'item'} }
defaultController: 'POI::list'
defaults:
page: '0'
requirements:
page: '\d+'
poi_title: '^[a-zA-Z0-9]'
aspects:
page:
type: StaticRangeMapper
start: '1'
end: '100'
poi_title:
type: PersistedAliasMapper
tableName: 'tx_supervendor_domain_model_poi'
routeFieldName: 'path_segment'
EventPlugin:
type: Extbase
extension: supervendor
plugin: events
routes:
- { routePath: '/{page}', _controller: 'Event::list', _arguments: {'page': '@widget_0/currentPage'} }
- { routePath: '/{event_title}', _controller: 'Event::show', _arguments: {'event_title': 'item'} }
defaultController: 'Event::list'
defaults:
page: '0'
requirements:
page: '\d+'
event_title: '^[a-zA-Z0-9]'
aspects:
page:
type: StaticRangeMapper
start: '1'
end: '100'
event_title:
type: PersistedAliasMapper
tableName: 'tx_supervendor_domain_model_event'
routeFieldName: 'path_segment'
The problem right now is, that even if I put those plugins on different pages, they seem to be in conflict. Because the pagination links always take the namespace of the first plugin to be mentioned in the config.yaml. So if the PoiPlugin is mentioned first, the pagination on the pois page is working with the "page"-parameter, but the events don't. If I change the order, the events work but the pois don't. My assumption was, that the routing base is always the page on which the plugin is embeded. I hope, you gals and guys understand what I mean. The only configuration that works right now is, when I change the routePaths to
- { routePath: '/pois/{page}', _controller: 'POI::list', _arguments: {'page': '@widget_0/currentPage'} }
and
- { routePath: '/events/{page}', _controller: 'Event::list', _arguments: {'page': '@widget_0/currentPage'} }
And I do understand why that is, the router has to have some kind of anchor to recognize the routePath to choose. But I don't want to tell the authors, that every page on which the PoiPlugin is put has to be named "Pois". I don't want to limit the authors on where and how to use the plugins, so I don't want to use the limitToPages-attribute nor the predefined route-prefixes. So is there any other possibility to solve that?
UPDATED: solution For now I solved this issue by prepending a prefix before the {page} argument in the routePath. It seems that only the pagination widget had problems with my configuration, everything else works quite well. For example the list-routes look like this now:
- { routePath: '/e_{page}', _controller: 'Event::list', _arguments: {'page': '@widget_0/currentPage'} }
- { routePath: '/p_{page}', _controller: 'POI::list', _arguments: {'page': '@widget_0/currentPage'} }
Upvotes: 2
Views: 2001
Reputation: 618
Maybe add an extra field at the plugin, which generates an unique slug (URL segment) for example based on the plugin's (required then) content element name, then put the slug in front of the routepath (for example before page). I am myself now struggling to get the page widget route working correctly in a routeEnhancer..
Since I allow frontend creation of records the generate code (could be better maybe) I use at the moment is something like
protected function createUniqueTopicSlug(\Bla\Blacommunity\Domain\Model\Topic $topic)
{
// Generate slug (URL segment)
$subject = (string)$topic->getSubject();
$trySlug = $this->slugHelper->sanitize(str_replace('/', '', $subject));
// Make slug unique
$existingSlugs = $this->topicRepository->countBySlug($trySlug);
$slugCounter = 1;
$slug = $trySlug;
while ($existingSlugs > 0) {
$slug = $trySlug . '-' . $slugCounter;
$existingSlugs = $this->topicRepository->countBySlug($slug);
$slugCounter++;
}
return $slug;
}
slugHelper comes from TYPO3\CMS\Core\DataHandling\SlugHelper
You would need to adjust it to check on unique slug. I think there is a core function for it too, but I don't really understand how to use it for a custom plugin, so I wrote my own function but at least I use the sanetize core function. But this is (I think) only needed with frontend creation of records, since the new TCA slug type can generate slugs too.
Upvotes: 1