0

I am having a gallery view page taken from a content type which is displaying the results based on relationship and contextual filters. I am getting multiple fields of a node that seem to be duplicates, even though I have checked the reduce duplicate check box in contextual filter field. Anyone can help on this issues to get perfect value.

2

What version of Drupal/Views are you using? In Views v7.x-3.x you can use hook_views_pre_render() in your template.php file or in a module to filter duplicate results like this:

function YOURTHEMENAME_views_pre_render(&$view) {
  $used_nids = array();
  $new_view_result = array();

  if ($view->name == 'YOURVIEWNAME') {
    foreach ($view->result as $row) {
        if (!in_array($row->nid, $used_nids)) {
          $new_view_result[] = $row;
          $used_nids[] = $row->nid;
        }
    }

    $view->result = $new_view_result;
  }
}
| improve this answer | |
  • 1
    +1 Even though it's not 100% rational, in the sense that you fetch all the data and then filter them instead of modifying the query, it is the only approach that worked for me :) – Potney Switters Jul 17 '13 at 14:40
1

Edit your view and expand "Advanced" section. In "Other" subsection choose "Query settings" and check "Distinct" - that's all.

| improve this answer | |
  • Thanks for your reply eugene ..the problem is in contextual filter added two more fiters.. anyhow your info is usefull for further issues. – Dhinesh.ES Aug 20 '12 at 12:10
0

Only problem with hook_views_pre_render() is that you're loosing related field values. For example if node contains multiple value referenced field you're left with only one. I have modified solution given by hachesilva slightly so you can access all values in fields array:

function YOURTHEMENAME_views_pre_render(&$view) {

$used_nids = array();
$new_view_result = array();

if ($view->name == 'YOURVIEWNAME' && $view->current_display == 'YOURDISPLAYID') {
  foreach ($view->result as $row) {
    if (!in_array($row->nid, $used_nids)) {
      $new_view_result[$row->nid] = $row;
      $used_nids[] = $row->nid;
    } else {
      $new_view_result[$row->nid]->YOURFIELDNAME[] = $row->YOURFIELDNAME[0]; 
    }
  }
  $view->result = $new_view_result;
}

}

Use dpm($row); to check avalible fields name. In my case field_author is named field_field_author. Of course You don't have to check
$view->current_display == 'YOURDISPLAYID'

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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