diff -u b/cache_tools.module b/cache_tools.module --- b/cache_tools.module +++ b/cache_tools.module @@ -4,6 +4,7 @@ * @file * Provides various caching enhancements. */ + use Drupal\Core\Hook\Attribute\LegacyHook; use Drupal\cache_tools\Hook\CacheToolsHooks; use Drupal\Core\Entity\EntityInterface; @@ -12,9 +13,8 @@ * Implements hook_entity_type_alter(). */ #[LegacyHook] -function cache_tools_entity_type_alter(array &$entity_types) -{ - \Drupal::service(CacheToolsHooks::class)->entityTypeAlter($entity_types); +function cache_tools_entity_type_alter(array &$entity_types) { + \Drupal::service(CacheToolsHooks::class)->entityTypeAlter($entity_types); } /** @@ -29,9 +29,8 @@ * https://www.drupal.org/project/drupal/issues/2551893 . */ #[LegacyHook] -function cache_tools_entity_insert(EntityInterface $entity) -{ - \Drupal::service(CacheToolsHooks::class)->entityInsert($entity); +function cache_tools_entity_insert(EntityInterface $entity) { + \Drupal::service(CacheToolsHooks::class)->entityInsert($entity); } /** @@ -43,9 +42,8 @@ * a field needs to be allowed for invalidation. */ #[LegacyHook] -function cache_tools_entity_update(EntityInterface $entity) -{ - \Drupal::service(CacheToolsHooks::class)->entityUpdate($entity); +function cache_tools_entity_update(EntityInterface $entity) { + \Drupal::service(CacheToolsHooks::class)->entityUpdate($entity); } /** @@ -59,5 +57,4 @@ #[LegacyHook] -function cache_tools_entity_delete(EntityInterface $entity) -{ - \Drupal::service(CacheToolsHooks::class)->entityDelete($entity); +function cache_tools_entity_delete(EntityInterface $entity) { + \Drupal::service(CacheToolsHooks::class)->entityDelete($entity); } diff -u b/src/Hook/CacheToolsHooks.php b/src/Hook/CacheToolsHooks.php --- b/src/Hook/CacheToolsHooks.php +++ b/src/Hook/CacheToolsHooks.php @@ -6,67 +6,68 @@ use Drupal\Core\Hook\Attribute\Hook; + /** * Hook implementations for cache_tools. */ -class CacheToolsHooks -{ - /** - * Implements hook_entity_type_alter(). - */ - #[Hook('entity_type_alter')] - public function entityTypeAlter(array &$entity_types) - { - /** @var \Drupal\Core\Config\Entity\ConfigEntityType $block */ - $block = $entity_types['block']; - $block->setViewBuilderClass('Drupal\cache_tools\CachewiseBlockViewBuilder'); - } - /** - * Implements hook_entity_insert(). - * - * Invalidates `entitytype_entitybundle_pub` and - * `entitytype_entitybundle_pub:field_name:value` if entity is going to be - * published. Entity type or a field needs to be allowed for invalidation. - * - * @todo - * Use a PublishedEntityCacheTag event subscriber instead of hooks after: - * https://www.drupal.org/project/drupal/issues/2551893 . - */ - #[Hook('entity_insert')] - public function entityInsert(\Drupal\Core\Entity\EntityInterface $entity) - { - /** @var \Drupal\cache_tools\Service\CacheInvalidator $cache_invalidator */ - $cache_invalidator = \Drupal::service('cache_tools.cache.invalidator'); - $cache_invalidator->invalidatePublishedEntity($entity); - $cache_invalidator->invalidatePublishedEntityFields($entity); - } - /** - * Implements hook_entity_update(). - * - * Invalidates `entitytype_entitybundle_pub` if entity is going from unpublished - * state to published and `entitytype_entitybundle_pub:field_name:value` if - * a published state and/or a configured field value is changed. Entity type or - * a field needs to be allowed for invalidation. - */ - #[Hook('entity_update')] - public function entityUpdate(\Drupal\Core\Entity\EntityInterface $entity) - { - /** @var \Drupal\cache_tools\Service\CacheInvalidator $cache_invalidator */ - $cache_invalidator = \Drupal::service('cache_tools.cache.invalidator'); - $cache_invalidator->invalidatePublishedEntity($entity); - $cache_invalidator->invalidatePublishedEntityFields($entity); - } - /** - * Implements hook_entity_delete(). - * - * Invalidates `entitytype_entitybundle_pub:field_name:value` if deleting - * published entity. - * Entity type or a field needs to be allowed for invalidation. - * Note: Deleted entities are invalidated via entity_type:id (eg node:123). - */ - #[Hook('entity_delete')] - public function entityDelete(\Drupal\Core\Entity\EntityInterface $entity) - { - /** @var \Drupal\cache_tools\Service\CacheInvalidator $cache_invalidator */ - $cache_invalidator = \Drupal::service('cache_tools.cache.invalidator'); - $cache_invalidator->invalidatePublishedEntityFields($entity); - } +class CacheToolsHooks { + + /** + * Implements hook_entity_type_alter(). + */ + #[Hook('entity_type_alter')] + public function entityTypeAlter(array &$entity_types) { + /** @var \Drupal\Core\Config\Entity\ConfigEntityType $block */ + $block = $entity_types['block']; + $block->setViewBuilderClass('Drupal\cache_tools\CachewiseBlockViewBuilder'); + } + + /** + * Implements hook_entity_insert(). + * + * Invalidates `entitytype_entitybundle_pub` and + * `entitytype_entitybundle_pub:field_name:value` if entity is going to be + * published. Entity type or a field needs to be allowed for invalidation. + * + * @todo + * Use a PublishedEntityCacheTag event subscriber instead of hooks after: + * https://www.drupal.org/project/drupal/issues/2551893 . + */ + #[Hook('entity_insert')] + public function entityInsert(EntityInterface $entity) { + /** @var \Drupal\cache_tools\Service\CacheInvalidator $cache_invalidator */ + $cache_invalidator = \Drupal::service('cache_tools.cache.invalidator'); + $cache_invalidator->invalidatePublishedEntity($entity); + $cache_invalidator->invalidatePublishedEntityFields($entity); + } + + /** + * Implements hook_entity_update(). + * + * Invalidates `entitytype_entitybundle_pub` if entity is going from unpublished + * state to published and `entitytype_entitybundle_pub:field_name:value` if + * a published state and/or a configured field value is changed. Entity type or + * a field needs to be allowed for invalidation. + */ + #[Hook('entity_update')] + public function entityUpdate(EntityInterface $entity) { + /** @var \Drupal\cache_tools\Service\CacheInvalidator $cache_invalidator */ + $cache_invalidator = \Drupal::service('cache_tools.cache.invalidator'); + $cache_invalidator->invalidatePublishedEntity($entity); + $cache_invalidator->invalidatePublishedEntityFields($entity); + } + + /** + * Implements hook_entity_delete(). + * + * Invalidates `entitytype_entitybundle_pub:field_name:value` if deleting + * published entity. + * Entity type or a field needs to be allowed for invalidation. + * Note: Deleted entities are invalidated via entity_type:id (eg node:123). + */ + #[Hook('entity_delete')] + public function entityDelete(EntityInterface $entity) { + /** @var \Drupal\cache_tools\Service\CacheInvalidator $cache_invalidator */ + $cache_invalidator = \Drupal::service('cache_tools.cache.invalidator'); + $cache_invalidator->invalidatePublishedEntityFields($entity); + } + } diff -u b/src/Service/CacheInvalidator.php b/src/Service/CacheInvalidator.php --- b/src/Service/CacheInvalidator.php +++ b/src/Service/CacheInvalidator.php @@ -133,7 +133,7 @@ * @return string[] * The custom cache tags `entitytype_entitybundle_pub:field_name:value`. */ - public function getPublishedEntityFieldsCacheTags(FieldableEntityInterface $entity, ?FieldableEntityInterface $entity_compare = NULL) { + public function getPublishedEntityFieldsCacheTags(FieldableEntityInterface $entity, FieldableEntityInterface $entity_compare = NULL) { $tags = []; $entity_type = $entity->getEntityTypeId(); // Get field-based tags configured for current entity bundle. @@ -208,8 +208,8 @@ * @param string $tag_prefix_field * Prefix for generated tags. * @param bool $invalidate_term_parents - * Flag to indicate invalidation strategy. - * Only applicable for entities of type taxonomy_term. + * Flag to indicate invalidation strategy. + * Only applicable for entities of type taxonomy_term. * * @return array * An array of cache tags to invalidate.