Commit 75ee363b authored by alexpott's avatar alexpott
Browse files

Issue #1863816 followup by tim.plunkett: [Followup] Allow plugins to have services injected.

parent 2e638686
......@@ -4,31 +4,19 @@
* @file
* Contains \Drupal\Core\Plugin\ContainerFactoryPluginBase.
*/
namespace Drupal\Core\Plugin;
use Drupal\Component\Plugin\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a base plugin that can pull it's dependencies from the container.
* Defines a base plugin that can pull its dependencies from the container.
*/
class ContainerFactoryPluginBase extends PluginBase {
abstract class ContainerFactoryPluginBase extends PluginBase implements ContainerFactoryPluginInterface {
/**
* Creates an instance of the plugin.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The container to pull out services used in the plugin.
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
*
* @return static
* Returns an instance of this plugin.
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition);
......
<?php
/**
* @file
* Contains \Drupal\Core\Plugin\ContainerFactoryPluginInterface.
*/
namespace Drupal\Core\Plugin;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines an interface for pulling plugin dependencies from the container.
*/
interface ContainerFactoryPluginInterface {
/**
* Creates an instance of the plugin.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The container to pull out services used in the plugin.
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
*
* @return static
* Returns an instance of this plugin.
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition);
}
......@@ -19,7 +19,14 @@ class ContainerFactory extends DefaultFactory {
public function createInstance($plugin_id, array $configuration) {
$plugin_definition = $this->discovery->getDefinition($plugin_id);
$plugin_class = static::getPluginClass($plugin_id, $plugin_definition);
return $plugin_class::create(\Drupal::getContainer(), $configuration, $plugin_id, $plugin_definition);
// If the plugin provides a factory method, pass the container to it.
if (is_subclass_of($plugin_class, 'Drupal\Core\Plugin\ContainerFactoryPluginInterface')) {
return $plugin_class::create(\Drupal::getContainer(), $configuration, $plugin_id, $plugin_definition);
}
// Otherwise, create the plugin directly.
return new $plugin_class($configuration, $plugin_id, $plugin_definition);
}
}
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