The Group module is a powerful one I turn to often when a site has a requirement for groups of users who should have specific permissions, access to edit or view specific content, etc.
A common requirement is to show a list of content belonging to the Group(s) a user is member of, using Views. That isn't as straightforward as it seems. I recently had to do this and struggled a bit, so thought this solution might be useful for someone else.
Note that this assumes you're starting from a View of content. You can create views of Group Content, in which case the process might be different. For my use case, I needed to start from the node (content) base table, so that's what this tutorial assumes. Also note that in my setup, nodes and users can only belong to one group; if your case is different, I believe but can't guarantee it will work the same way.
To test this, you need some content of the type you are displaying in the view, at least two groups, content added to the groups, and at least one user in each group.
Once you've created your content view, you will add some relationships to the view to let it know about what Group(s) each piece of content belongs to and which users belong to that Group, and then filter the view to the currently logged-in user.
Relationships to add, in this order:
- "Group content for Content - Relates to the group content entities that represent the Content." (You can select the "Group Content" category to find this relationship). You can select your content type(s) under "filter by plugin" if you want to limit what content should be available (although if your view is already filtering by content type, I think that will accomplish the same thing).
- "Group - The group containing the entity." This relationship is also under the Group Content category, but it was not available before adding the "Group content for Content" relationship. When you select it then click "Add and configure relationships" you will notice that it has a relationship to "Content group content" - the first relationship we added.
- "Group content - Relate to the group content entities. From there you can relate to the actual grouped entities." This relationship is in the "Group" category, which was not available before adding the relationship in step 2. (Don't get this confused with the "Group content for Content" relationship in the Group Content category.) It automatically gets a relationship to the "Group" relationship from the previous step. This time, under "filter by plugin", select "Group membership".
- "User from group content - Relates to the User entity the group content represents." This one is in the User category. (Don't get it confused with "User" in the Group or Group Content categories). Important: The second and third relationships were automatically given relationships to previous relationships, and there was only one available to them. For this relationship, you have to choose the correct relationship, which is "Group content", NOT "Content group content" (Group content should be the second in the "Relationships" dropdown.) Next, select "Group membership" under "filter by plugin" (it should be the only option).
Next, we have to add a contextual filter to filter the results to the currently-logged in user.
Add a "User ID" contextual filter (under the "User" category). It should have the "Group content User" relationship by default. Under "When the filter value is NOT in the URL" select "Provide default value", and for Type, select "User ID from logged in user".
Now you can test your view by entering the user IDs of users on your site who belong to groups in the "Preview with contextual filters" field in the View preview pane (again, make sure there is content in the groups). You should see only content from the group(s) that user belongs to. If not, check your relationships and contextual filter configuration or other filters or access settings you may have on the view.