Main menu

Drupal group by any field - Remove Druplicates

29 Mar
Published by Mark W. Breneman

I had a view that repeated a value several times even though I clicked the Distinct: Yes and was sill getting repeated values. In the SQL I found that the Distinct setting just adds group by nid. Since I was trying to get distinct values for another field, grouping by nid did nothing for me.

I found a few resources that pointed to views_pre_execute and found that to be a really really handy fix.

I added a function to my template.php. The below function makes sure that the function only effects the attachment view that I am targeting and does a find 'GROUP BY nid' and replaces it with 'GROUP BY field_sort_order_value'

function views_views_pre_execute(&$view){
if ($view->name == 'Opportunities' and $view->current_display == 'attachment_1'){
$search = array('GROUP BY nid');
$replace = array('GROUP BY field_sort_order_value');
$view->build_info['query'] = str_replace($search,
$replace,
$view->build_info['query']);
}
}

Category 

Drupal

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.