You are here

public function MembershipManager::getUserGroups in Organic groups 8

Returns all groups associated with the given user.

This is similar to \Drupal\og\Og::getGroups() 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.

string[] $states: (optional) Array with the states to return. Defaults to active.

Return value

\Drupal\Core\Entity\ContentEntityInterface[][] An associative array, keyed by group entity type, each item an array of group entities.

Overrides MembershipManagerInterface::getUserGroups

See also

\Drupal\og\MembershipManager::getGroups()

\Drupal\og\MembershipManager::getMemberships()

File

src/MembershipManager.php, line 81

Class

MembershipManager
Service for managing memberships and group content.

Namespace

Drupal\og

Code

public function getUserGroups($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 = $this
    ->getUserGroupIds($user_id, $states);
  return $this
    ->loadGroups($group_ids);
}