diff --git a/core/modules/content_moderation/tests/src/Functional/LayoutBuilderContentModerationIntegrationTest.php b/core/modules/content_moderation/tests/src/Functional/LayoutBuilderContentModerationIntegrationTest.php index 31819899c4ebf1d16fabe955d3fba3ca3749d9eb..35834e3bb35d535e613bcf4af6b3535eafc64c5c 100644 --- a/core/modules/content_moderation/tests/src/Functional/LayoutBuilderContentModerationIntegrationTest.php +++ b/core/modules/content_moderation/tests/src/Functional/LayoutBuilderContentModerationIntegrationTest.php @@ -43,11 +43,8 @@ protected function setUp(): void { // https://www.drupal.org/project/drupal/issues/2917777. $this->drupalPlaceBlock('local_tasks_block'); - $workflow = $this->createEditorialWorkflow(); - - // Add a new bundle and add an editorial workflow. + // Add a new bundle. $this->createContentType(['type' => 'bundle_with_section_field']); - $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'bundle_with_section_field'); // Add a new block content bundle to the editorial workflow. BlockContentType::create([ @@ -57,14 +54,29 @@ protected function setUp(): void { ])->save(); block_content_add_body_field('basic'); - $workflow->getTypePlugin()->addEntityTypeAndBundle('block_content', 'basic'); - $workflow->save(); - // Enable layout overrides. LayoutBuilderEntityViewDisplay::load('node.bundle_with_section_field.default') ->enableLayoutBuilder() ->setOverridable() ->save(); + // Create a node before enabling the workflow on the bundle. + $node = $this->createNode([ + 'type' => 'bundle_with_section_field', + 'title' => 'Pre-workflow node', + 'body' => [ + [ + 'value' => 'The first node body', + ], + ], + ]); + // View the node to ensure the new extra field blocks are not cached when + // the workflow is updated. + $this->drupalGet($node->toUrl()); + // Add editorial workflow for the bundle. + $workflow = $this->createEditorialWorkflow(); + $workflow->getTypePlugin()->addEntityTypeAndBundle('block_content', 'basic'); + $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'bundle_with_section_field'); + $workflow->save(); $this->drupalLogin($this->drupalCreateUser([ 'configure any layout', diff --git a/core/modules/layout_builder/layout_builder.services.yml b/core/modules/layout_builder/layout_builder.services.yml index c70f15534275497997518bd949ed7f3eb957a7ed..296100aeb9c46a455dde35b4354f3e572f7c5437 100644 --- a/core/modules/layout_builder/layout_builder.services.yml +++ b/core/modules/layout_builder/layout_builder.services.yml @@ -37,6 +37,12 @@ services: arguments: ['@current_route_match'] tags: - { name: cache.context } + layout_builder.extra_fields.invalidator: + class: Drupal\layout_builder\Cache\ExtraFieldBlockCacheTagInvalidator + arguments: ['@plugin.manager.block'] + public: false + tags: + - { name: cache_tags_invalidator } layout_builder.sample_entity_generator: class: Drupal\layout_builder\Entity\LayoutBuilderSampleEntityGenerator arguments: ['@tempstore.shared', '@entity_type.manager'] diff --git a/core/modules/layout_builder/src/Cache/ExtraFieldBlockCacheTagInvalidator.php b/core/modules/layout_builder/src/Cache/ExtraFieldBlockCacheTagInvalidator.php new file mode 100644 index 0000000000000000000000000000000000000000..a21dcc913203a40b80933640c9ba0b1a62f9d41b --- /dev/null +++ b/core/modules/layout_builder/src/Cache/ExtraFieldBlockCacheTagInvalidator.php @@ -0,0 +1,37 @@ +<?php + +namespace Drupal\layout_builder\Cache; + +use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface; +use Drupal\Core\Block\BlockManagerInterface; +use Drupal\Core\Cache\CacheTagsInvalidatorInterface; + +/** + * Provides a cache tag invalidator that clears the block cache. + * + * @internal + * Tagged services are internal. + */ +class ExtraFieldBlockCacheTagInvalidator implements CacheTagsInvalidatorInterface { + + /** + * Constructs a new ExtraFieldBlockCacheTagInvalidator. + * + * @param \Drupal\Core\Block\BlockManagerInterface $blockManager + * The block manager. + */ + public function __construct(protected BlockManagerInterface $blockManager) { + } + + /** + * {@inheritdoc} + */ + public function invalidateTags(array $tags) { + if (in_array('entity_field_info', $tags, TRUE)) { + if ($this->blockManager instanceof CachedDiscoveryInterface) { + $this->blockManager->clearCachedDefinitions(); + } + } + } + +}