diff --git a/core/modules/block_content/src/Entity/BlockContent.php b/core/modules/block_content/src/Entity/BlockContent.php index 95ba40da6d..b12536d95d 100644 --- a/core/modules/block_content/src/Entity/BlockContent.php +++ b/core/modules/block_content/src/Entity/BlockContent.php @@ -126,6 +126,20 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) { } } + /** + * {@inheritdoc} + */ + public static function preDelete(EntityStorageInterface $storage, array $entities) { + parent::preDelete($storage, $entities); + + /** @var \Drupal\block_content\BlockContentInterface $block */ + foreach ($entities as $block) { + foreach ($block->getInstances() as $instance) { + $instance->delete(); + } + } + } + /** * {@inheritdoc} */ @@ -162,16 +176,6 @@ public function preSaveRevision(EntityStorageInterface $storage, \stdClass $reco } } - /** - * {@inheritdoc} - */ - public function delete() { - foreach ($this->getInstances() as $instance) { - $instance->delete(); - } - parent::delete(); - } - /** * {@inheritdoc} */ diff --git a/core/modules/block_content/tests/src/Kernel/BlockContentDeletionTest.php b/core/modules/block_content/tests/src/Kernel/BlockContentDeletionTest.php index 14833adff6..90eea96a1a 100644 --- a/core/modules/block_content/tests/src/Kernel/BlockContentDeletionTest.php +++ b/core/modules/block_content/tests/src/Kernel/BlockContentDeletionTest.php @@ -6,6 +6,7 @@ use Drupal\block_content\Entity\BlockContentType; use Drupal\Component\Plugin\PluginBase; use Drupal\KernelTests\KernelTestBase; +use Drupal\Tests\block\Traits\BlockCreationTrait; /** * Tests that deleting a block clears the cached definitions. @@ -14,6 +15,8 @@ */ class BlockContentDeletionTest extends KernelTestBase { + use BlockCreationTrait; + /** * {@inheritdoc} */ @@ -57,6 +60,24 @@ public function testDeletingBlockContentShouldClearPluginCache() { $block_content->delete(); // The plugin should no longer exist. $this->assertFalse($block_manager->hasDefinition($plugin_id)); + + // Create another block content entity. + $block_content = BlockContent::create([ + 'info' => 'Spiffy prototype', + 'type' => 'spiffy', + ]); + $block_content->save(); + + $plugin_id = 'block_content' . PluginBase::DERIVATIVE_SEPARATOR . $block_content->uuid(); + $block = $this->placeBlock($plugin_id, ['region' => 'content']); + + // Delete it via storage. + $storage = $this->container->get('entity_type.manager')->getStorage('block_content'); + $storage->delete([$block_content]); + // The plugin should no longer exist. + $this->assertFalse($block_manager->hasDefinition($plugin_id)); + + $this->assertNull(\Drupal::entityTypeManager()->getStorage('block')->loadUnchanged($block->id())); } }