Hi,

I used this module with views 7.x-3.0-rc3, worked perfect.

In the official views 7.x-3.0 release, this module breaks: the content in my views block is empty. Switching to HTML format in my view the view is created.

Using Grouping Field (with Limit) there is the error:

    Notice: Undefined index: group in views_plugin_style->render() (regel 251 van /homel/public_html/drupal-7/sites/all/modules/views/plugins/views_plugin_style.inc).
    Notice: Undefined index: rows in views_plugin_style->render() (regel 254 van /home/public_html/drupal-7/sites/all/modules/views/plugins/views_plugin_style.inc).
    Warning: Invalid argument supplied for foreach() in views_plugin_style->render() (regel 254 van /home/public_html/drupal-7/sites/all/modules/views/plugins/views_plugin_style.inc).
    Notice: Undefined index: group in views_plugin_style->render() (regel 251 van /home/public_html/drupal-7/sites/all/modules/views/plugins/views_plugin_style.inc).
    Notice: Undefined index: rows in views_plugin_style->render() (regel 254 van /home/public_html/drupal-7/sites/all/modules/views/plugins/views_plugin_style.inc).
    Warning: Invalid argument supplied for foreach() in views_plugin_style->render() (regel 254 van /home/public_html/drupal-7/sites/all/modules/views/plugins/views_plugin_style.inc).

Comments

mxh’s picture

same problem here. i use unformatted list, grouping by rendered date fields. worked withoud problems in rc3.
Notice: Undefined index: group in views_plugin_style->render() (Zeile 251 von .../sites/all/modules/views/plugins/views_plugin_style.inc).

hope this bug will be fixed soon.

Thelrin’s picture

Same issue. Following.

matrlx’s picture

I was having the same problem.. just fixed it.

Find this:

