diff --git a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php index 7a1cdac3b32c..b7b0557a7dce 100644 --- a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php +++ b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php @@ -8,7 +8,7 @@ namespace Drupal\Core\Plugin; use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface; -use Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator; +use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator; use Drupal\Component\Plugin\PluginManagerBase; use Drupal\Component\Plugin\PluginManagerInterface; use Drupal\Component\Utility\NestedArray; @@ -98,7 +98,7 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt public function __construct($subdir, \Traversable $namespaces, $annotation_namespaces = array(), $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') { $this->subdir = $subdir; $this->discovery = new AnnotatedClassDiscovery($subdir, $namespaces, $annotation_namespaces, $plugin_definition_annotation_name); - $this->discovery = new DerivativeDiscoveryDecorator($this->discovery); + $this->discovery = new ContainerDerivativeDiscoveryDecorator($this->discovery); $this->factory = new ContainerFactory($this); } diff --git a/core/lib/Drupal/Core/Plugin/Discovery/ContainerDerivativeDiscoveryDecorator.php b/core/lib/Drupal/Core/Plugin/Discovery/ContainerDerivativeDiscoveryDecorator.php new file mode 100644 index 000000000000..fcae2e3f1438 --- /dev/null +++ b/core/lib/Drupal/Core/Plugin/Discovery/ContainerDerivativeDiscoveryDecorator.php @@ -0,0 +1,35 @@ +derivativeFetchers[$base_plugin_id])) { + $this->derivativeFetchers[$base_plugin_id] = FALSE; + if (isset($base_definition['derivative'])) { + $class = $base_definition['derivative']; + // If the derivative class provides a factory method, pass the container + // to it. + if (is_subclass_of($class, 'Drupal\Core\Plugin\Discovery\ContainerDerivativeInterface')) { + $this->derivativeFetchers[$base_plugin_id] = $class::create(\Drupal::getContainer(), $base_plugin_id); + } + else { + $this->derivativeFetchers[$base_plugin_id] = new $class($base_plugin_id); + } + } + } + return $this->derivativeFetchers[$base_plugin_id] ?: NULL; + } + +} diff --git a/core/lib/Drupal/Core/Plugin/Discovery/ContainerDerivativeInterface.php b/core/lib/Drupal/Core/Plugin/Discovery/ContainerDerivativeInterface.php new file mode 100644 index 000000000000..6ba68a68f0ec --- /dev/null +++ b/core/lib/Drupal/Core/Plugin/Discovery/ContainerDerivativeInterface.php @@ -0,0 +1,30 @@ +get('plugin.manager.entity')->getStorageController('view') + ); + } + + /** + * Constructs a ViewsBlock object. + * + * @param string $base_plugin_id + * The base plugin ID. + * @param \Drupal\Core\Entity\EntityStorageControllerInterface $view_storage_controller + * The entity storage controller to load views. + */ + public function __construct($base_plugin_id, EntityStorageControllerInterface $view_storage_controller) { + $this->basePluginId = $base_plugin_id; + $this->viewStorageController = $view_storage_controller; + } + /** * Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinition(). */ @@ -39,7 +78,7 @@ class ViewsBlock implements DerivativeInterface { */ public function getDerivativeDefinitions(array $base_plugin_definition) { // Check all Views for block displays. - foreach (views_get_all_views() as $view) { + foreach ($this->viewStorageController->loadMultiple() as $view) { // Do not return results for disabled views. if (!$view->status()) { continue; diff --git a/core/tests/Drupal/Tests/Core/Plugin/Discovery/ContainerDerivativeDiscoveryDecoratorTest.php b/core/tests/Drupal/Tests/Core/Plugin/Discovery/ContainerDerivativeDiscoveryDecoratorTest.php new file mode 100644 index 000000000000..5a53a6945699 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Plugin/Discovery/ContainerDerivativeDiscoveryDecoratorTest.php @@ -0,0 +1,68 @@ + 'Container aware derivative discovery decorator.', + 'description' => 'Tests the container aware derivative discovery decorator.', + 'group' => 'Plugin', + ); + } + + /** + * Tests the getDerivativeFetcher method. + * + * @see \Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator::getDerivativeFetcher(). + */ + public function testGetDerivativeFetcher() { + $example_service = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $example_container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder') + ->setMethods(array('get')) + ->getMock(); + $example_container->expects($this->once()) + ->method('get') + ->with($this->equalTo('example_service')) + ->will($this->returnValue($example_service)); + + \Drupal::setContainer($example_container); + + $definitions = array(); + $definitions['container_aware_discovery'] = array( + 'id' => 'container_aware_discovery', + 'derivative' => '\Drupal\Tests\Core\Plugin\Discovery\TestContainerDerivativeDiscovery', + ); + $definitions['non_container_aware_discovery'] = array( + 'id' => 'non_container_aware_discovery', + 'derivative' => '\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscovery', + ); + + $discovery_main = $this->getMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface'); + $discovery_main->expects($this->any()) + ->method('getDefinitions') + ->will($this->returnValue($definitions)); + + $discovery = new ContainerDerivativeDiscoveryDecorator($discovery_main); + $definitions = $discovery->getDefinitions(); + + // Ensure that both the instances from container and non-container test derivatives got added. + $this->assertEquals(4, count($definitions)); + } + +} diff --git a/core/tests/Drupal/Tests/Core/Plugin/Discovery/TestContainerDerivativeDiscovery.php b/core/tests/Drupal/Tests/Core/Plugin/Discovery/TestContainerDerivativeDiscovery.php new file mode 100644 index 000000000000..88bebf2142dd --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Plugin/Discovery/TestContainerDerivativeDiscovery.php @@ -0,0 +1,35 @@ +get('example_service')); + } + +} diff --git a/core/tests/Drupal/Tests/Core/Plugin/Discovery/TestDerivativeDiscovery.php b/core/tests/Drupal/Tests/Core/Plugin/Discovery/TestDerivativeDiscovery.php new file mode 100644 index 000000000000..d0b8218f47ef --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Plugin/Discovery/TestDerivativeDiscovery.php @@ -0,0 +1,36 @@ +getDerivativeDefinitions($base_plugin_definition); + return $definitions[$derivative_id]; + } + + /** + * {@inheritdoc} + */ + public function getDerivativeDefinitions(array $base_plugin_definition) { + $plugins = array(); + for ($i = 0; $i < 2; $i++) { + $plugins['test_discovery_' . $i] = $base_plugin_definition; + } + return $plugins; + } + +}