You are here

function og_invalidate_group_content_cache_tags in Organic groups 8

Invalidates group content cache tags for the groups this entity belongs to.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The group content entity that is being created, changed or deleted and is the direct cause for the need to invalidate cached group content.

3 calls to og_invalidate_group_content_cache_tags()
og_entity_delete in ./og.module
Implements hook_entity_delete().
og_entity_insert in ./og.module
Implements hook_entity_insert().
og_entity_update in ./og.module
Implements hook_entity_update().

File

./og.module, line 334

Code

function og_invalidate_group_content_cache_tags(EntityInterface $entity) {

  // If group content is created or updated, invalidate the group content cache
  // tags for each of the groups this group content belongs to. This allows
  // group listings to be cached effectively. The cache tag format is
  // 'og-group-content:{group entity type}:{group entity id}'.
  $is_group_content = Og::isGroupContent($entity
    ->getEntityTypeId(), $entity
    ->bundle());
  if ($is_group_content) {

    /** @var \Drupal\og\MembershipManagerInterface $membership_manager */
    $membership_manager = \Drupal::service('og.membership_manager');
    $tags = [];

    // If the entity is a group content and we came here as an effect of an
    // update, check if any of the OG audience fields have been changed. This
    // means the group(s) of the entity changed and we should also invalidate
    // the tags of the old group(s).

    /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
    $original = !empty($entity->original) ? $entity->original : NULL;
    if ($original) {

      /** @var \Drupal\og\OgGroupAudienceHelperInterface $group_audience_helper */
      $group_audience_helper = \Drupal::service('og.group_audience_helper');

      /** @var \Drupal\Core\Entity\FieldableEntityInterface $original */
      foreach ($group_audience_helper
        ->getAllGroupAudienceFields($entity
        ->getEntityTypeId(), $entity
        ->bundle()) as $field) {
        $field_name = $field
          ->getName();

        /** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $original_field_item_list */
        $original_field_item_list = $original
          ->get($field_name);
        if (!$entity
          ->get($field_name)
          ->equals($original_field_item_list)) {
          foreach ($original_field_item_list
            ->referencedEntities() as $old_group) {
            $tags = Cache::mergeTags($tags, $old_group
              ->getCacheTagsToInvalidate());
          }
        }
      }
    }
    foreach ($membership_manager
      ->getGroups($entity) as $groups) {

      /** @var \Drupal\Core\Entity\ContentEntityInterface $group */
      foreach ($groups as $group) {
        $tags = Cache::mergeTags($tags, $group
          ->getCacheTagsToInvalidate());
      }
    }
    Cache::invalidateTags(Cache::buildTags('og-group-content', $tags));
  }
}