I have a view which shows the users subscribed to the website. Some users have multiple roles, therefore, on the results, there are some duplicates.

What I want to do is to remove these duplicates, so I've tried to enable the Aggregation in the view settings. This action reduced the repeated results, but it haven't erased them.
By looking at the query the GROUP BY is done for every field, but I want to enable for just one field, more specifically like this: GROUP BY users_name.

I've tried to achieve it by making a simple module with this function:

function alter_users_query_views_query_alter(&$view, &$query) {
    if ($view->name == 'all_users') {

Which adds the additional field in the GROUP BY, but it doesn't remove the others.
How can I achieve it? More specifically, how can I unset the other fields and group by just one?


I was able to find a solution by following this example. Here is the code in my case:

function alter_users_query_query_alter($query) {
    if (isset($query->alterMetaData)) {
        if (isset($query->alterMetaData['view'])) {
            if($query->alterMetaData['view']->name == 'all_users') {
                $fields =& $query->getGroupBy();

| 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.