5

I am new to drupal 8 and i need to override a views template. So, for that I have enabled theme debug in services.yml to view the twig template suggestion name.

This is my view theme debug.

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'views_view_unformatted__test_content_view' -->
<!-- FILE NAME SUGGESTIONS:
   x views-view-unformatted--test-content-view.html.twig
   x views-view-unformatted--test-content-view.html.twig
   * views-view-unformatted.html.twig
-->

Then, I created the file in templates of drupal8 theme and the theme file calls. I used themename_views_view_unformatted__test_content_view() function to override in themename.theme file but the function doesn't called.

Is there anyway to override the variables coming to the twig template. Can anyone give me suggestion to override the views template.

Thanks in advance.

4

In your case 'views_view_unformatted__test_content_view' is the theme hook in the render array. In Drupal 8 that will make it possible to use following preprocess functions to modify the variables before they get to the template:

  • themename_preprocess_views_view_unformatted__test_content_view()
  • themename_preprocess_views_view_unformatted()

There is more information about preprocess functions available here.

0

The best place to start would be to look at the theme suggestions for views, however they're currently missing from core. This is a known bug in Drupal 8 core, currently being worked on for version 8.6

If you need the functionality to see the proper theme suggestions now, you have two options:

  1. Try the patches from comment #22 (or more recent) on Issue #2923634 and the patches on Issue #2752443. Read the comment thread on #2923634 to get a full understanding of what's going on here.

  2. Or use the well-vetted patch from comment #139 on Issue #2118743 which has been postponed in favor of option 1 above.

I'm using the patch from option 2 (#2118743) successfully. Please note that this does go against the standard core contributor recommendations. But the reason is because #2118743 has a single, well-vetted and working patch for something that is only enabled / used during development, and instead of needing to test out various combinations of two separate patches that are still under development. Once the better solution (#2923634 and #2752443) get fixed in core, #2118743 should be obviated and no longer required.

Combining this solution with @lauriii's explanation of template preprocess functions, you should be able to override the variables you need.

If that doesn't work, and you need to dig deeper, then you can alter query and or result data with the Views runtime hooks (eg hook_views_pre_render or hook_views_post_execute). Place these functions inside a module_or_themename.views_execution.inc file in your Drupal 8 custom module or theme. Please note that the documentation is currently very thin about these functions for Drupal 8, but in most cases the Drupal 7 examples can also helpful.

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.