public function MembershipManager::getGroupMembershipIdsByRoleNames in Organic groups 8
Returns the membership IDs of the given group filtered by role names.
Parameters
\Drupal\Core\Entity\EntityInterface $group: The group entity for which to return the memberships.
array $role_names: An array of role names to filter by. In order to retrieve a list of all membership IDs, pass `[OgRoleInterface::AUTHENTICATED]`.
array $states: (optional) Array with the states to return. Defaults to only returning active membership IDs. In order to retrieve all membership IDs regardless of state, pass `OgMembershipInterface::ALL_STATES`.
Return value
\Drupal\Core\Entity\EntityInterface[] The membership entities.
Overrides MembershipManagerInterface::getGroupMembershipIdsByRoleNames
1 call to MembershipManager::getGroupMembershipIdsByRoleNames()
- MembershipManager::getGroupMembershipsByRoleNames in src/
MembershipManager.php - Returns the memberships of the given group filtered by role name.
File
- src/
MembershipManager.php, line 184
Class
- MembershipManager
- Service for managing memberships and group content.
Namespace
Drupal\ogCode
public function getGroupMembershipIdsByRoleNames(EntityInterface $group, array $role_names, array $states = [
OgMembershipInterface::STATE_ACTIVE,
]) {
if (empty($role_names)) {
throw new \InvalidArgumentException('The array of role names should not be empty.');
}
// In case the 'member' role is one of the requested roles, we just need to
// return all memberships. We can safely ignore all other roles.
$retrieve_all_memberships = FALSE;
if (in_array(OgRoleInterface::AUTHENTICATED, $role_names)) {
$retrieve_all_memberships = TRUE;
$role_names = [
OgRoleInterface::AUTHENTICATED,
];
}
$role_names = $this
->prepareConditionArray($role_names);
$states = $this
->prepareConditionArray($states, OgMembership::ALL_STATES);
$cid = [
__METHOD__,
$group
->getEntityTypeId(),
$group
->id(),
implode('|', $role_names),
implode('|', $states),
];
$cid = implode(':', $cid);
// Only query the database if no cached result exists.
if (!($membership_ids = $this->staticCache
->get($cid)->data ?? [])) {
$entity_type_id = $group
->getEntityTypeId();
$query = $this->entityTypeManager
->getStorage('og_membership')
->getQuery()
->condition('entity_type', $entity_type_id)
->condition('entity_id', $group
->id())
->condition('state', $states, 'IN');
if (!$retrieve_all_memberships) {
$bundle_id = $group
->bundle();
$role_ids = array_map(function ($role_name) use ($entity_type_id, $bundle_id) {
return implode('-', [
$entity_type_id,
$bundle_id,
$role_name,
]);
}, $role_names);
$query
->condition('roles', $role_ids, 'IN');
}
$membership_ids = $query
->execute();
$this
->cacheMembershipIds($cid, $membership_ids);
}
return $membership_ids;
}