How to check if a view is grouped? In a views template, I need to check if the views rows are grouped. Which property of the $view object contains this information?



In the example below, $grouped = !empty($options['grouping'][0]); seems to work. However I don't know/understand all details about the $view object and I'm not sure if this really is a solid solution. Will this work in all situations?

Currently, my views-view-unformatted.tpl.php looks like this:

&lt;?php
$grouped = !empty($options['grouping'][0]);
$heading_classes = $grouped ? ' class="group-heading"' : '';
?&gt;

&lt;?php if ($grouped): ?&gt;
  &lt;div class="views-group clearfix"&gt;
&lt;?php endif; ?&gt;

&lt;?php if (!empty($title)): ?&gt;
  &lt;h3&lt;?php print $heading_classes; ?&gt;&gt;&lt;?php print $title; ?&gt;&lt;/h3&gt;
&lt;?php endif; ?&gt;
&lt;?php foreach ($rows as $id =&gt; $row): ?&gt;
  &lt;div&lt;?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?&gt;&gt;
    &lt;?php print $row; ?&gt;
  &lt;/div&gt;
&lt;?php endforeach; ?&gt;

&lt;?php if ($grouped): ?&gt;
  &lt;/div&gt;
&lt;?php endif; ?&gt;
Some tips about your solution:

1- It looks right to me. I would try keep the logic separate from the template. I'd also declare my own template to avoid problems. Another style plugin, for example a custom style plugin, could use the original template.

HOOK_preprocess_views_view_unformatted(&amp;$vars){
  if(!empty($options['grouping'])){
    $vars['heading_classes'] = $grouped ? ' class="group-heading"' : '';
    // My own template
    $vars['theme_hook_suggestions'] = 'views_view_unformatted_group';
  }
}

/*
* Implement hook_theme
*/
HOOK_theme($existing, $type, $theme, $path){
  return array(
    'views_view_unformatted_group' =&gt; array(
     'variables' =&gt; array(),
     'path' =&gt; 'PATH/TO/YOUR/MODULE',
     'template' =&gt; 'views_view_unformatted_group',
  ),
} 


Template views_view_unformatted_group.tpl.php

&lt;div class="views-group clearfix"&gt;
 &lt;?php if (!empty($title)): ?&gt;
 &lt;h3&lt;?php print $heading_classes; ?&gt;&gt;&lt;?php print $title; ?&gt;&lt;/h3&gt;
&lt;?php endif; ?&gt;
&lt;?php foreach ($rows as $id =&gt; $row): ?&gt;
  &lt;div&lt;?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?&gt;&gt;
  &lt;?php print $row; ?&gt;
  &lt;/div&gt;
&lt;?php endforeach; ?&gt;
&lt;/div&gt;


2 - The default function that manage the grouping is render_grouping_sets() in an style plugin object. The documentation warn about that:


  Plugins may override this method if they wish some other way of handling
     * grouping


The unformatted list, html list, grid and table style inherit the default methods and they don't override it.

3 - If you have nested groups, Views wrap the parent group with the next function in theme.inc:

function theme_views_view_grouping($vars) {
  $view = $vars['view'];
  $title = $vars['title'];
  $content = $vars['content'];

  $output = '&lt;div class="view-grouping"&gt;';
  $output .= '&lt;div class="view-grouping-header"&gt;' . $title . '&lt;/div&gt;';
  $output .= '&lt;div class="view-grouping-content"&gt;' . $content . '&lt;/div&gt;' ;
  $output .= '&lt;/div&gt;';

  return $output;
}