0

I need to redirect all existing links targeting a few hand-picked nodes to an external domain. I created an EventSubscriber listening to RequestEvents

my.services.yml

services:
  my.request_subscriber:
    class: Drupal\my\EventSubscriber\EventSubscriber
    arguments: ['@current_route_match', '@redirect.checker']
    tags:
      - { name: event_subscriber }

EventSubscriber.php

/* trimmed constructor, etc. for simplicity */

  public static function getSubscribedEvents() {
    // This needs to run before Redirect module
    $events[KernelEvents::REQUEST][] = ['onKernelRequestCheckRedirect', 34];
    return $events;
  }
  public function onKernelRequestCheckRedirect(RequestEvent $event) {
    // the follwing lines are taken from redirect module 
    // @see https://git.drupalcode.org/project/redirect/-/blob/8.x-1.x/src/EventSubscriber/RedirectRequestSubscriber.php#L111
    $request = clone $event->getRequest();
    if (!$this->checker->canRedirect($request)) {
      return;
    }

    /**
     * No matter which route I visit, the $this->routeMatch->getRouteObject is always null,
     * therefore the following lines also don't work as I hoped
     */
    $routeName = $this->routeMatch->getRouteName(); // returns NULL :(
    $nodeId = $this->routeMatch->getParameter('node'); // returns NULL :(

    if ($routeName !== 'entity.node.canonical' || !in_array($nodeId, [MY_HANDPICKED_NODE_IDS]) ) {
      return;
    }
    else {
       // do $event->setResponse(my TrustedRedirectResponse);
    }
}

Why is my route object always NULL? How do I get the routing information in my subscriber?

4
  • For debugging/testing purposes, if you try \Drupal::routeMatch()->getRouteName(); is it also NULL ?
    – No Sssweat
    Nov 24 at 12:12
  • Yes, same as injected, all route information is NULL
    – Hudri
    Nov 24 at 12:37
  • Why did you choose the priority 34? Routing is at priority 32. The default priority 0 should work most times unless you need to run before a specific core subscriber.
    – 4uk4
    Nov 24 at 12:43
  • 1
    @4uk4 Thanks, this was the issue. I was taking a look at the code in the redirect module, they used 34 there and I thought this is good for me too. Simply removing/using the default priority fixed it $events[KernelEvents::REQUEST][] = ['onKernelRequestCheckRedirect'];
    – Hudri
    Nov 24 at 12:52

1 Answer 1

1

The problem was caused by the priority inside the subscribe function. The redirect module used priority 34 here, but this was not fitting my case. Simply removing/using the default priority in the subscribe function solved the issue.

  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = ['onKernelRequestCheckRedirect'];
    return $events;
  }

This answer is coming from @4uk4's comment below the original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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