0

I am using addClass() in hook_preprocess_views_view_unformatted (see my question regarding this at here), and the class is added to each row, but the next stop for the view results is views_view.html.twig, where dumping the rows data with

{{ dd(rows) }} 

shows no sign of the attributes part of the row data, which contains the class.

1 Answer 1

1

The views-view.html.twig template is at a higher level. To manipulate data at that level, you'll need to use template_preprocess_views_view().

You used template_preprocess_views_view_unformatted() which allows you to manipulate the data in views-view-unformatted.html.twig. This seems like the correct approach given your case.

Here are the contents of views-view-unformatted.html.twig from the views module in core.

{#
/**
 * @file
 * Default theme implementation to display a view of unformatted rows.
 *
 * Available variables:
 * - title: The title of this group of rows. May be empty.
 * - rows: A list of the view's row items.
 *   - attributes: The row's HTML attributes.
 *   - content: The row's content.
 * - view: The view object.
 * - default_row_class: A flag indicating whether default classes should be
 *   used on rows.
 *
 * @see template_preprocess_views_view_unformatted()
 *
 * @ingroup themeable
 */
#}
{% if title %}
  <h3>{{ title }}</h3>
{% endif %}
{% for row in rows %}
  {%
    set row_classes = [
      default_row_class ? 'views-row',
    ]
  %}
  <div{{ row.attributes.addClass(row_classes) }}>
    {{- row.content -}}
  </div>
{% endfor %}

You can see that if you loop through the rows variable, each row has an attributes property - the classes you added in template_preprocess_views_view_unformatted() will be part of it.

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.