I'm using Views 7.x-3.6 and I tried altering the GROUP BY
clause with hook_views_query_alter()
as below:
function mymodule_views_query_alter(&$view, &$query) {
if ($view->name == "view_name"){
$query->add_groupby('field_name');
dpm($query);
}
}
When I look in $query
, the groupby
clause is correctly enabled but the SQL query is not affected: the GROUP BY
clause doesn't appear:
What I eventually did is using a Drupal core hook (hook_query_alter()
) and it worked fine: the SQL is now affected.
function mymodule_query_alter(QueryAlterableInterface $query) {
$view_name = 'view_name';
if ($query->hasTag('views_' . $view_name)) {
$query->groupBy('field_name');
}
}
Any reason why my hook_views_query_alter()
doesn't work? I'm wondering if there's a cleaner way to do it and.