Unverified Commit 84b902eb authored by alexpott's avatar alexpott

Issue #2966969 by tim.plunkett, msankhala, alexpott: Trigger a warning...

Issue #2966969 by tim.plunkett, msankhala, alexpott: Trigger a warning whenever the "Broken" block plugin is instantiated
parent cc59fcb0
......@@ -608,6 +608,7 @@ services:
plugin.manager.block:
class: Drupal\Core\Block\BlockManager
parent: default_plugin_manager
arguments: ['@logger.channel.default']
plugin.manager.field.field_type:
class: Drupal\Core\Field\FieldTypePluginManager
arguments: ['@container.namespaces', '@cache.discovery', '@module_handler', '@typed_data_manager']
......
......@@ -76,8 +76,7 @@ public function createInstance($plugin_id, array $configuration = []) {
return $this->getFactory()->createInstance($plugin_id, $configuration);
}
catch (PluginNotFoundException $e) {
$fallback_id = $this->getFallbackPluginId($plugin_id, $configuration);
return $this->getFactory()->createInstance($fallback_id, $configuration);
return $this->handlePluginNotFound($plugin_id, $configuration);
}
}
else {
......@@ -85,6 +84,22 @@ public function createInstance($plugin_id, array $configuration = []) {
}
}
/**
* Allows plugin managers to specify custom behavior if a plugin is not found.
*
* @param string $plugin_id
* The ID of the missing requested plugin.
* @param array $configuration
* An array of configuration relevant to the plugin instance.
*
* @return object
* A fallback plugin instance.
*/
protected function handlePluginNotFound($plugin_id, array $configuration) {
$fallback_id = $this->getFallbackPluginId($plugin_id, $configuration);
return $this->getFactory()->createInstance($fallback_id, $configuration);
}
/**
* {@inheritdoc}
*/
......
......@@ -8,6 +8,7 @@
use Drupal\Core\Plugin\CategorizingPluginManagerTrait;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\FilteredPluginManagerTrait;
use Psr\Log\LoggerInterface;
/**
* Manages discovery and instantiation of block plugins.
......@@ -23,6 +24,13 @@ class BlockManager extends DefaultPluginManager implements BlockManagerInterface
}
use FilteredPluginManagerTrait;
/**
* The logger.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* Constructs a new \Drupal\Core\Block\BlockManager object.
*
......@@ -33,12 +41,15 @@ class BlockManager extends DefaultPluginManager implements BlockManagerInterface
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
* @param \Psr\Log\LoggerInterface $logger
* The logger.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, LoggerInterface $logger) {
parent::__construct('Plugin/Block', $namespaces, $module_handler, 'Drupal\Core\Block\BlockPluginInterface', 'Drupal\Core\Block\Annotation\Block');
$this->alterInfo($this->getType());
$this->setCacheBackend($cache_backend, 'block_plugins');
$this->logger = $logger;
}
/**
......@@ -74,4 +85,12 @@ public function getFallbackPluginId($plugin_id, array $configuration = []) {
return 'broken';
}
/**
* {@inheritdoc}
*/
protected function handlePluginNotFound($plugin_id, array $configuration) {
$this->logger->warning('The "%plugin_id" was not found', ['%plugin_id' => $plugin_id]);
return parent::handlePluginNotFound($plugin_id, $configuration);
}
}
......@@ -4,9 +4,11 @@
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Core\Block\BlockManager;
use Drupal\Core\Block\Plugin\Block\Broken;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Tests\UnitTestCase;
use Psr\Log\LoggerInterface;
/**
* @coversDefaultClass \Drupal\Core\Block\BlockManager
......@@ -22,6 +24,13 @@ class BlockManagerTest extends UnitTestCase {
*/
protected $blockManager;
/**
* The logger.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* {@inheritdoc}
*/
......@@ -30,7 +39,8 @@ protected function setUp() {
$cache_backend = $this->prophesize(CacheBackendInterface::class);
$module_handler = $this->prophesize(ModuleHandlerInterface::class);
$this->blockManager = new BlockManager(new \ArrayObject(), $cache_backend->reveal(), $module_handler->reveal());
$this->logger = $this->prophesize(LoggerInterface::class);
$this->blockManager = new BlockManager(new \ArrayObject(), $cache_backend->reveal(), $module_handler->reveal(), $this->logger->reveal());
$this->blockManager->setStringTranslation($this->getStringTranslationStub());
$discovery = $this->prophesize(DiscoveryInterface::class);
......@@ -40,6 +50,8 @@ protected function setUp() {
'broken' => [
'admin_label' => 'Broken/Missing',
'category' => 'Block',
'class' => Broken::class,
'provider' => 'core',
],
'block1' => [
'admin_label' => 'Coconut',
......@@ -86,4 +98,13 @@ public function testGroupedDefinitions() {
$this->assertSame(['block3', 'block1'], array_keys($definitions['Group 2']));
}
/**
* @covers ::handlePluginNotFound
*/
public function testHandlePluginNotFound() {
$this->logger->warning('The "%plugin_id" was not found', ['%plugin_id' => 'invalid'])->shouldBeCalled();
$plugin = $this->blockManager->createInstance('invalid');
$this->assertSame('broken', $plugin->getPluginId());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment