Previously, I posted about how to create a view on a user's profile of content authored by that user, limited to content in Group(s) the viewing user is a member of. That tutorial shows how to accomplish that with a view of content (Drupal nodes) which are in Groups.
The person who requested that tutorial later told me they needed a view of Group content. Group content is the relationship between Drupal nodes (or other entities, such as users, media, etc) and their containing Group. It is a new entity created when an entity is added to a Group.
They wanted to be able to show all types of entities in Groups, and when you start from a base table of content, you can't show other entity types such as media or users.
So here's how to create a view of Group content, authored by the user whose profile is being viewed, limited to content in Groups the viewing user is a member of. Note that for each Group content type you want them to see, they must be given the "Relation: View any entity relations" permission for that content type in the Group type permissions table.
- Add a new View of Group content. Limit it by the types you want to show, if desired
- Create a block display (this will be placed on user profiles)
- Add fields, pagers, and any other View configuration you want
- To limit the view to content authored by user being viewed:
- Add the relationship "User" in the Group content category ("The username of the group content creator.")
- Add a User ID contextual filter. It should automatically have the "User" relationship you just added. Under "When the filter value is NOT available" select "Provide default value > User ID from route context". This will filter the view to content created by the user whose ID is in the URL of the profile page being viewed.
- Test the contextual filter by entering a user ID in the preview (make sure it's a user who has authored some group content)
- To limit the view to content in Group(s) the viewing user is a member of:
- Add a "Group" relationship, in the Group content category ("The group containing the entity.")
- Add a "Group content" relationship, in the Group category ("Relate to the group content entities. From there you can relate to the actual grouped entities."). This relationship will automatically have a relationship to the Group relationship just added. Under "filter by plugin" select "Group membership".
- Add the "User from group content" relationship, in the User category ("Relates to the User entity the group content represents."). When configuring this relationship, in the "Relationship" dropdown, select "Group content".
- Next, add another User ID contextual filter. In the "Relationship" dropdown, select "Group content User". Under "When the filter value is NOT available", select "Provide default value > User ID from logged in user".
Et voila. Make sure to test in the View preview by entering the ID of users who have authored content in groups and ID of users in those groups, in the format [ID]/[ID]. The first ID will be the author, the second the viewing user (if you added the contextual filters in the order I've outlined). Place the block on user profiles and test some more.
It may seem complicated, but it's actually quite logical. We are starting by showing Group content. We then add a relationship to the User who authored that content, so we can limit the view on their profile to their content with a contextual filter based on the user ID.
Next, we add a relationship to the Group(s) the content is in. We then add a relationship to the Group content in that group, limited to Group membership, which is users who have been added to the Group. From that relationship, we add another relationship to the actual user entity represented by the Group content, and add a contextual filter with the value of the currently logged-in user viewing the profile. This results in that user only seeing content if they are in the same Group(s) the content is in.
I hope that makes sense! Feel free to leave a comment or contact me with any questions.