25

In my custom submit handler I want to redirect to a view page Url, let's suppose my view name is team and its path is /admin/team, I find out I should use $from_state->setRedirect('route'); but when I tried $from_state->setRedirect('/admin/team'); Faced with error, How can I get view route name in?

2
  • This is also required for static YAML menu configuration.
    – vijaycs85
    Sep 27, 2017 at 9:54
  • 1
    Not direct response, but I had same question to links to a custom view in another custom module so in configration file menu : MODULE.links.menu.yml create a node with route option route_name: view.YOUR_CUSTOM_MODULE_CONTAINS_CUSTOM_CONTENT_TYPE.page_1 if page_1 is a name of page view mode. Aug 8, 2019 at 18:48

8 Answers 8

54

The correct routename for view is view.VIEW_MACHINE_NAME.PAGE_MACHINENAME(in another word "view.$view_id.$display_id" ) . try something like following

use \Drupal\Core\Url;

$url = Url::fromRoute('view.VIEW_MACHINE_NAME.PAGE_MACHINENAME');
$form_state->setRedirectUrl($url);

and for your case

$url = Url::fromRoute('view.team.page_1'); // page_1 is my guess as to your page name, please replace it with your page machine_name

Lean more : How to formulate the route name

after @4k4 comment, Also you can redirect form to custom router with URL

just need use $form_state->setRedirectUrl($url)

1
  • This answer is a little off, as you are getting the correct routename for the view, so there is no need to create a URL object from that routename. You should be able to use the routename directly: $form_state->setRedirect('view.VIEW_MACHINE_NAME.PAGE_MACHINENAME');
    – kbrinner
    Jun 14, 2019 at 16:13
11

To use views contextual filters as parameters use arg_X, fe.

$url = Url::fromRoute('view.team.page_1', ['arg_0' => 1234]);
1
  • 2
    Admitting your view page path is foo/%user/bar, then $url = Url::fromRoute('view.team.page_1', ['user' => 1234]);
    – Aporie
    Nov 11, 2020 at 15:44
4

The Webprofiler of the devel module let's you see the route of any page.

It also displays the number of DB queries made on the page and also the request time in addition to many other features.

It's available for Drupal 7 and 8.

3

Simple example: It will work :)

use Drupal\Core\Url;
$form_state->setRedirect('view.VIEW_MACHINE_NAME.PAGE_MACHINENAME'); // view.my_view.page_1
1
  • No need to use Drupal\Core\Url - you are using the route correctly so the URL class is not needed.
    – kbrinner
    Jun 14, 2019 at 16:12
2

With drush you can check by path for example:

drush route | grep '/path/you/are/looking/for'
1
  • 1
    A time saver not only for views. Thx a lot!
    – makkus
    Oct 11, 2022 at 10:29
1

With drupal console:

drupal router:debug | grep '/admin/team'

This returns:

 view.team.page_1    /admin/team   // views.team.page_1 is my route

Source: https://stefvanlooveren.me/blog/get-route-views-page-drupal-console

1

For Drupal 8 you might look into the "router" database table aswell to get your redirect parameters.

Look for the corresponding value in the "name" column and compare against the path column.

$form_state->setRedirect('YOUR_DB_VALUE_HERE'); 
0

An easy trick to show the route name of every page (not just views pages) you can use hook_menu_local_tasks_alter:

function HOOK_menu_local_tasks_alter(&$data, $route_name)
{
  dump($route_name);
}

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.