You are here

public function MembershipManager::getUserGroupIds in Organic groups 8

Returns all group IDs associated with the given user.

This is similar to \Drupal\og\MembershipManager::getGroupIds() but for users. The reason there is a separate method for user entities is because the storage is handled differently. For group content the relation to the group is stored on a field attached to the content entity, while user memberships are tracked in OgMembership entities.

Parameters

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

array $states: (optional) Array with the state to return. Defaults to active.

Return value

array An associative array, keyed by group entity type, each item an array of group entity IDs.

Overrides MembershipManagerInterface::getUserGroupIds

See also

\Drupal\og\MembershipManager::getGroupIds()

2 calls to MembershipManager::getUserGroupIds()
MembershipManager::getUserGroups in src/MembershipManager.php
Returns all groups associated with the given user.
MembershipManager::isMember in src/MembershipManager.php
Returns whether a user belongs to a group.

File

src/MembershipManager.php, line 62

Class

MembershipManager
Service for managing memberships and group content.

Namespace

Drupal\og

Code

public function getUserGroupIds($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();
  }
  $group_ids = [];

  /** @var \Drupal\og\Entity\OgMembership[] $memberships */
  $memberships = $this
    ->getMemberships($user_id, $states);
  foreach ($memberships as $membership) {
    $group_ids[$membership
      ->getGroupEntityType()][] = $membership
      ->getGroupId();
  }
  return $group_ids;
}