0

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

5
  • and you can't do if ($route = $collection->get('node.add') && $my_condition) in your RouteSubscriber? Nov 17, 2022 at 9:34
  • 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.
    – Giuseppe
    Nov 17, 2022 at 9:49
  • 1
    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 drupal.stackexchange.com/questions/256841/… Nov 17, 2022 at 10:05
  • 1
    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 drupal.org/node/2013014, or in a form alter hook targeting this specific entity form, see drupal.stackexchange.com/questions/311211/…
    – 4uk4
    Nov 17, 2022 at 10:15
  • 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
    – Giuseppe
    Nov 17, 2022 at 15:17

1 Answer 1

1

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);
    }

  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.