How do I access the logged user's group role? Do you have an API to access it?

I'm looking for a similar function, where one can load a Drupal User and access the user information.

$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());

Comments

gopivignesh.m created an issue. See original summary.

gopivignesh.m’s picture

Issue summary: View changes
kristiaanvandeneynde’s picture

$group->getMember()->getRoles();

kristiaanvandeneynde’s picture

Status: Active » Fixed
JacobCF’s picture

I am pretty new to Drupal 8 and Groups as well. So, please excuse me for asking this question, but where do I use the API
$group->getMember()->getRoles();

For example, do I extend the GroupRole class and use it from there?

kristiaanvandeneynde’s picture

You need to load the Group you want (Group::load() or similar) and assign that to a $group variable. Then call getMember() using the current user service to retrieve the current user.

JacobCF’s picture

I am getting the following error

Recoverable fatal error: Argument 1 passed to Drupal\group\Entity\Group::getMember() must implement interface Drupal\Core\Session\AccountInterface, none given, called in

kristiaanvandeneynde’s picture

I'm sorry, I can tell you where to look but I can't write the code for you. From what both you and I have written above, it should be clear that you need to call getMember() on a Group instance and provide it the account you wish to fetch the membership for.

JacobCF’s picture

Thanks Kristiaan, I am still finding my way through the Drupal 8 system. Now, I have a much better idea on how to proceed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.