How to Create a Drupal View to Display User Group Memberships
Overview
Organizing users into Groups is a use-case where Drupal is a preferred solution. Drupal Core supports Content and Users and thanks to the work of Drupal contributors we can extend the organization of content into Groups with a contributed module. During my recent development I was interested in showing a module to Users that listed all their Group memberships. I couldn't find a View shipped with the module. Here is how I created a Drupal View to display a list of Groups to which the User belongs.
There are a few ways to achieve this solution. First it will help to understand the relationship between Drupal Users and a Group. Both are Entities in Drupal and the contributed Group module is responsible for handling membership since it introduces the concept. Rather than creating a simple entity reference on the Group entity a GroupRelationship Entity is created to bind the relationship between User and Group. For each Group to which a User belongs there is a corresponding GroupRelationship. As an Entity it's fieldable and allows us to store information specific to the User—Group connection. This design allows us to query for User and/or Group information on the GroupRelationship Entity when we have a task like listing all the User's Groups.
I should also note that GroupRelationship entities are used to connect Groups to any entity, not just Users.
Fields: User View
In my opinion the simplest solution is to create a User view which displays a list of fields. Create a filter to select the User how you see fit for your use-case. In my case I create a contextual filter for the User ID so that each User can load this view and see only their Groups.
In order to get Group content fields into the View we need to add two relationships:
- The GroupRelationship entities. First add a Category: Group Relationship, Group relationship for User: Relates to the group relationship entities that represent the User. and Filter by Plugin Group membership plugin and Require the Relationship. I've named this "User Group Relationship." Apply.
- The Group entities. Add the second relationship Category: Group Relationship, Group: The group containing the entity. Set it to use the first relationship and Require it. I've named this second relationship "Group" in Administrative title for simplicity since data from this relationship pertains to the Group entity.
Check out the screenshot from my configured View. It will output all the Groups given the User ID in the contextual filter. In the Fields you can see I add the Group Title from the Group relationship in order to render the list.
Entities: Group View
If your goal is to display Group entities then you'll prefer to create a View which displays Group content by default. This will be similar to the User View approach except we'll configure the View to relate Groups to the GroupRelationship. Instead of relating the GroupRelationship to User we can simply filter by the User ID in the GroupRelationship!
- Create the view that displays Group content (View settings: Show Groups).
- Connect the GroupRelationship entity. Create a Relationship find Category: Group, Group Relationship: Relate to the group relationship entities. Filter by Plugin Group Membership and Require this relationship. I've named this Relationship "Group Membership."
Now you can add a contextual filter to your GroupRelationship. In my settings I wish to specify the User ID in the URL so I select Provide default value and Type: Content ID from URL. Now the numeric ID in your URL can be pulled in to filter on the User ID.
The final thing I do is ensure that the GroupRelationship only applies to the User membership content (remember that GroupRelationship entities can exist for other entities on your site). Note that in contextual filter options we didn't get to specify what content ID so we can theoretically pull in other GroupRelationship entities with the same numeric IDs. I've added this filter to prevent this from happening.
Conclusion
And that's it! Now you have examples of two Views which list a User's Groups by taking two different approaches. One from a View that renders User data and one from a View that renders Group data.
I hope this has been helpful. This isn't the only way I can imagine solving this problem. You could also try rendering things using GroupRelationship Views but these two examples demonstrate the patterns you will want to use.
The content here is an expanded example from my Comment on a Drupal issue.