summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2016-08-28 06:22:28 -0500
committerAlex Pott2016-08-28 06:22:43 -0500
commit82bece91c3a3b28c25c8dc3817188a87b1942a14 (patch)
treec15d1b013634bcd5d3a1ffb899040d4e07375b75
parent08aceee1b7c9136e52475174c77483e08cfe0020 (diff)
Issue #2786195 by larowlan: Deleted block is not removed from the block list
(cherry picked from commit 6bdf44e84fdde0c5897f2b236ed7ea622ead4044)
-rw-r--r--core/modules/block_content/src/Entity/BlockContent.php18
-rw-r--r--core/modules/block_content/src/Plugin/Derivative/BlockContent.php2
-rw-r--r--core/modules/block_content/tests/src/Kernel/BlockContentDeletionTest.php61
3 files changed, 79 insertions, 2 deletions
diff --git a/core/modules/block_content/src/Entity/BlockContent.php b/core/modules/block_content/src/Entity/BlockContent.php
index 958fc7a..425117d 100644
--- a/core/modules/block_content/src/Entity/BlockContent.php
+++ b/core/modules/block_content/src/Entity/BlockContent.php
@@ -104,9 +104,15 @@ class BlockContent extends ContentEntityBase implements BlockContentInterface {
*/
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 @@ class BlockContent extends ContentEntityBase implements BlockContentInterface {
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 63b68c0..ab56445 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 @@ class BlockContent extends DeriverBase implements ContainerDeriverInterface {
*/
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 0000000..d7a9444
--- /dev/null
+++ b/core/modules/block_content/tests/src/Kernel/BlockContentDeletionTest.php
@@ -0,0 +1,61 @@
+<?php
+
+namespace Drupal\Tests\block_content\Kernel;
+
+use Drupal\block_content\Entity\BlockContent;
+use Drupal\block_content\Entity\BlockContentType;
+use Drupal\Component\Plugin\PluginBase;
+use Drupal\KernelTests\KernelTestBase;
+
+/**
+ * Tests that deleting a block clears the cached definitions.
+ *
+ * @group block_content
+ */
+class BlockContentDeletionTest extends KernelTestBase {
+
+ /**
+ * {@inheritdoc}
+ */
+ public static $modules = ['block', 'block_content'];
+
+ /**
+ * {@inheritdoc}
+ */
+ public function setUp() {
+ parent::setUp();
+ $this->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));
+ }
+
+}