views_string_aggregation
Views String Aggregation is a new module that extends Drupal Views with database-level string aggregation and concatenation capabilities.
If you would like to combine values into concatenated string results, using more than the default core numeric aggregation options, then Views String Aggregation provides an SQL-native solution.
Key features
SQL optimized
String aggregation and concatenation happens at the SQL level using native database functions, such as GROUP_CONCAT and STRING_AGG.
Flexible configuration
- Choose a separator (e.g comma, pipe, space etc..)
- Order concatenated values by selected field
- Sort concatenated values ASC or DESC
- Show distinct values only
- Set
group_concat_max_len(MySQL/MariaDB) - Automatic Views integration
Supports multiple database types
- MySQL/MariaDB: Uses
GROUP_CONCATwith max length, custom separator, ordering and distinct support - PostgreSQL: Uses
STRING_AGGwith custom separator, ordering and distinct support - SQLite: Uses
GROUP_CONCATwith custom separator, ordering and distinct support - Database type is detected and handled automatically
Easy to use
No complex configuration required. Simply enable aggregation in your View and set a field to use "String aggregation" type.
Views String Aggregation field settings
Fig 1: New field aggregation settings are provided for string aggregation.
Example
Uses
- Content aggregation: Display all values for a field grouped by other field value
- Commerce: Display all order item titles grouped by order
- REST API endpoints: Create Views REST API response data including aggregated and concatenated field data
- Data exports: Create Views CSV exports including aggregated and concatenated field data
- etc…
Results
Before: Multiple rows per content item
Title Value Getting Started with Drupal drupal Getting Started with Drupal tutorial Getting Started with Drupal beginnerAfter: Aggregated and concatenated results as a comma separated string
Title Values Getting Started with Drupal drupal, tutorial, beginnerInstallation & Usage
- Install the module using Composer (recommended) or by downloading manually
- Enable the module: e.g:
drush en views_string_aggregation - In your View, enable Use aggregation in Advanced settings
- Set desired fields to String aggregation or String aggregation DISTINCT aggregation type
- Optionally configure grouping, e.g customize separator, max string length or order by field, under Advanced > Other > Query Settings
That's it! Your View will now display nicely aggregated string data.
Usage note:
Aggregation happens at the SQL level. Be aware of how the fields you configure to use “Group by” type aggregation affect the result. For example grouping by a node id field will show records grouped by node id on separate rows. You could however use string aggregation to show a set of node ids grouped by type, for example.
Similar modules
Differences
- Views Aggregator Plus provides a custom Views table display with a variety of aggregation features, using PHP at the display level.
- Views String Aggregation is different because it works for any aggregation-enabled Views display and provides string aggregation using native SQL functions at the query level.
Acknowledgements
This module acknowledges with thanks the initial work contributed in https://www.drupal.org/project/drupal/issues/2902481 namely by
- f2boot
- phjou
- navkaur
- kapilv
- jackson.cooper
- attisan
- simeonkesmev
- achton
- kasperg
- Sir-Arturio
- b.ravanbakhsh
- vensires
Requirements
- Drupal 9.x, 10.x or 11.x
- Views module (included in Drupal core)
- MySQL 5.7+, MariaDB 10.2+, SQLite 3+ or PostgreSQL 9.1+
Note: A legacy Drupal 9 version is also available.