1

I would like to clear cache tag of a particular bundle when any taxonomy terms are update/ saved.

I know the default node tag is saved like node:1, node:2, but I'm confused on how to invalidate a particular bundle.

5

You don't invalidate entity tags, Drupal does this automatically on CRUD operations.

Instead add a cache tag to all nodes of the specific content type:

/**
 * Implements hook_preprocess_node()
 *
 */
function mymodule_preprocess_node(&$variables) {
  if ($variables['node']->getType() === 'my_content_type') {
    $variables['#cache']['tags'][] = 'taxonomy_term_list';
  }
}

The tag taxonomy_term_list gets invalidated on any taxonomy term saved/updated/deleted.

8
  • This works, but it would affect all terms, it is more likely that you'd want do this only with terms from a specific vocabulary. Then you do need to define your own tag, add it and invalidate it. – Berdir Aug 10 '18 at 19:42
  • Thanks @4k4 for your point. Certainly gave me some understanding on how it works. I would like to add one more point - taxonomy_term_list tag is already added in the list of drupal cache tags. How can I test if this works? I checked in the cache_render table but the particular row is still there when I update any terms. – wilNev Aug 10 '18 at 20:17
  • Each entity type has a list tag, for example there is also node_list or user_list. Cache items are not deleted or modified when invalidated. The checksum of the cache item is no longer valid. Test it by rendering the node again and check if you get an old cached version or a new one. – 4k4 Aug 10 '18 at 21:19
  • Will the above code also work for a block which is attached to a certain region in the node page ? I basically have a facet filter block (facet filter names are specific taxonomy terms) . After implementing the above shared code I tried updating a certain term name which was already listed as a facet in the node page, but unfortunately the updated term does not reflect until I clear the cache. Am I missing out any steps? Sorry for the lengthy question I didn't know a proper way to explain rather than giving example. – wilNev Aug 12 '18 at 19:49
  • This should also work in a mymodule_preprocess_block() hook. – 4k4 Aug 12 '18 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.