Skip to content

Instantly share code, notes, and snippets.

@robdecker
Created November 18, 2019 20:51
Show Gist options
  • Save robdecker/b87c9d83b9c2376ad9508866a16a3a8d to your computer and use it in GitHub Desktop.
Save robdecker/b87c9d83b9c2376ad9508866a16a3a8d to your computer and use it in GitHub Desktop.
[Duplicate rows in views] #d8

try using https://www.drupal.org/project/views_merge_rows or a hook:

function MODULE_views_pre_render(ViewExecutable $view) {
  if ($view->id() == 'VIEW_ID') {
    $nids = [];
    foreach ($view->result as $k => $result) {
      $nid = $result->nid;
      if (in_array($nid, $nids)) {
        unset($view->result[$k]);
      }
      else {
        $nids[] = $nid;
      }
    }
  }
}

but this ^ will affect the pagination. so if using a pager, update its data:

$view->result = $new_results; // Where $new_results is an array of new rows
$view->total_rows = count($new_results);
$view->pager->total_items = count($new_results);
$view->pager->updatePageInfo(); // After updating page variables then it's important to run this method!

from https://drupal.stackexchange.com/a/264298

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment