Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

Get route name of a views page in Drupal 8 & 9 using  drupal console.

 

In this snippet I wanted to get the route route of a views page. The page is on /news. 

drupal router:debug | grep '/news'

 

This returned:

 view.news.page_1    /news

 

Now I can use this uri to set a redirection:

    $redirect_url = Url::fromRoute('view.news.page_1');
    $response = new RedirectResponse($redirect_url->toString(), 301);
    $event->setResponse($response);

 

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);

 

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

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

Comments

Submitted by Michael (not verified) on Tue, 05/03/2022 - 13:22 Permalink

Great info... thank you! I am trying to get the content from a view where the display is a page and the path is /people/%. The '%' is a reference to a taxonomy term. How do I get the content in this case? Thank you. Michael $view_id = 'person_list'; $display_id = 'page_3'; $args = [$term]; $view = Views::getView($view_id); $view->setArguments($args); $view->setDisplay($display_id); //$view->preExecute(); $view->execute(); $count = count( $view->result ); devel_dump($count);
Add new comment

Restricted HTML

Riadh Rahmi

Senior Web Developer (Drupal & Laravel)

I am a senior web developer, I have experience in planning and developing large scale dynamic web applications especially in Drupal and Laravel.

Web Posts

Search

Page Facebook