Filter/sort on a multi-value field creates duplicate rows

Created on 9 May 2016, over 8 years ago
Updated 9 September 2024, 2 months ago

Problem/Motivation

Create a view with a multi-valued field that is display multiple values in the same row, then add a filter or sort on that multi-valued field, or a field on an entity that is related via that multi-valued field, then duplicate rows will show in the results.

Steps to reproduce

  1. Create a content type that has a multi-valued field. Example: A taxonomy vocabulary, where each term has a multi-valued User field, such as "Working Group" with the field "Group Members".
  2. Create a few entities of the content type, some having multiple values for that field.
  3. Create a view for that content type. (Working Groups).
  4. Add the multi-valued field to the view. (Group Members)
  5. Add the a relationship based on that multi-valued field. (field_group_members: User)
  6. Add a sort to the view for a field on the related entity, such as "User: Last Name (ASC)"
  7. Instead of seeing one row for each Working Group, you should see one row per group for each person in the group. Each row shows all the group members.

Proposed resolution

Don't show duplicate rows when the multi-valued field is contained in a single row, while still allowing filtering and sorting on those rows.

Workaround

This workaround removed the duplicate rows, but the number of rows displayed on a page might be less than the configured setting, as each deleted row still took up a "slot" on the page when the query was run.

/**
 * Implements hook_views_pre_render().
 */
function my_module_views_pre_render(ViewExecutable $view): void {
  switch ($view->id()) {
    case 'my_view_id':
      // Ensure there's only one row per group.
      // See https://www.drupal.org/project/drupal/issues/2721691
      $combined = [];
      foreach ($view->result as $row) {
        if (!isset($combined[$row->_entity->id()])) {
          $combined[$row->_entity->id()] = $row;
        }
      }

      // Replace the result set with the combined result.
      $view->result = $combined;
      $view->total_rows = count($combined);

      break;
  }
}

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Original issue

I have a multivalued date field on a content type, and a view showing all published items of that content type. Adding an exposed sort by field_date to the view creates duplicate rows for content items which have multiple values in the date field.

Desired behavior: Adding a sort on a multivalue field on a view should expose additional options for how to sort that field, or should default to the first field item.

๐Ÿ› Bug report
Status

Active

Version

11.0 ๐Ÿ”ฅ

Component
Viewsย  โ†’

Last updated about 4 hours ago

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States t_stallmann

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024