0

I created a view for my content, where fields are displayed as a page. Scanning the source code, I noticed it uses the html--default--2.html.twig template.

I created a new custom template. how can I set that template for all the view pages, instead of the default template? I need to edit the whole page, including header and footer.

Notice that I've read this and this about custom templates for view. I've tried to rename my custom template views-view--page.html.twig, and put it in my template folder, and also in /core/modules/views/templates, flushed cache, but nothing happens. My view page is still using the default template.

0

if you want to add new page template only for views:

  1. Implement hook_theme_suggestions_page_alter like the following:

/**
 * Implements hook_theme_suggestions_page_alter().
 */
function mymodule_theme_suggestions_page_alter(&$suggestions, $variables) {
  //- Get the current route name.
  $route_name = \Drupal::routeMatch()->getRouteName();
  //- Check if the current route is a view.
  if (strpos($route_name, 'view') === 0) {
    //- Add suggestion to page when its a view page.
    $suggestions[] = 'page_views_view';
  }
}

Now in your views page you will have suggestions something like:

<!-- FILE NAME SUGGESTIONS:
   x page-views-view.html.twig
   * page--en.html.twig
   * page--list-page.html.twig
   * page.html.twig
-->
  1. Now implement hook_theme like the following:

/**
 2. Implements hook_theme().
 */
function mymodule_theme($existing, $type, $theme, $path) {
  return [
    'page_views_view' => [
      'base hook' => 'page',
    ],
  ];
}

  1. Then Create page-views-view.html.twig template inside templates folder in your module root folder.

  2. Flush all cache.

  • thanks. I'm quite new to code...is correct to past your code in .theme file, in my theme folder? – Max Dec 22 '18 at 7:23
  • it's better to create custom module check examples module for developers here to see how to create custom module, and place the above code in your .module file enable the module, it will work. – berramou Dec 22 '18 at 22:03
  • thnk you. I added your code to this file, in example module you suggested me: /modules/examples/hooks_example/hooks_example.module but nothing happens, page suggestion doesn't work... "page-views-view.html.twig" doesn't appear in theme suggestion. Is correcto to replace if (strpos($route_name, 'view') === 0) { with if (strpos($route_name, 'view') == 0) { ? anyway it doesn't work...should I try with completely new custom module? – Max Dec 24 '18 at 9:17
  • change mymodule by your module name, e.g mymodule_theme($existing, $type, $theme, $path) will be hooks_example_theme($existing, $type, $theme, $path) and then enable hooks_example module, clear cache then should work. – berramou Dec 24 '18 at 9:27

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.