0

I'm currently porting a Drupal module from 6.x -> 8.x. One of the lines in that module:

cache_clear_all('##NAME_OF_THE_BLOCK##', 'cache_block', TRUE);

The cache_clear_all method does no longer exist in Drupal 8.x. I cannot find now where the 'cache_block' is situated. I'm looking for a way to flush this cache - and I don't want to flush 'all' cache ...

Any idea's what the D8 equivalent of this line is?

1
  • Load the block object then call \Drupal\Core\Cache\Cache::invalidateTags($block->getCacheTagsToInvalidate());
    – Clive
    Commented May 24, 2017 at 10:30

1 Answer 1

1

The D8 equivalent is cache tags and it kind of works in the reversed way.

What exactly is changing that requires you to invalidate the cache? Identify that, if it has a cache tag (e.g. a config object or an entity) then just make sure your block adds the cache tag for it and you're done.

To figure out if a cache tag invalidation happens, you can truncate the cachetags table, then make the change and check that table for new entries.

If there is no cache tag yet for that event, then you can define one yourself, add it to the render array or getCacheTags() of the block plugin and invalidate it with Cache::invalidateTags().

I would not recommend to do what @Clive suggested because unlike 7.x, your block plugin (assuming it is a plugin not just one specific block instance) can be re-used many times, even with page_manager/panels.

3
  • Hi, thanks for your answer! In hook_cron() some functionallity happens which updates a MySQL. So in the cron() method, I have to add the cache tag. Right? At that moment, I need to invalidate the cache of the blocks (by the way, yes, I've created a plugin for my block). Commented May 24, 2017 at 11:49
  • Yes, a cache tag is just a unique string essentially. So something like return ['yourmodule-something'] in getCacheTags() and then pass that to invalidateTags().
    – Berdir
    Commented May 24, 2017 at 13:57
  • Thnx, got it! Will give this a try :) Commented May 24, 2017 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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