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?
8 Answers
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');
– kbrinnerJun 14, 2019 at 16:13
To use views contextual filters as parameters use arg_X, fe.
$url = Url::fromRoute('view.team.page_1', ['arg_0' => 1234]);
-
2Admitting your view page path is
foo/%user/bar
, then$url = Url::fromRoute('view.team.page_1', ['user' => 1234]);
– AporieNov 11, 2020 at 15:44
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.
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.– kbrinnerJun 14, 2019 at 16:12
With drush you can check by path for example:
drush route | grep '/path/you/are/looking/for'
-
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
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');
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);
}
route_name: view.YOUR_CUSTOM_MODULE_CONTAINS_CUSTOM_CONTENT_TYPE.page_1
if page_1 is a name of page view mode.