You are here

public function MembershipManager::getGroupIds in Organic groups 8

Returns all group IDs associated with the given group content entity.

Do not use this to retrieve group IDs associated with a user entity. Use MembershipManager::getUserGroups() instead.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The group content entity for which to return the associated groups.

string $group_type_id: Filter results to only include group IDs of this entity type.

string $group_bundle: Filter list to only include group IDs with this bundle.

Return value

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

Throws

\InvalidArgumentException Thrown when a user entity is passed in.

Overrides MembershipManagerInterface::getGroupIds

See also

\Drupal\og\GroupMembershipInterface::getUserGroups()

2 calls to MembershipManager::getGroupIds()
MembershipManager::getGroupCount in src/MembershipManager.php
Returns the number of groups associated with a given group content entity.
MembershipManager::getGroups in src/MembershipManager.php
Returns all groups that are associated with the given group content entity.

File

src/MembershipManager.php, line 261

Class

MembershipManager
Service for managing memberships and group content.

Namespace

Drupal\og

Code

public function getGroupIds(EntityInterface $entity, $group_type_id = NULL, $group_bundle = NULL) {

  // This does not work for user entities.
  if ($entity instanceof UserInterface) {
    throw new \InvalidArgumentException('\\Drupal\\og\\MembershipManager::getGroupIds() cannot be used for user entities. Use \\Drupal\\og\\MembershipManager::getUserGroups() instead.');
  }

  // This should only be called on group content types.
  if (!$this->groupAudienceHelper
    ->hasGroupAudienceField($entity
    ->getEntityTypeId(), $entity
    ->bundle())) {
    throw new \InvalidArgumentException('Can only retrieve group IDs for group content entities.');
  }
  $cid = [
    __METHOD__,
    $entity
      ->getEntityTypeId(),
    $entity
      ->id(),
    $group_type_id,
    $group_bundle,
  ];
  $cid = implode(':', $cid);
  if ($group_ids = $this->staticCache
    ->get($cid)->data ?? []) {

    // Return cached values.
    return $group_ids;
  }
  $group_ids = [];
  $tags = $entity
    ->getCacheTagsToInvalidate();
  $fields = $this->groupAudienceHelper
    ->getAllGroupAudienceFields($entity
    ->getEntityTypeId(), $entity
    ->bundle(), $group_type_id, $group_bundle);
  foreach ($fields as $field) {
    $target_type_id = $field
      ->getFieldStorageDefinition()
      ->getSetting('target_type');
    $target_type_definition = $this->entityTypeManager
      ->getDefinition($target_type_id);

    // Optionally filter by group type.
    if (!empty($group_type_id) && $group_type_id !== $target_type_id) {
      continue;
    }
    $values = $entity
      ->get($field
      ->getName())
      ->getValue();
    if (empty($values[0])) {

      // Entity doesn't reference any groups.
      continue;
    }

    // Compile a list of group target IDs.
    $target_ids = array_filter(array_map(function ($value) {
      return $value['target_id'] ?? NULL;
    }, $entity
      ->get($field
      ->getName())
      ->getValue()));
    if (empty($target_ids)) {
      continue;
    }

    // Query the database to get the actual list of groups. The target IDs may
    // contain groups that no longer exist. Entity reference doesn't clean up
    // orphaned target IDs.
    $entity_type = $this->entityTypeManager
      ->getDefinition($target_type_id);
    $query = $this->entityTypeManager
      ->getStorage($target_type_id)
      ->getQuery()
      ->accessCheck(FALSE)
      ->condition($entity_type
      ->getKey('id'), $target_ids, 'IN');

    // Optionally filter by group bundle.
    if (!empty($group_bundle)) {
      $query
        ->condition($entity_type
        ->getKey('bundle'), $group_bundle);
    }

    // Add the list cache tags for the entity type, so that the cache gets
    // invalidated if one of the group entities is deleted.
    $tags = Cache::mergeTags($target_type_definition
      ->getListCacheTags(), $tags);
    $group_ids = NestedArray::mergeDeep($group_ids, [
      $target_type_id => $query
        ->execute(),
    ]);
  }
  $this->staticCache
    ->set($cid, $group_ids, Cache::PERMANENT, $tags);
  return $group_ids;
}