3

Consider, I have created two pages using Views for content types Activites and Articles viz. Latest Activities and Latest Articles. I have made a view to display the grid structure for these contents. However, if I modify the views-view-grid.html.twig, it affects the both views. What twig file I should create so that I work on separate view for each pages? eg. something_view1.html.twig for Activities and something_view2.html.twig. Thanks.

2

Views uses a different mechanism to provide variations of twig templates that doesn't work yet with the debug feature from twig.

The logic that builds those suggestions is in \Drupal\views\ViewExecutable::buildThemeFunctions(), you can debug that or ad some php statements to dump the $themes variable for a certain hook to get what you need.

0

For this the best approach would be to use the template suggestions added by the devel module. Here is a tutorial for that: https://www.drupal.org/docs/8/theming/twig/working-with-twig-templates

Also see the Twig Template naming conventions: https://www.drupal.org/node/2354645

0

You can edit the services.yml file in sites/default and set debug to true. Doing so will add twig template suggestions in the page source of every page. It will also tell you what template is being used where. Here's an example of what some of the output will look like when you enable debug mode.

<!-- END OUTPUT from 'core/themes/stable/templates/misc/status-messages.html.twig' -->
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'block' -->
<!-- FILE NAME SUGGESTIONS:
   * block--custom_theme-content.html.twig
   * block--system-main-block.html.twig
   * block--system.html.twig
   x block.html.twig
-->
<!-- BEGIN OUTPUT from 'core/themes/stable/templates/block/block.html.twig' -->
<div id="block-custom_theme-content">
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'node' -->
<!-- FILE NAME SUGGESTIONS:
   * node--41--full.html.twig
   * node--41.html.twig
   * node--page--full.html.twig
   x node--page.html.twig
   * node--full.html.twig
   * node.html.twig
-->
<!-- BEGIN OUTPUT from 'themes/custom/custom_theme/templates/node--page.html.twig' -->

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'field' -->
<!-- FILE NAME SUGGESTIONS:
   * field--node--title--page.html.twig
   x field--node--title.html.twig
   * field--node--page.html.twig
   * field--title.html.twig
   * field--string.html.twig
   * field.html.twig
-->
<!-- BEGIN OUTPUT from 'core/themes/stable/templates/field/field--node--title.html.twig' -->
<span property="schema:name" data-quickedit-field-id="node/41/title/en/full">Navigator Newsletter</span>

<!-- END OUTPUT from 'core/themes/stable/templates/field/field--node--title.html.twig' -->
</h2>
0

You can create new view template with some naming conventions. You can check this link. It have useful information.

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

0

if you modify this file views-view-grid.html.twig it is not affecting only the both views page it would affect all the views with grid format, Your twig file name should be in proper format,

Format : views-view-{format-type}--{view-name}--{id}.html.twig

  • format-type = grid, table, unformatted etc..,
  • view-name = machine name of the view,
  • id = Unique id for the particular views page or block.

Samples :

views-view-unformatted--portfolio--page.html.twig
views-view-unformatted--portfolio--page1.html.twig
views-view-unformatted--portfolio--block.html.twig
views-view-unformatted--portfolio--block1.html.twig

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.