drupal 8 views 去重

admin 提交于 周一, 06/17/2019 - 06:42
<?php
/*
 * Implements hook_preprocess_HOOK().
 */
function hook_preprocess_views_view_unformatted__VIEW_ID(&$variables) {
  $entity_ids = [];
  foreach ($variables['rows'] as $key => $row) {
    $entity = $row['content']['#row']->_entity;
    $id = $entity->id();
    if (!in_array($id, $entity_ids)) {
      $entity_ids[] = $id;
    }
    else {
      unset($variables['rows'][$key]);
    }
  }
}