You are here

public function MembershipManager::getMembership in Organic groups 8

Returns the group membership for a given user and group.

Parameters

\Drupal\Core\Entity\EntityInterface $group: The group to get the membership for.

int $user_id: The ID of the user to get the membership 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|null The OgMembership entity. NULL will be returned if no membership is available that matches the passed in $states.

Overrides MembershipManagerInterface::getMembership

File

src/MembershipManager.php, line 129

Class

MembershipManager
Service for managing memberships and group content.

Namespace

Drupal\og

Code

public function getMembership(EntityInterface $group, $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();
  }
  foreach ($this
    ->getMemberships($user_id, $states) as $membership) {
    if ($membership
      ->getGroupEntityType() === $group
      ->getEntityTypeId() && $membership
      ->getGroupId() === $group
      ->id()) {
      return $membership;
    }
  }

  // No membership matches the request.
  return NULL;
}