Handy cache tags

Component ID

2907310

Component name

Handy cache tags

Component type

module

Maintenance status

Development status

Component security advisory coverage

covered

Downloads

670

Component created

Component changed

Component body

Provides some handy extra cache tags, so you can for example tag a block that deals with a certain node type, with the cache tag of that node type.

The module provides the following cache tags for you to use:

handy_cache_tags:[entity_type]
handy_cache_tags:[entity_type]:[entity_bundle]

For example with nodes of the bundle "article" this would make the following cache tags available:

handy_cache_tags:node
handy_cache_tags:node:article

The first would invalidate on any node (which by coincidence also is provided through the "node_list" cache tag in core). The second would invalidate on any node being created, updated or deleted with the bundle article.

To generate these tags for your render array, you can use the following:

$build['#cache'] = [
  'tags' => [
    // This one generates the bundle specific cache tag.
    handy_cache_tags_get_bundle_tag('node', 'article'),
    // This one generates the entity type cache tag.
    handy_cache_tags_get_tag('node'),
  ],
];

If you render array deals with an entity, you can also use the following helper functions:

// Get all tags for an entity:
handy_cache_tags_get_entity_tags(EntityInterface $entity);
// Get entity type tag from an entity:
handy_cache_tags_get_entity_type_tag_from_entity(EntityInterface $entity);
// Get bundle tag from entity:
handy_cache_tags_get_bundle_tag_from_entity(EntityInterface $entity);