Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #2
gopivignesh.m CreditAttribution: gopivignesh.m commentedComment #3
kristiaanvandeneynde$group->getMember()->getRoles();
Comment #4
kristiaanvandeneyndeComment #5
JacobCF CreditAttribution: JacobCF commentedI 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?
Comment #6
kristiaanvandeneyndeYou 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.
Comment #7
JacobCF CreditAttribution: JacobCF commentedI 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
Comment #8
kristiaanvandeneyndeI'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.
Comment #9
JacobCF CreditAttribution: JacobCF commentedThanks Kristiaan, I am still finding my way through the Drupal 8 system. Now, I have a much better idea on how to proceed.