code to grab a field value from a D8 group module member field

Submitted bybarnettech onFri, 01/29/2021 - 10:23

code to grab a field value from a group module member field


  $group =  \Drupal\group\Entity\Group::load(31);
  $group_memberships = \Drupal::service('group.membership_loader')->loadByGroup($group);
  // $results = $query->execute();
  foreach ($group_memberships as $member) {
    //$group_id[] = $member->groupContent->values["field_group_id"]["x-default"][0]["value"];
    $group_id[] = $member->getGroupContent()->get("field_group_id")->value;
  }
  $options = array_filter($group_id);

This worked to grab the uid from the member

$member_uid = $member->getGroupContent()->get('uid')->getValue()[0]['target_id'];

and

$member_id = $member->getGroupContent()->get('id')->getValue()[0]['value'];