0

i have a view which is not filtering the duplicated nodes and i tried couple of solutions but it looks like they are not working.

The code I am using is the following.

function mymodule_views_pre_render(ViewExecutable $view) {
  if ($view->id() == 'basicnews') {
    $unique_nids = $new_results = array();

    // Loop through results and filter out duplicate results.
    foreach ($view->result as $index => $result) {
      if (!in_array($result->nid, $unique_nids)) {
        $unique_nids[] = $result->nid; 
        $new_results[] = $result;
      }

      // Replace $view->result with new array. Apparently views requires sequentially keyed
      // array of results instead of skipping keys (e.g. 0, 2, 4, etc), so we can't just
      // unset the duplicates.
      $view->result = $new_results;
    }
  }
}

Is there any patch or work-around for this issue?

1 Answer 1

0

You can handle most duplicate node issues using Views Aggregation options.

Go to advanced in the view and select "Use Aggregation"

Then in the fields area, set the aggregation settings. Generally, you just want the group together option with Entity ID as the group column. Sometimes you have to play around with other settings as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.