You are here

public function MembershipManager::getMemberships in Organic groups 8

Returns the group memberships a user is associated with.

Parameters

int $user_id: The ID of the user to get group memberships for.

array $states: (optional) Array with the states to return. Defaults to only returning active memberships. In order to retrieve all memberships regardless of state, pass `OgMembershipInterface::ALL_STATES`.

Return value

\Drupal\og\OgMembershipInterface[] An array of OgMembership entities, keyed by ID.

Overrides MembershipManagerInterface::getMemberships

3 calls to MembershipManager::getMemberships()
MembershipManager::getMembership in src/MembershipManager.php
Returns the group membership for a given user and group.
MembershipManager::getUserGroupIds in src/MembershipManager.php
Returns all group IDs associated with the given user.
MembershipManager::getUserGroupIdsByRoleIds in src/MembershipManager.php
Returns an array of groups ids filtered by the og roles of the user.

File

src/MembershipManager.php, line 94

Class

MembershipManager
Service for managing memberships and group content.

Namespace

Drupal\og

Code

public function getMemberships($user_id, array $states = [
  OgMembershipInterface::STATE_ACTIVE,
]) {
  if ($user_id instanceof AccountInterface) {
    trigger_error('Passing an account object is deprecated in og:8.1.0-alpha4 and is removed from og:8.1.0-beta1. Instead pass the user ID as an integer value. See https://github.com/Gizra/og/issues/542', E_USER_DEPRECATED);
    $user_id = $user_id
      ->id();
  }

  // When an empty array is passed, retrieve memberships with all possible
  // states.
  $states = $this
    ->prepareConditionArray($states, OgMembership::ALL_STATES);
  $cid = [
    __METHOD__,
    $user_id,
    implode('|', $states),
  ];
  $cid = implode(':', $cid);

  // Use cached result if it exists.
  if (!($membership_ids = $this->staticCache
    ->get($cid)->data ?? [])) {
    $query = $this->entityTypeManager
      ->getStorage('og_membership')
      ->getQuery()
      ->condition('uid', $user_id)
      ->condition('state', $states, 'IN');
    $membership_ids = $query
      ->execute();
    $this
      ->cacheMembershipIds($cid, $membership_ids);
  }
  return $this
    ->loadMemberships($membership_ids);
}