Drupal 8 is a Great Way of Developing a Useful and Valuable Website – Part Nineteen

Drupal 9 and 10

Sharing is caring!

A world where things are available for you to use free of charge, is an ideal world, however if you don’t know how to use these tools to advance your purpose, you can be stuck at the same place for years, until you get up and go learn what it is that you need to learn; to be able to use the tools that you need, in order for you to accomplish your goals.

In today’s article, we are going to talk about Plugins in Drupal 8, we will focus first on Plugin Factories and Mapper and then we’ll talk about other aspects of the Plugin API. When a developer wants to instantiate a plugin and don’t know anything about how the plugin is handled, they can accomplish this by using the plugin factory.

Useful implementations of the FactoryInterface are provided by Drupal core, so that it can be used to instantiate plugin instances, through custom plugin managers. The MyPluginManager::createInstance()method is provided to the plugin manager. A plugin instance can be instantiated with this method because it proxies the request to the selected factory class which instantiate and return the specified plugin instance.

The \Drupal\Component\Plugin\Factory\FactoryInterface is implemented by the plugin factories. The most base factory in Drupal core is the default factory, which is provided by the Drupal\Component\Plugin\Factory\DefaultFactory class.

Questions such as What arguments should be passed to a class and which one should be instantiated? Are all answered by the default factory. By the way, this is not the default factory that Drupal uses, we’ll talk about the container factory next which is the one that Drupal uses as its default factory.

Once a plugin has been located and provided to the plugin manager, then the plugin factory maps that plugin ID to a class, then that class is returned after it has been instantiated with common arguments.

The definition of the plugin which is returned by the discovery mechanism, the ID of the plugin being instantiated and any additional information about configuration passed to the createInstance() method when it is called; are some of the arguments passed by the default factory.

An example code of this is: Note: All the code on this article is provided by drupilize.me

// Arguments passed to __construct() method:
// - $configuration (array): Passed to the createInstance() method from caller.
// - $plugin_id (string): Unique ID of the plugin being instantiated.
// - $plugin_definition (array): Plugin meta-data read from the annotation.
return new HamSandwich($configuration, $plugin_id, $plugin_definition);

The recommended factory for most use cases regardless of what the name may imply is known as the container factory. The


class can do the same thing as the


class; however, it has one major addition, once it has extended the DefaultFactory class.  The ContainerFactory class performs constructor injection, and by calling the:: create() method, it can inject a copy of the Drupal services container for a module to use. If you want your custom plugin manager to have access to the services container, then you’ll want to use the ContainerFactory class. When a plugin does not use the container injection pattern or don’t implement the:: create() method, the container factory will be able to recognize that, and instead follows the Default DefaultFactory factory method to instantiate the plugin.

Whenever an instance of a plugin has the ability to decide, which factory it wants to use, then that factory method is the best choice, in this case, this is the  ContainerFactory. A good example code is listed below.

/ Arguments passed to create() method:
// - $container: A copy of the Drupal service container.
// - $configuration (array): Passed to the createInstance() method from caller.
// - $plugin_id (string): Unique ID of the plugin being instantiated.
// - $plugin_definition (array): Plugin meta-data read from the annotation.
return CheeseSandwich::create(\Drupal::getContainer(), $configuration, $plugin_id, $plugin_definition);

Another type of factory is the reflection factory, which is provided by the Drupal\Component\Plugin\Factory\ReflectionFactory class.

This factory is able to allow a variable or unknown set of arguments to be in the constructor of a plugin manager, which adds more flexibility to development efforts. Some introspection is done by this factory in the class it is about to instantiate; by analyzing the class. The list of arguments being passed to the constructor, based on the method signature to the constructor is also derived by this factory.

Thank you for reading this article!!!