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 as0
(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.
- 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.
- 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>
- Create 2 articles.
- 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).
- View the front page.
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:
- Replace the grouping Array variable with separate individual variables useful to themers and improve the docblock.
- 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.
Related Issues
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
Comment | File | Size | Author |
---|---|---|---|
views-view-grouping-levels-always-zero.png | 163.78 KB | jwilson3 |
Comments
Comment #0.0
jwilson3the grouping is only broken by twig.
Comment #0.1
jwilson3updated proposed resolution
Comment #1
jwilson3The Grouping Array in the template is supposed to look like:
rendered_strip
andrendered
are values that themers don't need to know about (or do they?). Thefield
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.Comment #1.0
jwilson3fix resolution
Comment #2
jwilson3A couple tags.
Comment #2.0
jwilson3added link to meta issue
Comment #3
joelpittetI'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) {
Comment #4
tstoecklerSeems like a duplicate of #2508145: Grouping level is always zero in Views ?
Comment #5
tstoeckler