diff --git a/core/modules/aggregator/aggregator.module b/core/modules/aggregator/aggregator.module index f7a908a..9013963 100644 --- a/core/modules/aggregator/aggregator.module +++ b/core/modules/aggregator/aggregator.module @@ -333,9 +333,8 @@ function aggregator_save_category($edit) { ->execute(); // Make sure there is no active block for this category. if (Drupal::moduleHandler()->moduleExists('block')) { - foreach (entity_load_multiple_by_properties('block', array('plugin' => 'aggregator_category_block:' . $edit['cid'])) as $block) { - $block->delete(); - } + $event = new GenericEvent('aggregator_category_block:' . $edit['cid']); + \Drupal::service('event_dispatcher')->dispatch(BlockPluginEvents::DISAPPEAR_PERMANENT, $event); } $edit['title'] = ''; $op = 'delete'; diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Plugin/Core/Entity/Feed.php b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/Core/Entity/Feed.php index 73c015d..487a3a1 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Plugin/Core/Entity/Feed.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/Core/Entity/Feed.php @@ -13,6 +13,7 @@ use Drupal\Core\Entity\Annotation\EntityType; use Drupal\Core\Annotation\Translation; use Drupal\aggregator\FeedInterface; +use Drupal\block\BlockPluginEvents; /** * Defines the aggregator feed entity class. @@ -227,13 +228,10 @@ public static function preDelete(EntityStorageControllerInterface $storage_contr */ public static function postDelete(EntityStorageControllerInterface $storage_controller, array $entities) { foreach ($entities as $entity) { - // Make sure there is no active block for this feed. - $block_configs = config_get_storage_names_with_prefix('plugin.core.block'); - foreach ($block_configs as $config_id) { - $config = config($config_id); - if ($config->get('id') == 'aggregator_feed_block:' . $entity->id()) { - $config->delete(); - } + // Inform the block plugin system you have deleted a feed. + if (Drupal::moduleHandler()->moduleExists('block')) { + $event = new GenericEvent('aggregator_feed_block:' . $entity->id()); + \Drupal::service('event_dispatcher')->dispatch(BlockPluginEvents::DISAPPEAR_PERMANENT, $event); } } } diff --git a/core/modules/block/block.module b/core/modules/block/block.module index 321f584..ab9976d 100644 --- a/core/modules/block/block.module +++ b/core/modules/block/block.module @@ -583,17 +583,6 @@ function block_user_role_delete($role) { } /** - * Implements hook_menu_delete(). - */ -function block_menu_delete($menu) { - foreach (entity_load_multiple('block') as $block_id => $block) { - if ($block->get('plugin') == 'menu_menu_block:' . $menu->id()) { - $block->delete(); - } - } -} - -/** * Implements hook_admin_paths(). */ function block_admin_paths() { diff --git a/core/modules/block/block.services.yml b/core/modules/block/block.services.yml index ac72e22..137cfe6 100644 --- a/core/modules/block/block.services.yml +++ b/core/modules/block/block.services.yml @@ -1,4 +1,8 @@ services: + block.entity.event.subscriber: + class: Drupal\block\BlockEntityBlockEventSubscriber + tags: + - { name: event_subscriber } plugin.manager.block: class: Drupal\block\Plugin\Type\BlockManager arguments: ['@container.namespaces', '@cache.block', '@language_manager', '@module_handler'] diff --git a/core/modules/block/lib/Drupal/block/BlockEntityBlockEventSubscriber.php b/core/modules/block/lib/Drupal/block/BlockEntityBlockEventSubscriber.php new file mode 100644 index 0000000..06e7225 --- /dev/null +++ b/core/modules/block/lib/Drupal/block/BlockEntityBlockEventSubscriber.php @@ -0,0 +1,36 @@ +getSubject(); + foreach (entity_load_multiple_by_properties('block', array('plugin' => $plugin_id)) as $block) { + $block->delete(); + } + } + +} diff --git a/core/modules/block/lib/Drupal/block/BlockPluginEvents.php b/core/modules/block/lib/Drupal/block/BlockPluginEvents.php index e32e145..7b6af47 100644 --- a/core/modules/block/lib/Drupal/block/BlockPluginEvents.php +++ b/core/modules/block/lib/Drupal/block/BlockPluginEvents.php @@ -20,10 +20,11 @@ class BlockPluginEvents { const DISAPPEAR_TEMPORARY = 'temporary'; /** - * This event is thrown when a block plugin is permanent unavailable. + * This event is thrown when a block plugin is permanently unavailable. * - * This could be the case, when something is disabled but not deleted. + * This could be the case, when a module has been uninstalled or the data + * from which a block plugin was derived was deleted. */ - const DISAPPEAR_PERMANENT = 'temporary'; + const DISAPPEAR_PERMANENT = 'permanent'; } diff --git a/core/modules/block/lib/Drupal/block/Plugin/views/display/Block.php b/core/modules/block/lib/Drupal/block/Plugin/views/display/Block.php index 2399982..82a91f7 100644 --- a/core/modules/block/lib/Drupal/block/Plugin/views/display/Block.php +++ b/core/modules/block/lib/Drupal/block/Plugin/views/display/Block.php @@ -196,9 +196,6 @@ public function remove() { $plugin_id = 'views_block:' . $this->view->storage->id() . '-' . $this->display['id']; $event = new GenericEvent($plugin_id); \Drupal::service('event_dispatcher')->dispatch(BlockPluginEvents::DISAPPEAR_PERMANENT, $event); - foreach (entity_load_multiple_by_properties('block', array('plugin' => $plugin_id)) as $block) { - $block->delete(); - } } } diff --git a/core/modules/block/tests/lib/Drupal/block/Tests/Plugin/Type/BlockManagerTest.php b/core/modules/block/tests/lib/Drupal/block/Tests/Plugin/Type/BlockManagerTest.php deleted file mode 100644 index 839db3b..0000000 --- a/core/modules/block/tests/lib/Drupal/block/Tests/Plugin/Type/BlockManagerTest.php +++ /dev/null @@ -1,42 +0,0 @@ -getMockBuilder('Drupal\block\Plugin\Type\BlockManager') - ->disableOriginalConstructor() - ->setMethods(array('clearCachedDefinitions')) - ->getMock(); - - $block_manager->expects($this->once()) - ->method('clearCachedDefinitions'); - - $event_dispatcher->addSubscriber($block_manager); - $event = new GenericEvent(); - - $event_dispatcher->dispatch(BlockPluginEvents::DISAPPEAR_PERMANENT, $event); - } - -} diff --git a/core/modules/block/tests/lib/Drupal/block_test/Plugin/Type/BlockManagerTest.php b/core/modules/block/tests/lib/Drupal/block_test/Plugin/Type/BlockManagerTest.php new file mode 100644 index 0000000..0551763 --- /dev/null +++ b/core/modules/block/tests/lib/Drupal/block_test/Plugin/Type/BlockManagerTest.php @@ -0,0 +1,51 @@ + 'Block Plugin Event Test', + 'description' => 'Tests block event responses.', + 'group' => 'Block', + ); + } + + /** + * Tests the reaction when firing block plugin events. + */ + public function testBlockPluginEvents() { + $event_dispatcher = new EventDispatcher(); + + $block_manager = $this->getMockBuilder('Drupal\block\Plugin\Type\BlockManager') + ->disableOriginalConstructor() + ->setMethods(array('clearCachedDefinitions')) + ->getMock(); + + $block_manager->expects($this->once()) + ->method('clearCachedDefinitions'); + + $event_dispatcher->addSubscriber($block_manager); + $event = new GenericEvent('test_plugin_id'); + + $event_dispatcher->dispatch(BlockPluginEvents::DISAPPEAR_PERMANENT, $event); + } + +} diff --git a/core/modules/menu/menu.module b/core/modules/menu/menu.module index 850feed..bc0a35d 100644 --- a/core/modules/menu/menu.module +++ b/core/modules/menu/menu.module @@ -18,6 +18,7 @@ use Drupal\menu_link\Plugin\Core\Entity\MenuLink; use Drupal\menu_link\MenuLinkStorageController; use Drupal\node\NodeInterface; +use Drupal\block\BlockPluginEvents; /** * Maximum length of menu name as entered by the user. Database length is 32 @@ -303,9 +304,10 @@ function menu_menu_predelete(Menu $menu) { function menu_menu_delete(Menu $menu) { menu_cache_clear_all(); - // Invalidate the block cache to update menu-based derivatives. - if (module_exists('block')) { - Drupal::service('plugin.manager.block')->clearCachedDefinitions(); + // Inform the block plugin system you have deleted a menu. + if (Drupal::moduleHandler()->moduleExists('block')) { + $event = new GenericEvent('menu_menu_block:' . $menu->id()); + \Drupal::service('event_dispatcher')->dispatch(BlockPluginEvents::DISAPPEAR_PERMANENT, $event); } }