Problem/Motivation

hook_preprocess_views_view_grouping() is never invoked for single-level grouping, regardless of the display type (page, block, etc.).

The root cause is in StylePluginBase::renderGroupingSets(). A conditional branch decides which theme hook to use for each grouping set:

  • Branch A (is_array($row) && isset($row['group'])): creates a #theme => views_view_grouping render array. Only reached when the current set's rows are themselves nested grouping sets (i.e. with 2+ grouping levels, for non-leaf levels only).
  • Branch B (else): calls renderRowGroup(), which uses the style's own theme (e.g. views_view_unformatted). Always taken for single-level grouping, and for the leaf level of any multi-level grouping.

Because views_view_grouping is never rendered in these cases, hook_preprocess_views_view_grouping() is never triggered.

With 2 grouping levels, the hook fires once (for level 0). Level-1 titles are accessible indirectly via $variables['content'][x]['#title'] within the level-0 hook call, but views_view_grouping is never properly invoked for the leaf level.

Steps to reproduce

  1. Create a view with any display (page or block).
  2. Configure the style (e.g. Unformatted list) with one grouping field.
  3. Implement hook_preprocess_views_view_grouping() in a custom module.
  4. The hook is never called.

Proposed resolution

Two changes are needed:

  1. In StylePluginBase::renderGroupingSets(), when grouping is configured and branch B is taken (leaf rows), always build a #theme => views_view_grouping render array instead of delegating directly to renderRowGroup().
    Rows should be pre-rendered with the row plugin first, as currently done.
  2. In ViewsThemeHooks::preprocessViewsViewGrouping(), detect whether $variables['rows'] contains nested grouping sets or already-rendered row render arrays:
    • nested sets → existing recursive renderGroupingSets() call
    • pre-rendered rows → wrap them with $style->themeFunctions() directly (no new public API needed)

A change record will be needed: for single-level grouping, the group title currently rendered by views_view_unformatted (inside an <h3>) moves to views_view_grouping (which currently outputs a bare {{ title }}).

Note: template_preprocess_views_view_grouping() is deprecated in Drupal 11.3.0 and removed in 12.0.0. The initial preprocess is now registered via the 'initial preprocess' key in hook_theme(), pointing to ViewsThemeHooks::preprocessViewsViewGrouping() — which is the method to update.

Remaining tasks

API changes

hook_preprocess_views_view_grouping() will now be invoked for all grouping levels, including single-level grouping and leaf levels of multi-level grouping.

BC breaks

For single-level grouping, the HTML output changes: the group title moves from views_view_unformatted to views_view_grouping. Sites overriding views-view-grouping.html.twig or implementing hook_preprocess_views_view_grouping() may need to update accordingly.

Issue fork drupal-2950737

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

voleger created an issue. See original summary.

voleger’s picture

Also, I tried implement preprocess hooks for other views theme function, but they are worked.

voleger’s picture

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

voleger’s picture

Any thoughts?

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

macsim’s picture

Bumping this cause I am having the same problem with a block display on a drupal 11 instance. The hook is correctly invoked for a page display though.

macsim’s picture

Ok after digging into the code, the issue is not specific to the block display - hook_preprocess_views_view_grouping() is never invoked for single-level grouping, regardless of the display type (page, block, etc.).

The culprit is StylePluginBase::renderGroupingSets(). It contains a branch that decides which theme hook to use:

  $row = reset($set['rows']);

  // Branch A — only reached with 2+ grouping levels
  if (is_array($row) && isset($row['group'])) {
      // uses views_view_grouping ✓
      $single_output = ['#theme' => $theme_functions, ...];
  }
  // Branch B — always taken for single-level grouping
  else {
      // uses views_view_unformatted ✗
      $single_output = $this->renderRowGroup($set['rows']);
  }

With a single grouping level, renderGrouping() produces sets whose rows are ResultRow objects. reset() returns an object, so is_array($row) is FALSE → branch B is taken → renderRowGroup() is called → the style's own theme (e.g. views_view_unformatted) is used instead of views_view_grouping. The grouping title ends up as #title on the wrong render array, and hook_preprocess_views_view_grouping() is never triggered.

With 2 grouping levels, branch A is reached for level 0 (its rows are the level-1 sets, which are arrays with a 'group' key), so views_view_grouping is invoked once. However, the initial preprocess (preprocessViewsViewGrouping) immediately calls renderGroupingSets() on the level-1 sets, which fall through to branch B — so the hook never fires a second time for level 1. Level-1 titles are still accessible indirectly via $variables['content'][x]['#title'] within the level-0 hook call, but views_view_grouping is never properly invoked for the leaf level.

This matches my use case: my page display has 2 grouping levels while my block only has one

macsim’s picture

Issue summary: View changes
macsim’s picture

Issue summary: View changes

macsim’s picture

Issue summary: View changes
Status: Active » Needs review

Tested on an unformatted list view with both a single grouping level and 2 grouping levels - works perfectly
Please review the MR and if someone could also test it on a table-style view it would also be great.

macsim’s picture

Issue summary: View changes