diff --git a/core/modules/block_content/src/Entity/BlockContent.php b/core/modules/block_content/src/Entity/BlockContent.php index 958fc7ac8d803ef7693758ea8306cc6e446bcb2c..425117d830b73f8a09b2bab4cd5e6dde31b3c613 100644 --- a/core/modules/block_content/src/Entity/BlockContent.php +++ b/core/modules/block_content/src/Entity/BlockContent.php @@ -104,9 +104,15 @@ public function getTheme() { */ public function postSave(EntityStorageInterface $storage, $update = TRUE) { parent::postSave($storage, $update); + static::invalidateBlockPluginCache(); + } - // Invalidate the block cache to update custom block-based derivatives. - \Drupal::service('plugin.manager.block')->clearCachedDefinitions(); + /** + * {@inheritdoc} + */ + public static function postDelete(EntityStorageInterface $storage, array $entities) { + parent::postDelete($storage, $entities); + static::invalidateBlockPluginCache(); } /** @@ -237,4 +243,12 @@ public function setRevisionLog($revision_log) { return $this; } + /** + * Invalidates the block plugin cache after changes and deletions. + */ + protected static function invalidateBlockPluginCache() { + // Invalidate the block cache to update custom block-based derivatives. + \Drupal::service('plugin.manager.block')->clearCachedDefinitions(); + } + } diff --git a/core/modules/block_content/src/Plugin/Derivative/BlockContent.php b/core/modules/block_content/src/Plugin/Derivative/BlockContent.php index 63b68c07dc9bd05172c70b296908fb00cd113929..ab564451cb28e55f63562f8fc6b0fa1f58fbe35e 100644 --- a/core/modules/block_content/src/Plugin/Derivative/BlockContent.php +++ b/core/modules/block_content/src/Plugin/Derivative/BlockContent.php @@ -44,6 +44,8 @@ public static function create(ContainerInterface $container, $base_plugin_id) { */ public function getDerivativeDefinitions($base_plugin_definition) { $block_contents = $this->blockContentStorage->loadMultiple(); + // Reset the discovered definitions. + $this->derivatives = []; /** @var $block_content \Drupal\block_content\Entity\BlockContent */ foreach ($block_contents as $block_content) { $this->derivatives[$block_content->uuid()] = $base_plugin_definition; diff --git a/core/modules/block_content/tests/src/Kernel/BlockContentDeletionTest.php b/core/modules/block_content/tests/src/Kernel/BlockContentDeletionTest.php new file mode 100644 index 0000000000000000000000000000000000000000..d7a9444e8e7e061bb2b33fb2ebe14d4c470f0f80 --- /dev/null +++ b/core/modules/block_content/tests/src/Kernel/BlockContentDeletionTest.php @@ -0,0 +1,61 @@ +installEntitySchema('block_content'); + } + + /** + * Tests deleting a block_content updates the discovered block plugin. + */ + public function testDeletingBlockContentShouldClearPluginCache() { + // Create a block content type. + $block_content_type = BlockContentType::create([ + 'id' => 'spiffy', + 'label' => 'Mucho spiffy', + 'description' => "Provides a block type that increases your site's spiffiness by upto 11%", + ]); + $block_content_type->save(); + // And a block content entity. + $block_content = BlockContent::create([ + 'info' => 'Spiffy prototype', + 'type' => 'spiffy', + ]); + $block_content->save(); + + // Make sure the block content provides a derivative block plugin in the + // block repository. + /** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */ + $block_manager = $this->container->get('plugin.manager.block'); + $plugin_id = 'block_content' . PluginBase::DERIVATIVE_SEPARATOR . $block_content->uuid(); + $this->assertTrue($block_manager->hasDefinition($plugin_id)); + + // Now delete the block content entity. + $block_content->delete(); + // The plugin should no longer exist. + $this->assertFalse($block_manager->hasDefinition($plugin_id)); + } + +}