I have a view for a Drupal 8 site that uses 'work' as its path. I want to redirect user to this path right after the node has been submitted.

Form alter seems to be the right solution as it was in Drupal 7, but it's more complicated then it was before. In Drupal 7 we just used a direct path and redirect took place; now we need to provide a route name which isn't that obvious anymore. How do we find proper route name value? It's different for nodes, views, custom paths etc.

What I should use as argument for $form_state->setRedirect('')? How do I find what route name I should use in different cases?

function work_form_alter(&$form, \Drupal\Core\Form\FormStateInterface\FormStateInterface $form_state, $form_id) {
  switch ($form_id) {
    case 'node_work_form':
      $form['actions']['submit']['#submit'][] = 'work_redirect';

function work_redirect($form, FormStateInterface $form_state) {
  // Set redirect to some path.
  $form_state->setRedirect(''); // What should I provide here as an argument/arguments

2 Answers 2


For Views, the route pattern is view.(view_id).(display_id).

For instance, if I have a view called Articles, and a display that I named "All", it would be:


If you don't rename your display ids (I usually do), it is probably page_1 or something of that nature.

Beyond that it's similar to how it was before. Here is a redirect with query string args attached:

$form_state->setRedirect('view.articles.all', ['keywords' => $form_state->getValue('keywords')]);

Instead of setRedirect($route_name) we can use setRedirectUrl(Url $url) method which requires only the url.

To use this method you need use this namespace at the top of the page:

use Drupal\Core\Url;

Redirect function would look like this:

function work_redirect($form, FormStateInterface $form_state) {
  // Path needs to start with / or # or ?
  $redirect_path = "/work";
  $url = url::fromUserInput($redirect_path);

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.