Skip to main content

Drupal 8 Views: How to formulate the route name

An article from ComputerMinds - Building with Drupal in the UK since 2005
4th Oct 2016

Jo Fitzgerald

Developer
Hey, you seem to look at this article a lot! Why not Bookmark this article so you can find it easily in the future?

This article will explain how to formulate the route name for a view because there are very few sources for the information online.

The short version

"view.$view_id.$display_id"

The long(er) version

There are many reasons why you might require the route name for a view, but currently it is not very well documented. Fortunately, during my research I came across the drupal.org issue Document views routes on which this article is based. I owe many thanks to the contributors of that issue, particularly andypost who provided details of where the route name is generated (see the For Reference section below).

As explained in the short version (above), the route name for any view is
"view.$view_id.$display_id" - the string "view" followed by the View ID followed by the Display ID, each separated by a full stop ".".

The View ID is the machine name of the view, likely to be a machine readable version of the name you gave the view. E.g. If you originally gave your view the name "My example view" the the View ID will (probably) be "my_example_view". You can also find the View ID by going to the edit page for your view - the View ID will be at the end of the URL, e.g. example.com/admin/structure/views/view/my_example_view.

The Display ID determines which variation of the view we require (multiple pages etc. can be created from the same view). Very often the Display ID will be "page_1" because this is the default value given to the first page created for a view. In the UI you can check in a number of ways, including:

  1. At the top of the view edit page, click on the relevant display name - the Display ID will be at the end of the URL, e.g. example.com/admin/structure/views/view/my_example_view/edit/page_1.
  2. On the view edit page, click to reveal the ADVANCED section and, in the OTHER section, you will find the Machine Name - this is the Display ID you require.

For Reference

For those who are interested, the route name for each view is defined in \Drupal\views\Plugin\views\display\PathPluginBase::collectRoutes:


public function collectRoutes(RouteCollection $collection) { 
  $view_id = $this->view->storage->id();
  $display_id = $this->display['id']; 

  $route = $this->getRoute($view_id, $display_id);

  if (!($route_name = $this->getOption('route_name'))) {
    $route_name = "view.$view_id.$display_id";
  }
  $collection->add($route_name, $route); 
}

Hi, thanks for reading

ComputerMinds are the UK’s Drupal specialists with offices in Bristol and Coventry. We offer a range of Drupal services including Consultancy, Development, Training and Support. Whatever your Drupal problem, we can help.