You are here

public function GroupRoleStorage::loadByUserAndGroup in Group 8

Same name and namespace in other branches
  1. 2.0.x src/Entity/Storage/GroupRoleStorage.php \Drupal\group\Entity\Storage\GroupRoleStorage::loadByUserAndGroup()

Retrieves all GroupRole entities for a user within a group.

Parameters

\Drupal\Core\Session\AccountInterface $account: The account to load the group role entities for.

\Drupal\group\Entity\GroupInterface $group: The group entity to find the user's role entities in.

boolean $include_implied: (optional) Whether to include the implied roles 'anonymous', 'outsider' and 'member'. Defaults to TRUE.

Return value

\Drupal\group\Entity\GroupRoleInterface[] The group roles matching the criteria.

Overrides GroupRoleStorageInterface::loadByUserAndGroup

File

src/Entity/Storage/GroupRoleStorage.php, line 101

Class

GroupRoleStorage
Defines the storage handler class for group role entities.

Namespace

Drupal\group\Entity\Storage

Code

public function loadByUserAndGroup(AccountInterface $account, GroupInterface $group, $include_implied = TRUE) {
  $uid = $account
    ->id();
  $gid = $group
    ->id();
  $key = $include_implied ? 'include' : 'exclude';
  if (!isset($this->userGroupRoleIds[$uid][$gid][$key])) {
    $ids = [];

    // Get the IDs from the 'group_roles' field, without loading the roles.
    if ($membership = $this->groupMembershipLoader
      ->load($group, $account)) {
      foreach ($membership
        ->getGroupContent()->group_roles as $group_role_ref) {
        $ids[] = $group_role_ref->target_id;
      }
    }

    // Add the implied group role IDs.
    if ($include_implied) {
      $group_type = $group
        ->getGroupType();
      if ($membership !== FALSE) {
        $ids[] = $group_type
          ->getMemberRoleId();
      }
      elseif ($account
        ->isAnonymous()) {
        $ids[] = $group_type
          ->getAnonymousRoleId();
      }
      else {
        $ids[] = $group_type
          ->getOutsiderRoleId();
        foreach ($account
          ->getRoles(TRUE) as $role_id) {
          $ids[] = $this->groupRoleSynchronizer
            ->getGroupRoleId($group_type
            ->id(), $role_id);
        }
      }
    }
    $this->userGroupRoleIds[$uid][$gid][$key] = $ids;
  }
  return $this
    ->loadMultiple($this->userGroupRoleIds[$uid][$gid][$key]);
}