diff --git a/core/core.services.yml b/core/core.services.yml index 491ea25..7e368e9 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -311,8 +311,6 @@ services: plugin.manager.block: class: Drupal\Core\Block\BlockManager parent: default_plugin_manager - tags: - - { name: event_subscriber } plugin.manager.field.field_type: class: Drupal\Core\Field\FieldTypePluginManager parent: default_plugin_manager diff --git a/core/lib/Drupal/Core/Block/BlockManager.php b/core/lib/Drupal/Core/Block/BlockManager.php index f717a2a..3252c42 100644 --- a/core/lib/Drupal/Core/Block/BlockManager.php +++ b/core/lib/Drupal/Core/Block/BlockManager.php @@ -11,11 +11,8 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\Context\ContextAwarePluginManagerTrait; use Drupal\Core\Plugin\DefaultPluginManager; -use Drupal\Core\Plugin\Event\PluginRemovalEvent; -use Drupal\Core\Plugin\PluginRemovalEvents; use Drupal\Core\StringTranslation\StringTranslationTrait; use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** * Manages discovery and instantiation of block plugins. @@ -24,7 +21,7 @@ * * @see \Drupal\Core\Block\BlockPluginInterface */ -class BlockManager extends DefaultPluginManager implements BlockManagerInterface, EventSubscriberInterface { +class BlockManager extends DefaultPluginManager implements BlockManagerInterface { use StringTranslationTrait; use ContextAwarePluginManagerTrait; @@ -120,18 +117,8 @@ public function getSortedDefinitions() { /** * {@inheritdoc} */ - public static function getSubscribedEvents() { - $events[PluginRemovalEvents::PERMANENTLY_UNAVAILABLE][] = 'blockPluginRemoved'; - return $events; - } - - /** - * Reacts on removing a block plugin. - * - * @param \Drupal\Core\Plugin\Event\PluginRemovalEvent $event - * The event containing the plugin ID which is updated. - */ - public function blockPluginRemoved(PluginRemovalEvent $event) { + public function removePlugin($plugin_id, array $conditions = array()) { + parent::removePlugin($plugin_id, $conditions); $this->clearCachedDefinitions(); } diff --git a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php index 3c30bc6..2ba78a1 100644 --- a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php +++ b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php @@ -167,7 +167,7 @@ public function clearCachedDefinitions() { $new_definitions = $this->findDefinitions(); $removed_plugin_ids = array_diff(array_keys($existing_definitions), array_keys($new_definitions)); foreach ($removed_plugin_ids as $removed_plugin_id) { - $this->removePluginId($removed_plugin_id); + $this->removePlugin($removed_plugin_id); } } @@ -249,11 +249,10 @@ protected function findDefinitions() { /** * {@inheritdoc} */ - public function removePluginId($plugin_id, $permanent = TRUE, array $conditions = array()) { - $event_name = $permanent ? PluginRemovalEvents::PERMANENTLY_UNAVAILABLE : PluginRemovalEvents::TEMPORARILY_UNAVAILABLE; + public function removePlugin($plugin_id, array $conditions = array()) { // @todo Introduce a proper plugin type. $plugin_type = isset($this->subdir) ? strtolower(str_replace('/', '_', $this->subdir)) : get_class($this); - $this->eventDispatcher->dispatch($event_name, new PluginRemovalEvent($plugin_type, $plugin_id, $permanent, $conditions)); + $this->eventDispatcher->dispatch(PluginEvents::REMOVAL, new PluginRemovalEvent($plugin_type, $plugin_id, $conditions)); } } diff --git a/core/lib/Drupal/Core/Plugin/DefaultPluginManagerInterface.php b/core/lib/Drupal/Core/Plugin/DefaultPluginManagerInterface.php index 05e6853..c2b0a1b 100644 --- a/core/lib/Drupal/Core/Plugin/DefaultPluginManagerInterface.php +++ b/core/lib/Drupal/Core/Plugin/DefaultPluginManagerInterface.php @@ -11,7 +11,7 @@ use Drupal\Core\Cache\CacheBackendInterface; /** - * Extends the plugin manager interface with drupal specific methods. + * Extends the plugin manager interface with Drupal specific methods. */ interface DefaultPluginManagerInterface extends PluginManagerInterface { @@ -40,22 +40,15 @@ public function setCacheBackend(CacheBackendInterface $cache_backend, $cache_key /** * Announces that the plugin with the specified ID disappears. * - * The plugin might be removed permanently, for example when something on - * which it depends is deleted, or it might be removed temporarily, for - * example when something is disabled but might be re-enabled in the future. - * - * A concrete example is that you want to remove each block configuration - * once a content block is removed. + * A concrete example is that you want to remove each block configuration once + * a content block is removed. * * @param string $plugin_id * The ID of the plugin that is removed. - * @param bool $permanent - * (optional) Indicates whether the plugin is removed permanently or just - * temporarily disabled. * @param array $conditions * (optional) An array of information that is passed on to the subscribers * to help them decide how to react on the plugin removal. */ - public function removePluginId($plugin_id, $permanent = TRUE, array $conditions = array()); + public function removePlugin($plugin_id, array $conditions = array()); } diff --git a/core/lib/Drupal/Core/Plugin/Event/PluginRemovalEvent.php b/core/lib/Drupal/Core/Plugin/Event/PluginRemovalEvent.php index 625a894..628b117 100644 --- a/core/lib/Drupal/Core/Plugin/Event/PluginRemovalEvent.php +++ b/core/lib/Drupal/Core/Plugin/Event/PluginRemovalEvent.php @@ -29,13 +29,6 @@ class PluginRemovalEvent extends Event { protected $pluginId; /** - * Whether or not the removal of the plugin is permanent. - * - * @var bool - */ - protected $permanent; - - /** * An array of conditions. * * These are provided to help subscribers deciding how to act on the removal. @@ -45,22 +38,19 @@ class PluginRemovalEvent extends Event { protected $conditions; /** - * Constructs a PluginRemoveEvent object. + * Constructs a PluginRemovalEvent object. * * @param string $plugin_type * A unique string for each plugin type. * @param string $plugin_id * The ID of the plugin that is being removed. - * @param bool $permanent - * Whether or not the removal of the plugin is permanent. Defaults to TRUE. * @param array $conditions * An array of conditions intended to help subscribers to decide how to act * on the plugin removal. */ - public function __construct($plugin_type, $plugin_id, $permanent = TRUE, array $conditions = array()) { + public function __construct($plugin_type, $plugin_id, array $conditions = array()) { $this->pluginType = $plugin_type; $this->pluginId = $plugin_id; - $this->permanent = $permanent; $this->conditions = $conditions; } @@ -75,16 +65,6 @@ public function getConditions() { } /** - * Returns whether or not the plugin removal is permanent. - * - * @return bool - * TRUE if the removal is permanent, FALSE otherwise. - */ - public function getPersistence() { - return $this->permanent; - } - - /** * Returns the ID of the plugin that is being removed. * * @return string diff --git a/core/lib/Drupal/Core/Plugin/PluginEvents.php b/core/lib/Drupal/Core/Plugin/PluginEvents.php index 2b3efdd..e61fa62 100644 --- a/core/lib/Drupal/Core/Plugin/PluginEvents.php +++ b/core/lib/Drupal/Core/Plugin/PluginEvents.php @@ -8,23 +8,16 @@ namespace Drupal\Core\Plugin; /** - * Contains events fired when a plugin instance becomes unavailable. + * Defines events for plugins. */ class PluginEvents { /** - * This event is thrown when a plugin is temporary unavailable. + * This event is fired when a plugin becomes unavailable. * - * This could be the case, when something is disabled but not deleted. + * This could occur for example when a module is uninstalled or the data from + * which a plugin is derived is deleted. */ - const TEMPORARILY_UNAVAILABLE = 'plugin.unavailable.temporarily'; - - /** - * This event is thrown when a plugin is permanently unavailable. - * - * This could be the case, when a module has been uninstalled or the data - * from which a plugin was derived was deleted. - */ - const PERMANENTLY_UNAVAILABLE = 'plugin.unavailable.permanently'; + const REMOVAL = 'plugin.removal'; } diff --git a/core/modules/aggregator/src/Entity/Feed.php b/core/modules/aggregator/src/Entity/Feed.php index 2736a13..33cc9e5 100644 --- a/core/modules/aggregator/src/Entity/Feed.php +++ b/core/modules/aggregator/src/Entity/Feed.php @@ -118,7 +118,7 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti /** @var \Drupal\Core\Plugin\DefaultPluginManagerInterface $block_manager */ if (\Drupal::hasService('plugin.manager.block') && $block_manager = \Drupal::service('plugin.manager.block')) { foreach ($entities as $entity) { - $block_manager->removePluginId('aggregator_feed_block', FALSE, array('settings.feed' => $entity->id())); + $block_manager->removePlugin('aggregator_feed_block', FALSE, array('settings.feed' => $entity->id())); } } } diff --git a/core/modules/block/src/BlockEntityBlockEventSubscriber.php b/core/modules/block/src/BlockEntityBlockEventSubscriber.php index 2589071..65443e6 100644 --- a/core/modules/block/src/BlockEntityBlockEventSubscriber.php +++ b/core/modules/block/src/BlockEntityBlockEventSubscriber.php @@ -10,7 +10,7 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Plugin\Event\PluginRemovalEvent; -use Drupal\Core\Plugin\PluginRemovalEvents; +use Drupal\Core\Plugin\PluginEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** @@ -49,8 +49,7 @@ public function __construct(QueryFactory $query_factory, EntityManagerInterface * {@inheritdoc} */ public static function getSubscribedEvents() { - $events[PluginRemovalEvents::PERMANENTLY_UNAVAILABLE][] = 'deleteEntityByPlugin'; - $events[PluginRemovalEvents::TEMPORARILY_UNAVAILABLE][] = 'deleteEntityByPlugin'; + $events[PluginEvents::REMOVAL][] = 'deleteEntityByPlugin'; return $events; } diff --git a/core/modules/block_content/src/Entity/BlockContent.php b/core/modules/block_content/src/Entity/BlockContent.php index cc61130..f75181f 100644 --- a/core/modules/block_content/src/Entity/BlockContent.php +++ b/core/modules/block_content/src/Entity/BlockContent.php @@ -132,7 +132,7 @@ public function delete() { // Allow subscribers to react to the removal of the block. /** @var \Drupal\block\BlockManagerInterface $block_manager */ $block_manager = \Drupal::service('plugin.manager.block'); - $block_manager->removePluginId('block_content:' . $this->uuid()); + $block_manager->removePlugin('block_content:' . $this->uuid()); parent::delete(); } diff --git a/core/modules/system/src/Entity/Menu.php b/core/modules/system/src/Entity/Menu.php index 186e828..ab81480 100644 --- a/core/modules/system/src/Entity/Menu.php +++ b/core/modules/system/src/Entity/Menu.php @@ -73,7 +73,7 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti foreach ($entities as $entity) { /** @var \Drupal\block\BlockManagerInterface $block_manager */ $block_manager = \Drupal::service('plugin.manager.block'); - $block_manager->removePluginId('menu_menu_block:' . $entity->id()); + $block_manager->removePlugin('menu_menu_block:' . $entity->id()); } } diff --git a/core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php b/core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php index 194205d..bb6bd59 100644 --- a/core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php @@ -8,7 +8,7 @@ namespace Drupal\Tests\Core\Block; use Drupal\Core\Plugin\Event\PluginRemovalEvent; -use Drupal\Core\Plugin\PluginRemovalEvents; +use Drupal\Core\Plugin\PluginEvents; use Drupal\Tests\UnitTestCase; use Symfony\Component\EventDispatcher\EventDispatcher; @@ -41,7 +41,7 @@ public function testBlockPluginEvents() { $event_dispatcher->addSubscriber($block_manager); $event = new PluginRemovalEvent('plugin_block', 'test_plugin_id'); - $event_dispatcher->dispatch(PluginRemovalEvents::PERMANENTLY_UNAVAILABLE, $event); + $event_dispatcher->dispatch(PluginEvents::REMOVAL, $event); } } diff --git a/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php index cc51194..642d06e 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php @@ -8,7 +8,7 @@ namespace Drupal\Tests\Core\Plugin; use Drupal\Core\Plugin\Event\PluginRemovalEvent; -use Drupal\Core\Plugin\PluginRemovalEvents; +use Drupal\Core\Plugin\PluginEvents; use Drupal\Tests\UnitTestCase; /** @@ -215,15 +215,15 @@ public function testCacheClearWithFilledCacheAndRemovedPlugins() { $plugin_manager->setCacheBackend($cache_backend, $cid); $plugin_manager->setSubDir('Plugin/test/example'); - // Remove the available plugins and ensure that the removePluginId method - // is called automatically. + // Remove the available plugins and ensure that the removePlugin() method is + // called automatically. $static_discovery = $plugin_manager->getDiscovery(); $static_discovery->deleteDefinition('apple'); $plugin_event = new PluginRemovalEvent('plugin_test_example', 'apple'); $event_dispatcher->expects($this->once()) ->method('dispatch') - ->with(PluginRemovalEvents::PERMANENTLY_UNAVAILABLE, $plugin_event); + ->with(PluginEvents::REMOVAL, $plugin_event); $plugin_manager->clearCachedDefinitions(); } @@ -329,11 +329,11 @@ public function testGetDefinitionsWithoutRequiredInterface() { } /** - * Tests the removePluginId method. + * Tests the removePlugin() method. * - * @covers ::removePluginId + * @covers ::removePlugin */ - public function testRemovePluginId() { + public function testRemovePlugin() { $event_dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, $event_dispatcher, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface'); $plugin_manager->setSubDir('Plugin/test/Example'); @@ -341,9 +341,9 @@ public function testRemovePluginId() { $plugin_event = new PluginRemovalEvent('plugin_test_example', 'test_id'); $event_dispatcher->expects($this->once()) ->method('dispatch') - ->with(PluginRemovalEvents::PERMANENTLY_UNAVAILABLE, $plugin_event); + ->with(PluginEvents::REMOVAL, $plugin_event); - $plugin_manager->removePluginId('test_id'); + $plugin_manager->removePlugin('test_id'); } }