Filter a view by the Group ID of the current logged in user

I have been looking for an answer for 3 days with no success.

Looks like this is a common requirement among plenty of Drupal 8 projects with not a single answer accross the internet about how is possible to achieve it.

I, as user id 1, created 2 groups titled: “Basketball” and “Football” of the group type (sports) and then I added some users as members to those two groups…

So I have something similar to:

Group Title: Basketball

Group Members:

  • User A (whatever role)

  • User B (whatever role)

  • User C (whatever role)

  • User D (whatever role)

Group Title: Football

Group Members:

  • User 1 (whatever role)

  • User 2 (whatever role)

  • User 3 (whatever role)

  • User 4 (whatever role)

Now, what I really want to accomplish using views is to show a table with the names of all available members registered at the same group of the current logged user.

So if for example if User D is the current logged in user, I want to show him a table displaying:

  • User A

  • User B

  • User C

  • User D

without showing up the other members from other groups like user 1, user 2…

and for example if User 2 is the current logged in user, I want to show him a table displaying:

  • User 1

  • User 2

  • User 3

  • User 4

without showing up the other members from other groups like user A, user B…

Is that possible, and how?

I have followed this issue: https://www.drupal.org/node/2793631#comment-11613765 but with no success!

And this question How do I show the members in a block, contextually filtered by the currently logged in user? with no direct solution by using only the views UI.

My aim is to create an entity reference views to be called from a entity reference field element inside a webform.

UPDATE: This issue (https://www.drupal.org/project/group/issues/2793631#comment-12808833) has a solution On how to a list of all groups in which the current logged in user is a member.

However, for my requirement I need to show a list of all members that belong to the same group of the logged in user… so I did:

  • Create a page view and set the content of the view to be Group content with a url: /my-friends

  • Add a relationship to Group (The group containing the entity.)

  • Add a relationship to Group content for User (Relates to the group content entities that represent the User.)

  • Add a contextual filter to Group ID and when the filter value is not available set the default value to User ID from logged in user and the relationship is set to the Group content for User.

For the preview of the view, everything looks good and I, the admin, can see my name inside the display table along with all the other member with me inside the group.

However, if i log in as any other member inside this group such as: User 2 and go to the view page and receive a table with the message: no members available.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Filter a view by the Group ID of the current logged in user

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.