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

  1. Create and place the block content in a region using the block layout.
  2. Render the page where the block is placed to ensure it is cached.
  3. Edit the block content.
  4. 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

Issue fork drupal-3466047

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

xavier.masson created an issue. See original summary.

xavier.masson’s picture

Title: BlockContentBlock don't inherit block content entity cacheable dependency » BlockContentBlock does not inherit block content entity cacheable dependency

xavier.masson’s picture

Issue summary: View changes
xavier.masson’s picture

Status: Active » Needs review
smustgrave’s picture

Version: 10.3.x-dev » 11.x-dev
Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests

Thank 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.

larowlan’s picture

Status: Needs work » Postponed (maintainer needs more info)
xavier.masson’s picture

@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!

larowlan’s picture

Status: Postponed (maintainer needs more info) » Active

I think this is reasonable.
I wonder how we go about testing it.

acbramley’s picture

Should this be changed to a feature request then based on #8?

smustgrave’s picture

Category: Bug report » Feature request

@acbramley feature request seems right. Though I’m not entirely sure how to write cache tests..

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

smustgrave’s picture

Do 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

smustgrave’s picture

Issue tags: -Needs tests

Disclosure 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.

smustgrave’s picture

Status: Active » Needs review