I created a new view, based on Users. Than I made the relationship "User: Profile", so I will be able to add fields from the profiles. The output of the view shows all user, as expected, but they appear as many times as there are profiles attached to these users. Example: I've got two Profile2 profiles attached to users. Result: the users appear twice in the view. I've then added a third Profile2 profile, and than the users appear 3 times in the view. This only happens for the users where those profiles are populated. So a user with only 2 profiles populated appears only twice.
Setting "Distinct" in Query settings solves this problem.

All this happens before I even added fields from those profiles to the view. But when I add fields from the profiles, the "Distinct" setting doesn't work, and the users appear as many times as there are profiles. Also the extra setting "Pure Distinct" doesn't help.

I'm using the latest versions of all the modules in my Drupal installation. Am I doing something wrong, is this a bug, or... Any help?

Thanks,

Mike.

Comments

sportel’s picture

Issue summary: View changes

typo correction

fago’s picture

Status: Active » Closed (works as designed)

that's how views works.

sportel’s picture

Category: bug » support
Status: Closed (works as designed) » Active

Hi fago,

Thanks for your helpfull reply.

And, sorry for my stupid and unnecessary issue. But please allow me to ask a stupid question then: this might be how views works, but I can't imagine this is how people would like it to work. So, has anyone got a solution for this? How can I create a view from users with fields from multiple profiles, without the users showing up more than once?

Thanks,

Mike

sportel’s picture

Issue summary: View changes

another typo

crantok’s picture

Version: 7.x-1.2 » 7.x-1.3
Issue summary: View changes

Edit: Don't do the following. See #6 instead.

This worked for me...

function MY_MODULE_views_pre_render( &$view ) {
    
    if ( $view->name != MY_VIEW_NAME ) {
        return;
    }
    
    // Collect the rows of the view grouped by user ID.
    $grouped_by_uid = [];
    
    foreach ( $view->result as $row ) {
        if ( isset( $grouped_by_uid[$row->uid] ) ) {
            $grouped_by_uid[$row->uid][] = $row;
        }
        else {
            $grouped_by_uid[$row->uid] = [ $row ];
        }
    }

    $new_result = [];

    // For each set of views rows specific to one user...
    foreach ( $grouped_by_uid as $uid => $rows ) {

        // ...iterate through the object properties...
        foreach ( $rows[0] as $property_name => $property_value ) {
            
            // ...and where a views field property is empty for the first row...
            if ( empty( $property_value )  &&
                0 === strpos( $property_name, 'field_field_' ) ) {

                // ... try overwriting it with a non-empty property from later rows.
                foreach ( $rows as $row ) {
                    $rows[0]->$property_name = $row->$property_name;
                    if ( !empty($rows[0]->$property_name) ) {
                        break;
                    }
                }
            }
        }
        $new_result[] = $rows[0];
    }
    
    $view->result = $new_result;
}
joachim’s picture

Status: Active » Fixed

Hacking your view at the render stage does not seem like a good idea to me!

You need to restrict the relationship to a particular profile type.

crantok’s picture

Well, yeah, it's probably a bad idea, but I don't see a much better method for aggregating data from 2 profile types.

I can't merge the profiles in to one because they are each editable by a different set of people. To make life easy for the admins, fields from both profiles are shown on the same view. I could make them use two views or, I suppose, write my own custom page. (What I'd really like to do is to write my own SQL query and then throw the result at views for presentation.)

joachim’s picture

Add the same relationship twice, restricting it to a different profile type each time. It's really simply once you know how, and no awful hacking is required!

crantok’s picture

Ah! I see. Thank you Joachim. That worked for me. Is that knowledge recorded anywhere other than in the issue queue? If not, is there an obvious place that I should put it?

joachim’s picture

I think it's in the documentation pages for the module. If it's not, feel free to add it!

joachim’s picture

It's on this page: https://www.drupal.org/node/2027157 but it could maybe do to be clearer. If you have time to work on expanding that text, maybe adding step-by-step instructions, please do :)

crantok’s picture

I reworded that. I hope it's a little clearer. It is more likely to catch the keywords I was searching for.

joachim’s picture

That's much better. Thanks!

crantok’s picture

Glad I could help :)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.