I propose something like

$output .= '<div class="view-grouping-header view-grouping-header-' . $grouping_level . '">' . $title . '</div>';

It would allow me to style the levels differently.

I don't know yet, where to get the grouping level, and if theme.inc is the right place to edit.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kanani’s picture

there's a views/theme/views-view-grouping.tpl.php file that theoretically could be used to add the $grouping_level.

 /**
  * This template is used to print a single grouping in a view. It is not
  * actually used in default Views, as this is registered as a theme
  * function which has better performance. For single overrides, the
  * template is perfectly okay.
  *
  * Variables available:
  * - $view: The view object
  * - $grouping: The grouping instruction.
  * - $grouping_level: Integer indicating the hierarchical level of the grouping.
  * - $rows: The rows contained in this grouping.
  * - $title: The title of this grouping.
  * - $content: The processed content output that will normally be used.
  */
?>
<div class="view-grouping">
  <div class="view-grouping-header"><?php print $title; ?></div>
  <div class="view-grouping-content">
    <?php print $content; ?>
  </div>
</div>

template_preprocess_views_view_grouping() is what passes the value of $grouping_level through. However, in my views, I'm only ever seeing a value of 0

function template_preprocess_views_view_grouping(&$vars) {
  $vars['content'] = $vars['view']->style_plugin->render_grouping_sets($vars['rows'], $vars['grouping_level']);
}
robdubparker’s picture

Any luck with this? I'm seeing all 0 values as well.

goldenboy’s picture

Category: feature » bug
FileSize
1.27 KB

The groping_level is always set to 0.
Looking at the code there is a function in the style plugin that should use the group level as a param. Here the definition:

  /**
   * Render the grouping sets.
   *
   * Plugins may override this method if they wish some other way of handling
   * grouping.
   *
   * @param $sets
   *   Array containing the grouping sets to render.
   * @param $level
   *   Integer indicating the hierarchical level of the grouping.
   *
   * @return string
   *   Rendered output of given grouping sets.
   */
  function render_grouping_sets($sets, $level = 0) {
    ...
  }

The default value for the param is 0 and there are no calls that pass the group level on.

Also, this function is called just one time per view. So, even if the group level is passed on, it will have always the same value.

Maybe the signature of this function should be changed and the group level should be passed on through the sets param.
The sets param is built by the render_grouping function. I think that could be the right place to do it.

The attached patch fix this behavior just for the views_plugin_style.inc file. I left the signature of the render_grouping_sets function the same so nothing will be broken.

goldenboy’s picture

Status: Active » Patch (to be ported)
goldenboy’s picture

Status: Patch (to be ported) » Needs work
FileSize
1.27 KB

Sorry I made a little bit of confusion with the status values.
I re-attach the patch and set the status correctly.

scotwith1t’s picture

/* hehe @ #3, groping_level */

so, related comment? if i group level one, it's a table caption, but if it's level 2 it's using the div.views-grouping-header being discussed here...is this related? is there a reason it's that way at level 1 and level 2 uses the div?

lunk rat’s picture

Thanks, patch in #5 worked for me on 7.x-3.3. What I did:

Apply the patch in #5.
Copy modules/views/theme/views-view-grouping.tpl.php into my theme's templates directory.
Edit the copy of views-view-grouping.tpl.php so it looks like this:

<?php
 /**
  * This template is used to print a single grouping in a view. It is not
  * actually used in default Views, as this is registered as a theme
  * function which has better performance. For single overrides, the
  * template is perfectly okay.
  *
  * Variables available:
  * - $view: The view object
  * - $grouping: The grouping instruction.
  * - $grouping_level: Integer indicating the hierarchical level of the grouping.
  * - $rows: The rows contained in this grouping.
  * - $title: The title of this grouping.
  * - $content: The processed content output that will normally be used.
  */
?>
<div class="view-grouping group-level-<?php print $grouping_level; ?>">
  <div class="view-grouping-header"><?php
  print $title; ?></div>
  <div class="view-grouping-content">
    <?php print $content; ?>
  </div>
</div>

Clear views + theme cache.

Then I target the group-level-* class in my css.

rongok’s picture

It still give me 0.

rongok’s picture

Now it works fine by adding $level++;

Thanks for the patch.

chrsnlsn’s picture

Where did you add $level++ I added the patch cleared caches and am still just getting 0?

rongok’s picture

Inside foreach() in render_grouping_sets function of views_plugin_style.inc

jibran’s picture

Title: Add class with grouping level to view-grouping-header » Grouping level is always zero in views_plugin_style::render_grouping_sets()
Status: Needs work » Needs review
FileSize
535 bytes

Let's fix the bug first.

dawehner’s picture

@Jibran
I think your fix will not really work, as it's just adding 1 all the time, so if level is 0 at the moment it will be just 1 all the time.

http://drupal.org/files/views-grouping-level-always-is-zero-1488744-3_0.... looks like a better fix,
though it doesn't seem to work for some people.

pmcdougl’s picture

Patch #5 works for me!

wooody’s picture

I think It will be better if we don't patch views files...
just copy this code and put it in your theme within file name views-view-grouping.tpl.php

