1

I have a website running Drupal 8.x with different views. I would like to use a page--template.html.twig template basing to the view ID. Is this possible?

I was thinking of using code similar to the following.

function mythemename_theme_suggestions_page_alter(array &$suggestions, array $variables, $hook) {
 if ($hook === 'views_view') {
   if ($variables['view']->id() === 'movies') {
     $suggestions[] = 'page__movies';
   }
 }
}
2

1 Answer 1

2

First of all, the code is never run, because you have the hook page in the function name and check for the hook views_view. Secondly the variables in a page hook don't contain variables from the view, so you have to get them from the route:

function mythemename_theme_suggestions_page_alter(array &$suggestions, array $variables) {

  $view_id = \Drupal::routeMatch()->getParameter('view_id');
  if ($view_id === 'movies') {
    $suggestions[] = 'page__movies';
  }

}
1
  • Amazing! Tks so much!, again, I am just new in this and trying to improve, glad you got to understand what I needed. Xxx :) Aug 22, 2017 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.