0

I'm using a form_alter hook to add a custom submit callback. I want to redirect the user to a particular page display of a view. However, I'm having trouble figuring out how to specify the path.

In my custom submit hook, I tried $form_state->setRedirect('/my-view/page/path'); but that didn't work. From searching, I found this question, Redirect after form submission, but the answers indicate to specify the machine name of a path from a routing yaml file.

I'm wanting to redirect the user to a views page display that I built in the UI, so I don't have any routing.yml file (that I'm aware of) where I could glean a machine name of a path from.

How do I set the redirect in a custom submit handler to a page display of a view?

1 Answer 1

1

Routes are not only defined in yaml files, but also dynamically in route subscribers.

Drupal\views\EventSubscriber\RouteSubscriber collects routes from all views displays which implement DisplayRouterInterface::collectRoutes, like the Page display:

PathPluginBase::collectRoutes:

  public function collectRoutes(RouteCollection $collection) {
    $view_id = $this->view->storage->id();
    $display_id = $this->display['id'];

    $route = $this->getRoute($view_id, $display_id);

    if (!($route_name = $this->getOption('route_name'))) {
      $route_name = "view.$view_id.$display_id";
    }
    $collection->add($route_name, $route);
    return ["$view_id.$display_id" => $route_name];
  }

This produces route names like view.my_view.page_1 and you can redirect in a form submit:

$form_state->setRedirect('view.my_view.page_1');

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.