14

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?

  • This is also required for static YAML menu configuration. – vijaycs85 Sep 27 '17 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. – Jean-Luc Barat Aug 8 at 18:48
37

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)

  • 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 at 16:13
5

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

$url = Url::fromRoute('view.team.page_1', ['arg_0' => 1234]);
3

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.

2

Simple example: It will work :)

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

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

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

$from_state->setRedirect('YOUR_DB_VALUE_HERE'); 

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.