This project is not covered by Drupal’s security advisory policy.
Views Pivot Table adds a Pivot table (crosstab) format to Views. Pick one field for the rows, one for the columns and one for the values: each cell shows the values found for that row and that column, optionally summed, with optional totals.
No JavaScript library, no extra database query, no custom code: the table is built from the results your view already returns, using the rendered output of its fields.
Example
A view listing volunteer shifts (volunteer, day, hours), with "Volunteer" as row field, "Day" as column field, "Hours" as value field, and sums and totals turned on:
| Friday | Saturday | Sunday | Total
-----------+--------+----------+--------+------
Alice | 4 | 6 | - | 10
Bob | 2 | 3 | 5 | 10
Total | 6 | 9 | 5 | 20
Features
- Any field of the view can be used for rows, columns or values, including fields from relationships, with their formatter and "Rewrite results" settings.
- Rows and columns are grouped on their plain text, so the same label always lands in the same row or column, even when it is rendered with different markup.
- Cells with several values list them all, with a configurable separator (HTML allowed). Empty cells show a configurable text.
- Optionally remove duplicate values in each cell, or replace the values of a cell with their sum.
- Optional total row, total column and grand total, with a configurable label. Totals are always computed from all the underlying values, even when duplicates are hidden.
- Configurable number format (decimals, decimal separator, thousands separator), used both to read numeric values and to display sums, so
1 234,50works as well as1,234.50. - Rows and columns can be sorted independently (ascending or descending, natural sort), or keep the order of the view results.
- Views grouping fields are supported: one pivot table is displayed per group, including nested groups.
- Accessible markup: row and column headers are
<th>elements with ascope. - Themeable: a Twig template that can be overridden per view and display, and CSS classes on the totals.
- Config schema, translatable labels, and automated tests.
Post-installation
- Create or edit a view that displays fields.
- In the "Format" section, set the format to Pivot table (crosstab).
- Choose the row field, the column field and the value field.
- Optionally set the separator, the empty cell text, sorting, duplicate removal, sums, totals and number format.
- Save the view.
Fields that are not used for rows, columns, values or grouping are not displayed, so you can keep helper fields in the view.
Good to know
- The table is built from the results of the current page. If the view has a pager, rows and totals only cover the items of that page: set the pager to "Display all items" to get the complete table.
- Results without a row value or a column value are skipped.
- Sums and totals ignore non-numeric values. If the value field displays a prefix or suffix (for example a currency sign), hide it in the field formatter settings so the values can be read as numbers.
Theming
The table is rendered with the views-view-pivot-table.html.twig template, which can be overridden like any other Views template, for example views-view-pivot-table--VIEW-ID.html.twig. The total row and column have the pivot-table-total-row, pivot-table-total-column and pivot-table-total-cell classes.
Additional requirements
Drupal 10.3 or later, or Drupal 11, and the core Views module. No other dependency.
Similar projects
- Views Aggregator Plus groups table rows on one column and applies summary functions to the other columns. Rows stay rows: it does not turn the values of a field into columns, which is what a pivot table does. Both modules can be used on the same site.
- Pivot Tables for Views and Views Crosstab provided a similar feature for Drupal 7 only.
- Core Views aggregation ("Use aggregation") computes sums and counts in SQL, but displays them as a flat list or table, not as a crosstab.
Project information
- Project categories: Administration tools, Content display
1 site reports using this module
- Created by johaziel on , updated
This project is not covered by the security advisory policy.
Use at your own risk! It may have publicly disclosed vulnerabilities.
