0

I am trying to figure out if there is a way to override the base templates for one view.
I created a page with a view called 'Promotions'.
I need to override the views-view-field.html.twig, views-view-fields.html.twig, and views-view-unformatted.html.twig for just the one view/page.

I read about putting it into your custom theme's template folder in it's own folder that mimics the page name.
In my case, following the example it was views-view-fields--promotions--page.

The overrides work but they work for all the views (pages and blocks) that I created.
Can you narrow it down to just one view/page?

1

Yes.

The pattern will be something like views-view-promotions--display_name.html.twig

See https://api.drupal.org/api/drupal/core%21modules%21views%21views.theme.inc/group/views_templates/8.2.x

Also, I recommend you activate Twig debugging (https://www.drupal.org/docs/8/theming/twig/debugging-twig-templates) to see actual suggestions.

|improve this answer|||||
  • Thank you! I'll give that a try. I have twig debugging on but it doesn't give me suggestions when I am down to the unformatted, fields, and field.html.twig levels – Wally Kolcz Mar 12 at 20:06
  • Those finer grained templates may have been eliminated by your wider scale template, dependent on what did. Remove it temporarily to see if the base gives you the suggestions. – beltouche Mar 12 at 20:13
1

To override templates for one view page or block, i guess you should add another suggestions like views-view-fields--VIEW_NAME--DISPLAY_NAME, to do so you should implement hook_theme_suggestions_HOOK_alter.
here is an example for views_view_fields hook:

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function MODULENAME_theme_suggestions_views_view_fields_alter(array &$suggestions, array $variables) {
  $view_id = \Drupal::routeMatch()->getParameter('view_id');
  $display_id = \Drupal::routeMatch()->getParameter('display_id');
  $suggestions[] = 'views_view_fields__' . $view_id . '__' .  $display_id;
} 

Now if you have a view called Promotions and a display called page you will get the following suggestion:

views-view-fields--promotions--page

you can do the same to views_view_unformatted hook.

|improve this answer|||||
-1

Hey i think you need to override only one node template,

If i am not wrong then,you need to override only node page for eg.you created 'Promotion' node page and in your case your 'Promotion' page node has eg. 101.

According to that node '101' will be override.

eg. node--101.html.twig or node--promotion.html.twig

Note - You don't need to create views view for this.

Hope,this will help you.

|improve this answer|||||

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.