Verified Commit 9e67c211 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #1937266 by smustgrave, ravi.shankar, immaculatexavier, Berdir, Pavan B...

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
parent da8a916e
Loading
Loading
Loading
Loading
+14 −10
Original line number Diff line number Diff line
@@ -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}
   */
+21 −0
Original line number Diff line number Diff line
@@ -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($this->container->get('entity_type.manager')->getStorage('block')->loadUnchanged($block->id()));
  }

}