Problem/Motivation

A follow-up from exploratory testing done on #1843752-32: Convert views/templates/views-view-grouping.tpl.php to twig, I've found that the grouping and grouping_level variables, which are available, but never used by the views_view_grouping template, have some fundamental issues:

  • the grouping variable produces empty results (ie, an empty string), when printed in the template. This is broken only for twig.
  • the grouping_level variable always reports the level as 0 (zero). This is broken for both twig and phptemplate.

Steps to reproduce:

This can be verified with either PHPTemplate and Twig, but the latter requires first applying a patch from #1843752: Convert views/templates/views-view-grouping.tpl.php to twig.

  1. Copy the views-view-grouping.tpl.php (or views-view-grouping.html.twig) template into Bartik's "templates" folder, so that it actually gets used.
  2. Edit the template file, adding two lines below to print out the two variables:
    PHPTemplate
    <pre>Grouping: <?php print_r($grouping); ?></pre>
    <pre>Grouping Level: <?php print $grouping_level; ?></pre>
    

    Twig

    <pre>Grouping: {{ dump(grouping) }}</pre>
    <pre>Grouping Level: {{ dump(grouping_level) }}</pre>
    
  3. Create 2 articles.
  4. Edit front page view to have grouping on at least two fields (grouping on one field won't trigger the view-grouping markup). In the screenshot below, I used fields Node date, with the HTML Hours time format for the first level, and Node author name for the second level (which required a views relationship on the author of the post).
  5. View the front page.

views view grouping levels always zero

Proposed resolution

What triggered the discovery of this issue was the lack of documentation for the grouping variable in the template itself. The template's docblock documents this variable as "The grouping instruction", which is close to meaningless; and, after printing the variable to try to understand it, it is always empty.

So the proposed resolution would be to:

  1. Replace the grouping Array variable with separate individual variables useful to themers and improve the docblock.
  2. Fix grouping_level variable so it increments properly for each sub-grouping. The value should be equal to the grouping number "n" used in Views UI, minus 1.

Remaining tasks

User interface changes

None.

API changes

None.

Meta issue: #1843738: [meta] Convert views module to Twig
Discovered during #1843738: [meta] Convert views module to Twig
Discovered by #1843752: Convert views/templates/views-view-grouping.tpl.php to twig

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jwilson3’s picture

Issue summary: View changes

the grouping is only broken by twig.

jwilson3’s picture

Issue summary: View changes

updated proposed resolution

jwilson3’s picture

The Grouping Array in the template is supposed to look like:

Grouping: Array
(
    [field] => created
    [rendered] => 0
    [rendered_strip] => 1
)

rendered_strip and rendered are values that themers don't need to know about (or do they?). The field variable however could be useful. I propose we remove the grouping variable and replace it with a grouping variable, which would take the $grouping['field'], prefix it with "grouping-", and pass it through drupal_html_class(), to end up with a useful class name such as grouping-created.

jwilson3’s picture

Issue summary: View changes

fix resolution

jwilson3’s picture

Issue tags: +Twig, +VDC, +VDC-cleanup

A couple tags.

jwilson3’s picture

Issue summary: View changes

added link to meta issue

joelpittet’s picture

I'd like to know more of what these do too. A little sluething produced where they are being built:

core/modules/views/lib/Drupal/views/Plugin/views/style/StylePluginBase.php
public function renderGroupingSets($sets, $level = 0) {

       $output[] = array(
          '#theme' => $theme_functions,
          '#view' => $this->view,
          '#grouping' => $this->options['grouping'][$level],
          '#grouping_level' => $level,
          '#rows' => $set['rows'],
          '#title' => $set['group'],
        );
tstoeckler’s picture

tstoeckler’s picture

Status: Active » Closed (duplicate)