0

I created a custom twig template to override views-view-fields in an unformatted view. What I would like to do is get rid of the rendering loop and explicitly display the variables in certain positions. I used dump() to list the variables, and am seeing a list like

  'field_pf_site_url' => 
    object(Drupal\views\Plugin\views\field\EntityField)[4373]
      ...
  'title' => 
    object(Drupal\views\Plugin\views\field\EntityField)[4375]
      ...

So I tried to display the title field with

title3: {{ title }}<br />

"title3" is displayed but not the variable. How can I correctly render it? I also have a field that is an array of images; are there special tricks to displaying them?

7
  • 2
    Why not just use a rendered entity and view mode in Views?
    – Kevin
    Feb 23, 2022 at 18:50
  • I tried that, but still don't have the control of the fields I'd like. I made a two-column layout, and have eight fields in the first column. The only thing that gets displayed is {{ content.first }}. What I'd really like to do is break out the individual fields and put each in a div that I could apply a custom style to.
    – pglatz
    Feb 24, 2022 at 0:23
  • 1
    In Views template you'll find the rendered markup in markup objects. This is what you are supposed to print and you can wrap divs around them. The inside of the markup is configured in UI and there are plenty of options to add divs and classes to apply custom styles. You can even rewrite the entire field content with tokens if you need to.
    – 4uk4
    Feb 24, 2022 at 10:36
  • Are you talking about the views_templates module? I can't find much documentation on it. How is this different than just creating an override for views-view-fields.html.twig? I tried that, but can't figure out the syntax to grab an individual field variable to display it.
    – pglatz
    Feb 24, 2022 at 18:29
  • 1
    I'm talking about the core templates, each one has a default which demonstrates what should be printed. By the way, you can only print fields which are configured in the View. They are not necessarily the same as the entity. Both in names and numbers. You can configure entity fields multiple times with different names (which is not possible in content types) or add computed fields not directly connected to an entity field.
    – 4uk4
    Feb 24, 2022 at 20:28

1 Answer 1

2

To sum up the comments. When customizing the output of a View based on fields (not on entities rendered in view modes as suggested by @Kevin), don't start with templating too early. First use the options available in Views. The core field plugins provide a lot of options, even to rewrite the entire field content using tokens of the same field or any other field defined before (which you can hide from display if you want to use it only as token).

You can also use style plugins to show fields in columns. If the core styles don't work for you add more contrib or custom styles. See https://www.drupal.org/docs/creating-custom-modules/building-a-views-display-style-plugin-for-drupal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.