Score:0

Alter a route according the current user

br flag

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

unusedspoon avatar
aq flag
and you can't do if `($route = $collection->get('node.add') && $my_condition)` in your RouteSubscriber?
Giuseppe avatar
br flag
No, because that condition depend on the current user, and the alterRoute is run only "After building routes (e.g. when a module is enabled or when caches are cleared)," and not when the page is loaded.
unusedspoon avatar
aq flag
Potentially in your else section you'd need to just re-create what core is doing for this route (aka provide the node edit form). Something like this might work https://drupal.stackexchange.com/questions/256841/alter-existing-route-to-a-different-route-for-a-specific-content-type-node-using
4uk4 avatar
cn flag
Yes, or extend the core controller so that you don't need to re-create what core is doing. But in this case the route points at a generic entity form, which I would leave alone to avoid conflicts with other modules. You can redirect in other places, like an event subscriber, see https://www.drupal.org/node/2013014, or in a form alter hook targeting this specific entity form, see https://drupal.stackexchange.com/questions/311211/clarify-difference-between-hook-form-type-alter-and-hook-form-type-edit-alter
Giuseppe avatar
br flag
Thanks to both, I started to extends the `HtmlEntityFormController` to "re-create" the core logics, but then the suggestions of @4uk4 let to me easily implement what I needed. Thanks
Score:1
br flag

Thanks to the comment of @4uk4 I've develop an Event Subscriber where is easy to implement the logic I need:


  public function onKernelRequest(RequestEvent $event) {
     if ($this->account->hasPermission('my permission')) {
      return;
    }

    $route_name = $this->routeMatch->getRouteName();
    
    if($route_name === 'node.add') {
     $url = Url::fromRoute('custom_route')->toString();
     $response = new RedirectResponse($url);
     $event->setResponse($response);
    }

  }

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.