function _social_follow_taxonomy_invalidate_follow_tag_cache in Open Social 10.1.x
Same name and namespace in other branches
- 10.3.x modules/social_features/social_follow_taxonomy/social_follow_taxonomy.module \_social_follow_taxonomy_invalidate_follow_tag_cache()
- 10.0.x modules/social_features/social_follow_taxonomy/social_follow_taxonomy.module \_social_follow_taxonomy_invalidate_follow_tag_cache()
- 10.2.x modules/social_features/social_follow_taxonomy/social_follow_taxonomy.module \_social_follow_taxonomy_invalidate_follow_tag_cache()
Invalidates cache for added/removed tags to entity.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: The entity.
2 calls to _social_follow_taxonomy_invalidate_follow_tag_cache()
- social_follow_taxonomy_entity_delete in modules/
social_features/ social_follow_taxonomy/ social_follow_taxonomy.module - Implements hook_entity_delete().
- social_follow_taxonomy_entity_presave in modules/
social_features/ social_follow_taxonomy/ social_follow_taxonomy.module - Implements hook_entity_presave().
File
- modules/
social_features/ social_follow_taxonomy/ social_follow_taxonomy.module, line 518 - Contains social_follow_taxonomy.module.
Code
function _social_follow_taxonomy_invalidate_follow_tag_cache(EntityInterface $entity) {
if (!$entity instanceof ContentEntityInterface || !$entity
->hasField('social_tagging')) {
return;
}
if ($entity
->isNew()) {
if ($entity
->get('social_tagging')
->isEmpty()) {
return;
}
// Added tags.
$tags = array_column($entity
->get('social_tagging')
->getValue(), 'target_id');
}
else {
$tags = [];
/** @var \Drupal\node\NodeInterface $old_entity */
$old_entity = $entity->original;
if (!is_null($old_entity)) {
// Tags before save.
$old_tags = $old_entity
->get('social_tagging')
->getValue();
// Tags after save.
$new_tags = $entity
->get('social_tagging')
->getValue();
// Get removed/added tags.
$tags_removed = array_diff(array_column($old_tags, 'target_id'), array_column($new_tags, 'target_id'));
$tags_added = array_diff(array_column($new_tags, 'target_id'), array_column($old_tags, 'target_id'));
if (!empty($tags_removed)) {
$tags = array_merge($tags, $tags_removed);
}
if (!empty($tags_added)) {
$tags = array_merge($tags, $tags_added);
}
}
else {
$tags = array_column($entity
->get('social_tagging')
->getValue(), 'target_id');
}
}
// Get entity type.
$entity_type = $entity
->getEntityTypeId();
// Invalidate cache for specific tags.
if (!empty($tags)) {
foreach ($tags as $tag) {
$invalidate_tag[] = "follow_tag_{$entity_type}:{$tag}";
}
\Drupal::service('cache_tags.invalidator')
->invalidateTags($invalidate_tag);
}
}