I'm on D 9.4.x, trying to redirect the node.add
route to a custom one, only when the current user has the right role\permission.
So I don't think I can't work in the alterRoutes but I probably need to override the controller of that route. In a custom RouteSubscriber I have:
protected function alterRoutes(RouteCollection $collection) {
if ($route = $collection->get('node.add')) {
$route->setDefault('_controller', '\Drupal\my_module\myController::alterRoute');
}
}
and in the controller
public function alterRoute(RouteMatchInterface $route_match) {
if ($my_condition) {
return $this->redirect('my_route');
}
else {
$node_type = $route_match->getRawParameter('node_type');
return $this->redirect('node.add', ['node_type' => $node_type]); //this goes loop
}
}
This code causes a loop, as when the condition fails the redirect return in my custom controller and so on. Any idea I could implement this feature? Thanks
($route = $collection->get('node.add') && $my_condition)
in your RouteSubscriber?HtmlEntityFormController
to "re-create" the core logics, but then the suggestions of @4uk4 let to me easily implement what I needed. Thanks