-1

Can anyone please help me how to add/alter/remove aggregate options like count/sum/average etc. along with groupings using hook_views_query_alter in Drupal 8. I cant seem to find a suitable example.

Thanks BR/Pratip

1

I am not sure why people down-voted my question. It might have been a little silly to ask if there's a way to alter the group by of a views query using hook_views_query_alter because I think aggregation is actually a part of the base query and not views. So instead of using hook_views_query_alter we need to use hook_query_alter.

Ex.

/**
 * Implements hook_query_alter().
 */
function my_module_query_alter(Drupal\Core\Database\Query\AlterableInterface $query) {
  if ($query->getMetaData('view') && $query->getMetaData('view')->id() === 'my_view') {
    $group_by =& $query->getGroupBy();
    unset($group_by['users_field_data.uid']);
  }
}

There are a lot of other memeber function that you can check out like $query->groupBy('field_name) that can add another group by clause and so on...

Still not sure why it was down-voted, but I'm sure this might help someone some someday like me.

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.