Issue #1937266 by smustgrave, ravi.shankar, immaculatexavier, Berdir, Pavan B S, arunkumark, manuel.adan, kfitz, pixelcab, lokapujya: BlockContent entities uses delete method instead of pre/postdelete

(cherry picked from commit 9e67c21100)
merge-requests/2809/head
Alex Pott 2022-09-26 16:54:13 +01:00
parent 348d20be24
commit ba44f6430e
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
2 changed files with 35 additions and 10 deletions

View File

@ -126,6 +126,20 @@ class BlockContent extends EditorialContentEntityBase implements BlockContentInt
}
}
/**
* {@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 @@ class BlockContent extends EditorialContentEntityBase implements BlockContentInt
}
}
/**
* {@inheritdoc}
*/
public function delete() {
foreach ($this->getInstances() as $instance) {
$instance->delete();
}
parent::delete();
}
/**
* {@inheritdoc}
*/

View File

@ -6,6 +6,7 @@ use Drupal\block_content\Entity\BlockContent;
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 @@ use Drupal\KernelTests\KernelTestBase;
*/
class BlockContentDeletionTest extends KernelTestBase {
use BlockCreationTrait;
/**
* {@inheritdoc}
*/
@ -57,6 +60,24 @@ class BlockContentDeletionTest extends KernelTestBase {
$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($this->container->get('entity_type.manager')->getStorage('block')->loadUnchanged($block->id()));
}
}