Editing a block via context doesn't clear cache for that block, I need to manually clear cache every time after I edit a block config. I think the fix might be to call Cache::invalidateTags() on the block tags every time that block gets saved.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

George Bills created an issue. See original summary.

George Bills’s picture

Version: 8.x-4.x-dev » 8.x-4.0-beta2

It seems like:

  1. "Normal" blocks get a block entity, Drupal\block\Entity\Block, which extends ConfigEntityBase which extends Entity.
  2. Saving any Entity calls postSave() calls invalidateTagsOnSave() calls Cache::invalidateTags($this->getCacheTagsToInvalidate());
  3. Context blocks, don't have a "normal" block entity, instead they have Drupal\context\Entity\Context.
  4. Saving that context entity should automatically call Cache::invalidateTags() just like any other entity (but I haven't tested that).
  5. Drupal\context\Plugin\ContextReaction\Blocks::execute() doesn't add the context cache tags to the block it renders, so Cache::invalidateTags($the_context_tags) won't invalidate the cached block rendering.

So I think the fix is actually just to add the context entity cache tags to the block render array we return from Blocks::execute(). We shouldn't need to explicitly call Cache::invalidateTags().

George Bills’s picture

Attached is a "proof of concept" for a fix, I haven't figured out how to get access to the current context from inside the Blocks ContextReaction plugin yet; possibly need to pass it down from ContextManager? Also need to DI the entity type manager.

With this patch changing block configuration for a context block on my homepage will refresh cache for that block, without this patch changing block configuration for a context block on my homepage won't take show any effect until I manually flush cache.

George Bills’s picture

Actually attach the patch.

George Bills’s picture

Status: Active » Needs review
FileSize
3.18 KB

Actual patch (was easier than I thought to get the context) - this works on my local.

nace_fr’s picture

I fixed the patch apply failing.

nace_fr’s picture

FileSize
3.07 KB
strozx’s picture

Tested the patch and it works. Thank you Nace

strozx’s picture

Status: Needs review » Reviewed & tested by the community
Feng-Shui’s picture

This is a re-roll against 8.x-4.0-beta2, not against head - the two patches above didn't apply on the current release.

  • boshtian committed 438c2a0 on 8.x-4.x authored by George Bills
    Issue #2979259 by George Bills, nace_fric, strozx: Saving a block doesn'...
boshtian’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.