I have been trying to intercept a page request from /node/nid which is of type the_content_type and then appending some facet query parameters to it which is something like this


The purpose is to preselect the facet options from that page.

Here is the subscriber class I have written so far


namespace Drupal\custom_module\EventSubscriber;

use Drupal\Core\Url;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Drupal\Core\Routing\TrustedRedirectResponse;

class RedirectWithFilterSubscriber implements EventSubscriberInterface {
   * {@inheritdoc}
  public static function getSubscribedEvents() {
    // This announces which events you want to subscribe to.
    // We only need the request event for this example.  Pass
    // this an array of method names
      KernelEvents::REQUEST => [

   * Redirect requests for my_content_type node detail pages to node/123.
   * @param GetResponseEvent $event
   * @return void
  public function redirectWithFilter(GetResponseEvent $event) {
    $request = $event->getRequest();

    // This is necessary because this also gets called on
    // node sub-tabs such as "edit", "revisions", etc.  This
    // prevents those pages from redirected.
    if ($request->attributes->get('_route') !== 'entity.node.canonical') {

    // Only redirect a certain content type.
    if ($request->attributes->get('node')->getType() !== 'the_content_type') {

    $node = $request->attributes->get('node');
    $node_id = $node->id();
    $node_url = $node->url();
    $country = $node->field_country->getValue();
    $city = $node->field_city->getValue();
    $country_target_id = $country[0]['target_id'];
    $city_target_id = $city[0]['target_id'];

    if (empty($country) && empty($city)) {

    // This is where you set the destination.
    $option = [
      "query" => [
        "f" => [

    $redirect_url = Url::fromUri("entity:node/$node_id", $option);
    $response = new RedirectResponse($redirect_url->toString(), 301);

The resulting url looks fine though


But the problem is, the page always shows

The page isn’t redirecting properly

on my browser. I've been figuring this out for almost 3 days now and I can't seem to find a way through it. I've been wanting someone to have a fresh look into it. Maybe I am doing something wrong. Is there somebody can lend me a help please? Or maybe, a different way other than this? Thank you in advance.

1 Answer 1


Could be a redirect loop. You have to check whether the query string is already present and then stop redirecting again:

  public function redirectWithFilter(GetResponseEvent $event) {
    $request = $event->getRequest();
    if ($request->query->has('city')) {
  • Thank you for feedback 4k4. It really helps me to figure out the problem and allow me to fix the issue. I have modified a bit your answer to suit my needs. Jan 26, 2020 at 11:08
  • This is how I did it. $query = $request->query->all(); if ($query['f'][0] == "city:$city_target_id") { return; } if ($query['f'][0] == "country:$country_target_id") { return; } Jan 26, 2020 at 11:10

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.