3

Using views-view.html.twig as a starting point, I can output {{ rows }}, but I want to loop through each field of each row to aggregate and style them.

In Drupal 7, I use dpm(rows) to locate (in the array) where content, label etc. But kint doesn't seem to provide me the same information. Can anyone help?

1
  • OK, so I've discovered the template preprocess function for my view, but I'm still unsure how to output specific variables... Commented Jan 21, 2016 at 14:01

1 Answer 1

4

In views-view.html.twig is not appropriate to style your row and fields.

To style them first you need override template "views_view_'format_style'" (format style specified when you edit/create a view). Here you can style row and access 'fields' variables (use kint(row) to see them).

Something like:

{% for row in rows %} {% set row_classes = [ default_row_class ? 'views-row', ] %} {{ kint(row) }} {# devel #} <h2> {{row.content['#node'].getTitle()}} </h2> {# access fields #} <div{{ row.attributes.addClass(row_classes) }}> {{ row.content }} </div> {% endfor %} 

For style fields its better override field template.

For example field--node--body--article.html.twig:

{% set mode = element['#view_mode'] %} {% if label_hidden %} {% for item in items %} {% if mode == 'full' %} <div{{ attributes }}>{{ item.content }}</div> {% elseif mode == 'teaser' %} <p{{ attributes }}>{{ item.content }}</p> {% endif %} {% endfor %} {% else %} <div{{ attributes }}> <div{{ title_attributes }}>{{ label }}</div> {% for item in items %} <div{{ item.attributes }}>{{ item.content }}</div> {% endfor %} </div> {% endif %} 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.