Hi,

I created a view listing all the groups and would like to filter/show only groups that the logged in user belongs to - either as group creator, or group user. Is this doable?

Thank you.

Seong

Comments

seongbae created an issue. See original summary.

kristiaanvandeneynde’s picture

Yes, for now you'll need to start from a Group content view with a relationship to Group. It's not possible yet to go the other way around (but it will be in the near future). Then you can filter to only list memberships and add a relation to the user. Then filter on the user being the current user.

Should be easier to pull off when we have a full release.

kristiaanvandeneynde’s picture

Status: Active » Fixed
JulienD’s picture

Status: Fixed » Active

I would like to achieve the same purpose but by code. @kristiaanvandeneynde, can you tell me how to load all the group a user has access to?

I got lost when playing with access...

Thanks

Reopening the issue

sumanthkumarc’s picture

@JulienD You can use membership loader service. See the below code.

$user is user object for whom you wish to load the data for.

$groups = array();
$grp_membership_service = \Drupal::service('group.membership_loader');
$grps = $grp_membership_service->loadByUser($user);
foreach ($grps as $grp) {
        $groups[] = $grp->getGroup();
}
maxilein’s picture

Closing, since it seems to be a duplicate of

maxilein’s picture

Status: Active » Closed (duplicate)
jedgar1mx’s picture

Thanks @sumanthkumarc, that's exactly what I was looking for.

debasisnaskar’s picture

Component: Group (group) » Code

Hello @sumanthkumarc

Please help me to print the label of the group! I want to just print the label of the Group Name(just similar to Node title).
I have used your code, it gave me the correct output which serves the other purpose, but could not able the print the Group Name(Label).

naveenvalecha’s picture

Title: How do I list all groups a user belongs to? » How can I create a view listing all groups that a user belong to?
Version: 8.x-1.0-beta4 » 3.2.x-dev
Status: Closed (duplicate) » Active

Reopening this issue for 3.2.x
The solution mentioned in #2829632: How can I create a view listing all groups that a user belong to? for 1.x doesn't work for 3.2.x

naveenvalecha’s picture

Title: How can I create a view listing all groups that a user belong to? » [3.x] How can I create a view listing all groups that a user belong to?
kristiaanvandeneynde’s picture

Status: Active » Closed (duplicate)

Please don't reopen issues that have been closed for years. File a new one.

naveenvalecha’s picture

Title: [3.x] How can I create a view listing all groups that a user belong to? » How do I list all groups a user belongs to?
Version: 3.2.x-dev » 8.x-1.0-beta4

Please don't reopen issues that have been closed for years. File a new one.

Apologies for opening the old one. I have also reverted the version and title to match as it was before.
I have opened a new one #3397547: [3.x] How can I create a view listing all groups that a user belong to?
Looking forward to hear from you there.