You are here

Create a View of other members of a Group - Drupal 8

I previously wrote about how to create a View of content from a Group that a user is a member of. For another site, I have to show a list of other members in the same group on user profiles. Another common use case might be showing a logged-in user a list of other members in his/her group(s).

You can do this two ways:

  1. Create a View of Group Content of type [Group name]: Group membership
  2. or create a view of Users

Which one you choose depends on your use case. For example, I needed to show fields from the user profiles of the related users, but was unable to access them from a view of Group content—I couldn't get a relationship to them, so user profile fields would only show values from the user the view results were based on. (I could show fields from the group and user/group relationship for each user). So I ended up needing a view of users. You may need to experiment to see which fits your needs. I'll show how to do the first way, then add info about how to do the second.

(This assumes some knowledge of Drupal and Views; I'll only go over how to create the basic view; you will need to decide whether you need page, block, or other displays; what fields you need; etc).

First, create a new View of Group content, type will be the Group you want to show users from: group membership. (If you need to show results from more than one group, you can edit the view filter later to select them).

Next, add the following relationships to the view, in this order:

  • "Group: The group containing the entity" in the "Group content" category.
  • "Group content: Relate to the group content entities. From there you can relate to the actual grouped entities." in the "Group" category. Check the "Group membership" option under "Filter by plugin". This relationship should automatically have a relationship to the previously-added "Group" relationship
  • "User from group content: Relates to the User entity the group content represents." in the "User" category. Select "Group content" in the Relationship dropdown.

Now, we need to add a contextual filter to tell the site which user we're displaying related information for. We'll add the "User ID" filter in the "User" category. In the "Relationship" dropdown, select "Group content User" if it's not automatically selected. You'll then need to provide a default value for when the ID is not in the URL. If the display is, for example, a block on the user's profile to show anyone other members of that user's group, you'll select the "User ID from route context" default value. If you want to show a logged-in user other members of his group, you'll add the "User ID from logged in user" default value.

Finally, if you're starting from a view of users rather than group content:

  • Before adding the other relationships, add a "Group content for User: Relates to the group content entities that represent the User." relationship (in the Group content category).
  • When you add the User ID contextual filter, do not select any relationships.
  • If you need to show fields from the user profile, make sure the fields use the "Group content User" relationship. To show fields from the user/group relationship, use the "Group content" relationship. To show fields from the group (e.g. the group name), use the Group relationship.

How to exclude the user the view is based on

If you want to only show other members, and exclude the user who is being viewed (or who is viewing the content), here's how to do that with a view of Users (I didn't have to do it with a Group content view, but hopefully this will get you on the right path if you do):

  • Add another contextual filter to the view of type User ID
  • Give this contextual filter the Group content User relationship
  • Under "when the filter value is not available", select "Provide default value" > "User ID from route context"
  • Under "more", select the "Exclude" checkbox

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.