You are here

public function MembershipManager::getUserGroupIdsByRoleIds in Organic groups 8

Returns an array of groups ids filtered by the og roles of the user.

Parameters

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

string[] $role_ids: A list of OG role IDs to filter by.

string[] $states: (optional) An array of states to filter the memberships by.

bool $require_all_roles: (optional) If set to TRUE, all requested roles must be present to return the group. Set to FALSE to return the groups that match one or more of the requested roles. Defaults to TRUE.

Return value

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

Overrides MembershipManagerInterface::getUserGroupIdsByRoleIds

1 call to MembershipManager::getUserGroupIdsByRoleIds()
MembershipManager::getUserGroupsByRoleIds in src/MembershipManager.php
Returns an array of groups filtered by the OG roles of the user.

File

src/MembershipManager.php, line 148

Class

MembershipManager
Service for managing memberships and group content.

Namespace

Drupal\og

Code

public function getUserGroupIdsByRoleIds($user_id, array $role_ids, array $states = [
  OgMembershipInterface::STATE_ACTIVE,
], bool $require_all_roles = TRUE) : array {
  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();
  }

  /** @var \Drupal\og\OgMembershipInterface[] $memberships */
  $memberships = $this
    ->getMemberships($user_id, $states);
  $memberships = array_filter($memberships, function (OgMembershipInterface $membership) use ($role_ids, $require_all_roles) : bool {
    $membership_roles_ids = $membership
      ->getRolesIds();
    return $require_all_roles ? empty(array_diff($role_ids, $membership_roles_ids)) : !empty(array_intersect($membership_roles_ids, $role_ids));
  });
  $group_ids = [];
  foreach ($memberships as $membership) {
    $group_ids[$membership
      ->getGroupEntityType()][] = $membership
      ->getGroupId();
  }
  return $group_ids;
}