<?php
/**
  * This template is used to print a single grouping in a view. It is not
  * actually used in default Views, as this is registered as a theme
  * function which has better performance. For single overrides, the
  * template is perfectly okay.
  *
  * Variables available:
  * - $view: The view object
  * - $grouping: The grouping instruction.
  * - $grouping_level: Integer indicating the hierarchical level of the grouping.
  * - $rows: The rows contained in this grouping.
  * - $title: The title of this grouping.
  * - $content: The processed content output that will normally be used.
  */
?>
<?php //dpm(get_defined_vars()); ?>
<div class="view-grouping group-level-<?php print $id; ?>">
  <div class="view-grouping-header"><?php
  print $title; ?></div>
  <div class="view-grouping-content">
    <?php print $content; ?>
  </div>
</div>
wooody’s picture

Issue summary: View changes

tagging the code

solesum’s picture

Issue summary: View changes

Thanks Wooody!

aquasplash’s picture

id is the wrong approach.

If you have more than one group, you get a bad result.

I tried a little bit to improve one of the patches.

Probably can be done more accurately.

But I managed to get the correct result in this way.

koechsle’s picture

Still a problem with Views 7.x-3.8.

Rather than patching Views or using $id (which only works for one group anyway) I managed to solve this by adding this to my theme's template.php.
It overrides the corresponding views theme function. The only difference is adding 1 to $vars['grouping_level'].

function MYTHEME_preprocess_views_view_grouping(&$vars) {
$vars['content'] = $vars['view']->style_plugin->render_grouping_sets($vars['rows'], $vars['grouping_level']+1);
}

lunk rat’s picture

YES! @koechsle your #20 is perfect, thank you so much! I was using #5 for over two years and just today I needed to update views and became very annoyed that I had to patch views just so that my CSS doesn't break, every time I update views.

So your template.php solution is a total win. Thanks again.

s.reichert’s picture

This Bug still exists in Drupal 8.
#2508145: Grouping level is always zero in Views (Patch available)

Cracu’s picture

#20 is not working for me, but I used that hook to provide the correct grouping_level.

/**
 * Calculate current grouping level.
 *
 * This works like a patch for views contrib module, considering that
 * $grouping_level is always 0 in the views-view-grouping.tpl.php template.
 *
 * @see my_module_preprocess_views_view_grouping()
 *
 * @param array $rows
 *   View rows.
 * @param int $level (optional)
 *   Current group level.
 *
 * @return int
 *   Group level.
 */
function _my_module_get_views_grouping_level($rows, $level = 0) {
  $first_row = reset($rows);
  if (is_array($first_row) && isset($first_row['group']) && !empty($first_row['rows'])) {
    $level++;
    return _my_module_get_views_grouping_level($first_row['rows'], $level);
  }
  return $level;
}

/**
 * Implements hook_preprocess_views_view_grouping().
 *
 * Correct the grouping level.
 */
function my_module_preprocess_views_view_grouping(&$vars) {
  $grouping_level = _my_module_get_views_grouping_level($vars['rows']);
  $vars['grouping_level'] = $grouping_level;
}

Helper function will simply parse the $vars['rows'] until no 'group' key is found in the first element.

group_levels will start with 1 -> that being the first wrapper of the content

discipolo’s picture

#23 seems to work for me on initial test

tstoeckler’s picture

Seems like a duplicate of #2508145: Grouping level is always zero in Views ?

Edit: Sorry, wrong issue

jibran’s picture

Version: 7.x-3.3 » 7.x-3.x-dev
Issue tags: +Needs reroll

#2508145: Grouping level is always zero in Views got committed so this patch needs a reroll.

BrightBold’s picture

Thanks for #20! A perfect workaround until this patch can get in.

Tess Bakker’s picture

New patch, with changes to the code and test as suggested in the D8 patch.

scotwith1t’s picture

Love to see this get in! Also wondering about a group count class, like group-1, group-2, group-3, etc...perhaps odd/even group classes as well? first/last groups? might as well go all the way! :P

alex.skrypnyk’s picture

Status: Needs review » Reviewed & tested by the community

#28 passed test and is actually working. Thank you! Marking for RTBC.

a.milkovsky’s picture

+++ b/plugins/views_plugin_style.inc
@@ -355,6 +355,7 @@ class views_plugin_style extends views_plugin {
+      $level = isset($set['level']) ? $set['level'] : 0;

Better fallback to the default.

$level = isset($set['level']) ? $set['level'] : $level;

Else looks fine for me.

a.milkovsky’s picture

Fixed the issue from my last comment.

MustangGB’s picture

D8 does it as per #28 rather than #32, not sure of the relevance/consequence of this, just pointing it out.

  • DamienMcKenna committed 4a603f4 on 7.x-3.x
    Issue #1488744 by goldenboy, jibran, a.milkovsky, aquasplash, Tessa...
DamienMcKenna’s picture

Status: Reviewed & tested by the community » Fixed

Going to go with #28, because it matches what D8 does. If this needs further work to change the default, please open a new issue for Views-D7 and for core-D8 so both can be the same.

Committed. Thanks!

LAguilar’s picture

For my purposes #17 it worked for me

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.