0

I am wondering why the last two entries do not work:

public function getCacheTags() {
  $tags = [
    'node:270',
    'node:271',
    'node:273',
    'node:282',
    'block:customblockname',
    'block:7',
  ];
  $cacheTags = Cache::mergeTags(parent::getCacheTags(), $tags);

  return $cacheTags;
}

When I edit my custom block the URL is:

/block/7?destination=/admin/structure/block/block-content

The actual id of the block (for instance when embedding it) is customblockname.

What are the correct cache tags to include that block in my caching?

3
  • There is a database table named cachetags. Opening that makes me believe blocks are named like this: config:block.block.bartik_branding Where bartik_branding is the system name. So I suppose the pattern to be config:block.block.SYSNAME. You should however be able to use the getCacheTags() method on the block entity. That should always work. Commented Aug 1, 2017 at 9:37
  • its actually just 'block_content:7', :)
    – Alex
    Commented Aug 1, 2017 at 9:53
  • It actually is both. block_content:7 is the content entity with the actual data inside. But config:block.block.machine_name is the config entity of the block config entity, which contains placement information (theme, region, weight, settings, ..). What exactly you need depends on what you are doing, you should not have to hardcode your cache tags like that
    – Berdir
    Commented Aug 1, 2017 at 15:36

1 Answer 1

1

I just figured myself. The cache tag for custom blocks is simply:

block_content:7

4
  • Still I think it would be better to use getCacheTags on the block entity. Seems more solid and future proof. Commented Aug 1, 2017 at 9:55
  • @Neograph734 how would you do that?
    – Alex
    Commented Aug 1, 2017 at 10:03
  • 2
    For custom blocks you should be able to do something like $block = \Drupal\block_content\Entity\BlockContent::load($bid); to obtain the block (or maybe you have loaded it already). Then $block->getCacheTags(); to obtain the cache tag(s). Commented Aug 1, 2017 at 10:06
  • @Neograph734 That is pretty the correct answer, which remains valid even if the Block entity defines new cache tags, or third-party modules add more tags.
    – avpaderno
    Commented Aug 1, 2017 at 10:59

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.