Views grouping
A Drupal Views format setting that files rows under a heading for each value of one field, for example products listed under their brand.
Views grouping is a setting in the format of a view that collects rows into sets and puts a title above each set. You pick one field, and every distinct value of that field becomes a heading with its rows below it. A list of articles can, for example, appear under the heading of each category.
How views grouping works in Drupal 11
Grouping belongs to the style plugin, the part that decides how rows are laid out. In the Views editor, click Settings next to the format name in the Format section. There the field select is titled "Grouping field Nr.1", with the help text that you may optionally specify a field to group the records by and leave it blank to not group.
The base class StylePluginBase sets usesGrouping to true, and the grouping options show only when the view uses fields. A style can turn grouping off: the Entity Reference list style does.
Two checkboxes refine the result:
- Use rendered output to group rows. Groups by what the field displays, not its raw value.
- Remove tags from rendered output. Strips HTML from the group title.
The method renderGrouping() places each row into a set by its grouping value. Rows with the same value end up in one set even when other rows sit between them in the results. With several grouping fields, the result is a nested structure, and each outer level is output with the theme hook views_view_grouping. The innermost sets use the template of the format itself. Each set gets the field output as its title. If the grouping field shows a label, the title starts with that label.
Example
You build a view of articles with the fields Title and Category, where Category is a taxonomy term reference with its label turned off. In the Format settings you choose Category as grouping field Nr.1. The page now shows each category name once, with the matching article titles below it.
Common mistakes
- Mixing up three features. Grouping arranges rows. Use aggregation calculates over rows. Grouped filters are a type of exposed filter.
- Grouping on a field with a label. The label becomes part of every group title, so uncheck the label on the field when the heading should show only the value.
- Grouping by a field with free text. Each different rendered value makes its own group, so a value spelled two ways makes two headings. Group by a field with controlled values, such as a term reference.
- Looking for the option in a content-based format. If the view shows whole items instead of fields, switch the row to fields first.
For marketing and content teams
Grouping decides the headings your readers see on listing pages, such as case studies by industry. The headings come from field values, so a term name you choose in the editor becomes a heading on the page. Keep those values consistent, and tell your developer which field should drive the headings.