I want to get a list of all users that are members of a group.

I can get the members using getMembers():

$members = $group->getMembers();
  foreach ($members as $member) {    

But this returns not User objects but GroupMembership objects:

object(Drupal\group\GroupMembership)#2609 (1) {
  object(Drupal\group\Entity\GroupContent)#2667 (28) {
    array(12) {
      array(1) {
        string(1) "1"

So how do I get the User IDs from the GroupMembership objects? Or is there an easier way?

1 Answer 1


Found the answer myself minutes after posting the question... so it goes :).

  $members = $group->getMembers();
  foreach ($members as $member) {
    $user = $member->getUser();
    $userids[] = $user->id();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.