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.
You can do this with either a view of the nodes or entities themselves, or a view of Group content (the relationship between the Drupal entities and the Group they are in). The process is very similar; if you start with a view of Content or other entities, you will need to add one additional relationship at the beginning.
To test, 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 the view, you will add some relationships to let it know about what Group(s) each content belongs to and which users are in that Group, and then filter the view to the currently logged-in user.
Relationships to add, in this order (note that you only add the first relationship if your view is not of Group content):
- Start here for a view of nodes or other entities: "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, if you haven't already done so in the view filter.
- Start here for a view of Group content: "Group - The group containing the entity." This relationship is also under the Group Content category. When configuring it, if this is not a view of Group content, 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 (don't get it 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." 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. If this is not a view of Group content, 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.
Comments
Hello dear i am new to drupal looking for a solution found your comment related to my problem by some one else. so writing here will be highly greatful if you help me for giving solution to my problem
I have a custom field for user country every time when a user fill a signup form he must select the country. now user belongs to a role who having rights to create a content / node. and they working well.
now i have a requirement that i want to create a view which list all content which having same country of author with logged in user.
if manager is logged in who country is German then he must see all post of authors who's countries are also Germany.
kindly guide me please
is that possible or not. please guide me really looking for a solution for above