function render_grouping($records, $grouping_field = '') {
    $this->render_fields($this->view->result);
    $sets = array();
    if ($grouping_field) {
      foreach ($records as $index => $row) {
        $grouping = '';
        if (isset($this->view->field[$grouping_field])) {
          $grouping = $this->get_field($index, $grouping_field);
          if ($this->view->field[$grouping_field]->options['label']) {
            $grouping = $this->view->field[$grouping_field]->options['label'] . ': ' . $grouping;
          }
        }
        $sets[$grouping][$index] = $row;
      }
    }
    else {
      $sets[''] = $records;
    }

Replace it, with this code:

function render_grouping($records, $grouping_field = '', $group_rendered = NULL) {
    $this->render_fields($this->view->result);
    $sets = array();
    if ($grouping_field) {
      foreach ($records as $index => $row) {
        $grouping = '';
        if (isset($this->view->field[$grouping_field])) {
          $group_content = $this->get_field($index, $grouping_field);
          if ($this->view->field[$grouping_field]->options['label']) {
            $group_content = $this->view->field[$grouping_field]->options['label'] . ': ' . $group_content;
          }
          if ($group_rendered) {
            $grouping = $group_content;
          }
          else {
            $grouping = $this->get_field_value($index, $grouping_field);
          }
          if (empty($sets[$grouping]['group'])) {
            $sets[$grouping]['group'] = $group_content;
          }
        }
        $sets[$grouping]['rows'][$index] = $row;
      }
    }
    else {
      $sets[''] = array(
        'group' => '',
        'rows' => $records,
      );
    }

Now it`s working!! :)

At least, for me..

svenryen’s picture

Hi!

Thanks for sharing the fix.
In order to properly limit the items,
I also had to change this:

    foreach ($sets as $group => $rows) {
      $output[$group] = array_slice($rows, $this->options['grouping-offset'], $this->options['grouping-limit'], TRUE);
    }

to

    foreach ($sets as $group => $rows) {
      $output[$group]['group'] = $rows['group'];
      $output[$group]['rows'] = array_slice($rows['rows'], $this->options['grouping-offset'], $this->options['grouping-limit'], TRUE);
    }
matrlx’s picture

Great! I was having difficulty on limit the number of rows!

Thank you!

Anonymous’s picture

#3 + #4 worked for me, thanks.

kalmarr’s picture

I same error, but I can't this code (which line have to change?) the new version....(7.x-3.x-dev) Can You make a path?

THX

KALMI

matrlx’s picture

kalmarr,
open modules/views_limit_grouping/views_limit_grouping_style_plugin.inc
The code on #3 starts at line 47.

The code on #4 starts at line 78.

redndahead’s picture

Status: Active » Needs review
StatusFileSize
new2.04 KB

Here is a patch.

redndahead’s picture

StatusFileSize
new2.34 KB

forgot a portion

bschilt’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! The patch in #10 worked for me.

melon’s picture

Thanks @redndahead, the patch is working great for me as well. Please commit.

shawn dearmond’s picture

This is working, but I'm getting a notice repeated a bunch of times:

Notice: Undefined offset: 1 in include() (line 13 of /path/to/drupal/sites/all/modules/views_limit_grouping/views-limit-grouping.tpl.php).

It's repeating a bunch of times, each with different offsets: 1, 8, 18, 31, 42, 52, 58, 64, and 71.

If this is unrelated to this issue, I'd be happy to open up a new issue.

timmetj’s picture

I got the same notices.. I changed $classes[$id] to $classes on line #13 in .tpl (for me i dont care about the classes)
The notice is because the $id is not reset properly, it takes the "normal" rowID, and doesn't reset it (i think)

for me I also had to change $grouping_field into $grouping_field[0]['field'] between line #53 and #62. Using latest views 7.x-3.1 and drupal 7.12

Too bad this module isn't updated/invested that much. I normally write my own dirty code in the template to limit rows per group :)
This module could be a good work arround, but needs some rework/update first. As far of now you can use the dev and add the markups mentioned in this thread

poorva’s picture

#14 works for me .
thanks !!

shawn dearmond’s picture

StatusFileSize
new2.86 KB

Here's a re-rolled patch that includes #10 and #14.

theunraveler’s picture

Sorry to have missed this, and thanks to all involved in producing the patch. I'll commit this in the next couple days and release a new version.

kruser’s picture

I applied the patch in #16, but now the module isn't grouping anything. If I set the Items to display (ex. 3), then it only shows three rows, not a grouping with 3 rows each.

tommy kaneko’s picture

I had problems getting this to work also. I had a situation where I needed to nest multiple groups, and wanted to limit the number of rows within each successive group. The following solution made it work for me. I took the function render_grouping() from the views_plugin_style.inc that comes with Views and adapted it for my purposes.

I think a little more work needs to be done to the code below to get it "production worthy". For example, at the moment, there is one grouping limit option applied to all nested groups. This should eventually be more granular, so that the user can limit the number of rows within each grouping field.

In any case, my temporary solution, in views_limit_grouping_style_plugin.inc, replace the entire function:

<?php
function render_grouping($records, $groupings = array(), $group_rendered = NULL) {
...
}
?>

With the following two functions.

<?php
  function render_grouping($records, $groupings = array(), $group_rendered = NULL) {
    // This is for backward compability, when $groupings was a string containing
    // the ID of a single field.
    if (is_string($groupings)) {
      $rendered = $group_rendered === NULL ? TRUE : $group_rendered;
      $groupings = array(array('field' => $groupings, 'rendered' => $rendered));
    }

    // Make sure fields are rendered
    $this->render_fields($this->view->result);
    $sets = array();
    if ($groupings) {
      foreach ($records as $index => $row) {
        // Iterate through configured grouping fields to determine the
        // hierarchically positioned set where the current row belongs to.
        // While iterating, parent groups, that do not exist yet, are added.
        $set = &$sets;
        foreach ($groupings as $info) {
          $field = $info['field'];
          $rendered = isset($info['rendered']) ? $info['rendered'] : $group_rendered;
          $rendered_strip = isset($info['rendered_strip']) ? $info['rendered_strip'] : FALSE;
          $grouping = '';
          $group_content = '';
          // Group on the rendered version of the field, not the raw.  That way,
          // we can control any special formatting of the grouping field through
          // the admin or theme layer or anywhere else we'd like.
          if (isset($this->view->field[$field])) {
            $group_content = $this->get_field($index, $field);
            if ($this->view->field[$field]->options['label']) {
              $group_content = $this->view->field[$field]->options['label'] . ': ' . $group_content;
            }
            if ($rendered) {
              $grouping = $group_content;
              if ($rendered_strip) {
                $group_content = $grouping = strip_tags(htmlspecialchars_decode($group_content));
              }
            }
            else {
              $grouping = $this->get_field_value($index, $field);
              // Not all field handlers return a scalar value,
              // e.g. views_handler_field_field.
              if (!is_scalar($grouping)) {
                $grouping = md5(serialize($grouping));
              }
            }
          }

          // Create the group if it does not exist yet.
          if (empty($set[$grouping])) {
            $set[$grouping]['group'] = $group_content;
            $set[$grouping]['rows'] = array();
          }

          // Move the set reference into the row set of the group we just determined.
          $set = &$set[$grouping]['rows'];
        }
        // Add the row to the hierarchically positioned row set we just determined.
        $set[$index] = $row;
      }
    }
    else {
      // Create a single group with an empty grouping field.
      $sets[''] = array(
        'group' => '',
        'rows' => $records,
      );
    }

    // If this parameter isn't explicitely set modify the output to be fully
    // backward compatible to code before Views 7.x-3.0-rc2.
    // @TODO Remove this as soon as possible e.g. October 2020
    if ($group_rendered === NULL) {
      $old_style_sets = array();
      foreach ($sets as $group) {
        $old_style_sets[$group['group']] = $group['rows'];
      }
      $sets = $old_style_sets;
    }
    // Apply the offset and limit.
    $output = array();
    $output = $this->group_limit_recursive($sets, $this->options['grouping-offset'], $this->options['grouping-limit'], TRUE);

    return $output;
  }


  /**
   * Recursively limits the number of rows in nested groups.
   */
  function group_limit_recursive($rows, $grouping_offset, $grouping_limit) {
    $output = array();
    $output = array_slice($rows, $grouping_offset, $grouping_limit, TRUE);
    
    foreach ($output as $key => $row) {
      if (is_array($row) && isset($row['rows'])) {
        $output[$key]['rows'] = $this->group_limit_recursive($row['rows'], $grouping_offset, $grouping_limit);
      }
    }
    
    return $output;
  }
?>

EDIT: I corrected a piece in the above code. The code above "forgot" to limit the first grouping field.

xaqrox’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new8.14 KB

Building on the work Tommy Kaneko did in #16, I added the ability to choose a different limit and offset for each grouping level. It's done enough for my purposes, but it's definitely not done, and I'm hoping someone with more Views plugin experience can help.

I was able to add sections to the settings form to choose values for the limit and offset, and the view preview and a fully rendered view on a page exhibit the desired functionality. However, if you go back to the settings form to edit the values you just entered, they are reset to the defaults. I commented out the implementation of option_definition(), because I wasn't sure what was the proper way to describe the new options I was adding, which I'm guessing is necessary to get the form to work right.

Anyway, my patch including Tommy Kaneko's work and my edits is attached.

sun-fire’s picture

#20 works for me

toddtomlinson’s picture

Try patch in #20 below. I had the same problem - only displayed 3 items for the first group and no successive groups. #20 worked for me and fixed the problem.

theunraveler’s picture

Status: Needs work » Closed (fixed)

I just pushed some work inspired by the patch in #20 to the 7.x-1.x branch. Please note that once you update, you will have to re-save any views that use group limiting, because I had to change the data structure a bit.

Thanks all for your hard work and clever solutions.

hanskuiters’s picture

7.x-1.x-dev works without any problems (so far). Thanks for this nice module.