Problem/Motivation
After creating a block content placed in the block overview, if the block is rendered and then the block content is edited, the block render cache is not invalidated.
Steps to reproduce
- Create and place the block content in a region using the block layout.
- Render the page where the block is placed to ensure it is cached.
- Edit the block content.
- Re-render the page and observe that the block content has not been updated.
Proposed resolution
Merge the cache contexts, tags and max age of the block content block with the loaded block content entity (from getEntity).
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
$contexts = parent::getCacheContexts();
if (($entity = $this->getEntity()) && $entity instanceof BlockContentInterface) {
$contexts = Cache::mergeContexts($contexts, $entity->getCacheContexts());
}
return $contexts;
}
...
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Comments
Comment #2
xavier.massonComment #4
xavier.massonComment #5
xavier.massonComment #6
smustgrave commentedThank you for reporting.
Fixes should be against the 11.x branch as the current development branch.
Also may need a test case showing this issue.
Comment #7
larowlanAre you using a custom template here?
https://www.previousnext.com.au/blog/ensuring-drupal-8-block-cache-tags-...
Comment #8
xavier.masson@larowlan you are right, the block template is overridden and doesn't fully render the `content` variable. I agree it fixes the problem, but is it legit to extend the BlockContentBlock cache methods with the encapsulated block content entity? Thanks!
Comment #9
larowlanI think this is reasonable.
I wonder how we go about testing it.
Comment #10
acbramley commentedShould this be changed to a feature request then based on #8?
Comment #11
smustgrave commented@acbramley feature request seems right. Though I’m not entirely sure how to write cache tests..
Comment #13
smustgrave commentedDo we know if this is still an issue I tried to reproduce on main but the block seems to update just fine when using page_cache
Comment #15
smustgrave commentedDisclosure used AI to help come up with a test. Since testing with the steps still passes because the cache was bubbling up somewhere (not sure). This least tests the tags directly.
Update to core/modules/block_content/src/Plugin/Block/BlockContentBlock.php was 100% from original MR.
Comment #16
smustgrave commented