2

I have a view being grouped by one field. I'm trying to theme the groups as a whole (wrap divs around the whole thing, essentially) and I did so by overriding views-view-grouping.tpl.php. However, nothing happens. Out of curiosity I added another grouping and voilá, my template override is being picked up now. Only problem is, I don't need to group by two fields, just one.

Why is it doing this? Is there a separate override for single-grouped views? What is going on and how can I get Views to pick up my override of views-view-grouping.tpl.php when grouping by only one field?

2 Answers 2

6

I've had the same issue. At first I though it was connected to the current theme I was using, but after testing with a few different themes I discovered that not to be the case. I still do not understand that behavior.

As a workaround I've used views-view-unformatted--YOURVIEWMACHINENAME.tpl.php template where you can give a wrapper to a group in a single-grouped view. Here's what I had in the template:

<?php /** * @file * Default simple view template to display a list of rows. * * @ingroup views_templates */ ?> <?php if (!empty($title)): ?> <?php //open group wrapper print '<div class="custom-class">'; ?> <h3><?php print $title; ?></h3> <?php endif; ?> <?php foreach ($rows as $id => $row): ?> <div<?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"'; } ?>> <?php print $row; ?> </div> <?php endforeach; ?> <?php if (!empty($title)): ?> <?php // close group wrapper print '</div>'; ?> <?php endif; ?> 

You can even assign a custom class to each group by taking a certain field from $view object and transforming its value into a class name.

Of course, this method will only work for a single-grouped view. If the view will be grouped by more than one field, only the most inner group will be wrapped. Hope this helps.

1

The brilliant solution by Oksana-c also works great in Drupal 8 in Twig (views-view-unformatted--my-view--my-display.html.twig):

{# /** * @file * Theme override 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() */ #} <div class="grouping-row"> {% if title %} <div class="grouping-title">{{ title }}</div> {% endif %} {% for row in rows %} {% set row_classes = [ default_row_class ? 'views-row', ] %} <div{{ row.attributes.addClass(row_classes) }}> {{ row.content }} </div> {% endfor %} </div> 
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.