0

I have a relatively large production site that uses some custom views to filter by taxonomys and other factors through dropdowns. I have come across the issue that adding a new taxonomy does not allow it to appear in these dropdowns until the cache has been cleared.

I have had a read around and it seems like the most specific thing to do would be to locate the cache tags for these views and invalidate them on creation of a new taxonomy term. However this could be quite complex for me and I was hoping there would be something like cache_clear_all('*', 'cache_view', TRUE); from Drupal 7 so I could just rebuild by views cache when in a hook but this doesn't seem to be the case.

I have come across the D8 drupal_flush_all_caches() method however this appears to clear and rebuild ALL caches with no options to limit its effect. My first question is, what would be the performance effect on my site if I used hook_cron to flush all caches every ~3 hours? Would it be noticeable for the users?

Also, if anyone has a D8 method for clearing specific caches for views/custom blocks etc. I would love to hear them?

  • Normally you use the tag taxonomy_term_list for this. This tag gets invalidated on all CRUD operations, also when a taxonomy term is added. – 4k4 Mar 31 '17 at 16:41
2

You can clear the cache tags of a specific view whenever a term is created.

function hook_taxonomy_term_insert(EntityInterface $term) {
  if ($term->bundle() == 'VOCABULARY_ID') {
    Cache::invalidateTags(Views::load('view_name')->getCacheTags());
  }
}
  • Agreed, this is the best workaround, but I'd argue that this is a core bug, so I'd suggest you open an issue about it. – Berdir Apr 1 '17 at 11:50
  • Code example could be improved a bit though, as it stands, it is pretty bogus, should be yourmodule_taxonomy_term_insert() (or update/presave), then you don't need the TermInterface check. Instead, you want to add a $term->bundle() == 'vocabulary_id' check, to do it as ittle as possible. – Berdir Apr 1 '17 at 11:51
  • @Berdir updated answer accordingly – Eyal Apr 1 '17 at 13:55

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.