diff --git a/plugin_type_example/plugin_type_example.module b/plugin_type_example/plugin_type_example.module index 5f9153b..e9c7ac5 100644 --- a/plugin_type_example/plugin_type_example.module +++ b/plugin_type_example/plugin_type_example.module @@ -32,7 +32,8 @@ * \Drupal\plugin_type_example\SandwichPluginManager::__construct. * * To see the plugins in action visit /examples/plugin_type_example. The output - * is rendered in Drupal\plugin_type_example\Controller\PluginTypeExampleController::description(). + * is rendered in + * Drupal\plugin_type_example\Controller\PluginTypeExampleController::description(). * Read through that method to see how to use plugins using the * plugin.manager.sandwich service. */ diff --git a/plugin_type_example/src/Controller/PluginTypeExampleController.php b/plugin_type_example/src/Controller/PluginTypeExampleController.php index d29360e..7f5b042 100644 --- a/plugin_type_example/src/Controller/PluginTypeExampleController.php +++ b/plugin_type_example/src/Controller/PluginTypeExampleController.php @@ -8,6 +8,8 @@ namespace Drupal\plugin_type_example\Controller; use Drupal\Core\Controller\ControllerBase; +use Drupal\plugin_type_example\SandwichPluginManager; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Controller for our example pages. @@ -15,7 +17,31 @@ use Drupal\Core\Controller\ControllerBase; class PluginTypeExampleController extends ControllerBase { /** - * Displays a page with an overview of our plugin type and plugins. + * The sandwich plugin manager used to get all of the sandwich plugins for + * use. + * + * @var \Drupal\plugin_type_example\SandwichPluginManager + */ + protected $sandwichManager; + + /** + * Constructs a \Drupal\plugin_type_example\PluginTypeExampleController + * object. + * + * @param \Drupal\plugin_type_example\SandwichPluginManager $sandwichManager + * The sandwich plugin manager service. + */ + public function __construct(SandwichPluginManager $sandwichManager) { + $this->sandwichManager = $sandwichManager; + } + + /** + * Displays a page with an overview of our plugin type and plugins. Lists all + * the Sandwich plugin definitions by using methods on the + * \Drupal\plugin_type_example\SandwichPluginManager class. Lists out the + * description for each plugin found by invoking methods defined on the + * plugins themselves. You can find the plugins we have defined in the + * \Drupal\plugin_type_example\Plugin\Sandwich namespace. */ public function description() { $build = array(); @@ -24,17 +50,11 @@ class PluginTypeExampleController extends ControllerBase { '#markup' => t('The plugin type example page.'), ); - // Get our plugin manager from the service container. - // The string we pass is the machine name of the service, which is set in - // the plugin_type_example.services.yml file. - // We get back an instance of our SandwichPluginManager class. - $manager = \Drupal::service('plugin.manager.sandwich'); - // Get the list of all the sandwich plugins defined on the system from the // plugin manager. // Note that at this point, what we have is *definitions* of plugins, not // the plugins themselves. - $sandwich_plugin_definitions = $manager->getDefinitions(); + $sandwich_plugin_definitions = $this->sandwichManager->getDefinitions(); // Let's output a list of the plugin definitions we now have. $items = array(); @@ -59,7 +79,7 @@ class PluginTypeExampleController extends ControllerBase { // If we want just a single plugin definition, we can use getDefinition(). // This requires us to know the ID of the plugin we want. This is set in the // annotation on the plugin class: see ExampleHamSandwich. - $ham_sandwich_plugin_definition = $manager->getDefinition('ham_sandwich'); + $ham_sandwich_plugin_definition = $this->sandwichManager->getDefinition('ham_sandwich'); // To get an actual plugin, we call createInstance() on the plugin manager, // passing the ID of the plugin we want to load. @@ -71,7 +91,7 @@ class PluginTypeExampleController extends ControllerBase { foreach ($sandwich_plugin_definitions as $plugin_id => $sandwich_plugin_definition) { // We now have a plugin! From here on it can be treated just as any other // object: have its properties examined, methods called, etc. - $plugin = $manager->createInstance($plugin_id, array('of' => 'configuration values')); + $plugin = $this->sandwichManager->createInstance($plugin_id, array('of' => 'configuration values')); $items[] = $plugin->description(); } @@ -84,4 +104,22 @@ class PluginTypeExampleController extends ControllerBase { return $build; } + /** + * Override the parent method so that we can inject out sandwich plugin + * manager service into the controller. This is dependancy injection at work + * for a controller. Rather than access the global service container via + * \Drupal::service(), it's best practice to use dependancy injection. Notice + * our controller extends ControllerBase, which implements the + * ContainerInjectionInterface interface. As such we can implement a create + * method to control how our controller in instantiated. We do just that, + * passing in the sandwich plugin manager service that we need. + */ + public static function create(ContainerInterface $container) { + // Use the service container to instantiate a new instance of our + // controller. The string we pass is the machine name of the service, + // which is set in the plugin_type_example.services.yml file. We get back + // an instance of our SandwichPluginManager class, which is passed to the + // controller's constructor. + return new static($container->get('plugin.manager.sandwich')); + } }