Score:0

Creating twig files for the a view

il flag

Hi there I'm new to Drupal. I have made a test project, where I've made a custom content-type called 'persons' it has a couple of values. And I made it able to custom style it by making a twig file called 'node--persons.html.twig'. Now I have built a listing view, where I loop through all the 'persons'. It's called 'People view' But I can't get it to work to make a working twig file that is able to properly style every person item in a loop. I've already tried making twig files like views_view--people_view.html.twig etc. But nothing actually seemed to work. I'm very confused about all this stuff and also whenever I need to use a machine_name or so. Can someone help me?

Score:0
in flag

First, I recommend manipulating twig as the ultimate last resort. Drupal provides some core and third-party modules that allow you to manipulate how an entity is rendered:

  • Layout Builder allows you to manipulate an entity's display with layouts.
    • This allows you to create layouts, and use layouts to position fields and blocks on the page.
  • Drupal has a very predictable field structure (wrapper, label, value).
  • The Fences module allows you to manipulate field markup, add classes, etc.
  • Views, just like fields, also has a very predictable markup structure.
  • Views also has a built-in way to change field markup, add classes.

I would exhaust those options first before resorting to twig, as those options allow you to manipulate the appearance of an entity without hardcoding everything into a twig file.

As for Views, it has two ways of rendering an entity. You either go the fields route where you render individual fields, or you surrender rendering to the entity where the the entity Displays are rendered instead of fields.

Drupal 8+ also has a bug where theme suggestions for various parts of the view are not printed in HTML comments. You can find these suggestions here. For convenience, here are some of the options:

views-view--foobar--page.html.twig
views-view--page.html.twig
views-view--foobar.html.twig
views-view.html.twig

views-view-unformatted--foobar--page.html.twig
views-view-unformatted--page.html.twig
views-view-unformatted--foobar.html.twig
views-view-unformatted.html.twig

views-view-fields--foobar--page.html.twig
views-view-fields--page.html.twig
views-view-fields--foobar.html.twig
views-view-fields.html.twig
Jorn Reed avatar
il flag
Thanks, I managed to get it working by using the right template files (I copied them from a core theme). And added some random html to see if it got printed on the right place. And it works great! Also another thing. I'm building a module that renders pages on custom routes. But does drupal provide a way in controllers to pass data from one page to another and back?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.