2

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) {    
    var_dump($member);
  }

But this returns not User objects but GroupMembership objects:

object(Drupal\group\GroupMembership)#2609 (1) {
  ["groupContent":protected]=>
  object(Drupal\group\Entity\GroupContent)#2667 (28) {
    ["values":protected]=>
    array(12) {
      ["id"]=>
      array(1) {
        ["x-default"]=>
        string(1) "1"
      }
      ["type"]=>

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

1 Answer 1

4

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.