Drupal 8 is a Great Way of Developing a Useful and Valuable Website - Part Twenty

feature-top

Wouldn’t it be a great thing to do, to go into any store and not know what you want but somehow you walk out of that store with the items that you need? Well in programming developers can accomplish just that by using logic and implementing their code correctly. In the beginning, programmers don’t really know how they are going to solve their problems, or what tools and resources will they use to solve their problems, but at the end of their tasks, somehow, they are able to solve their problems, using the correct tools and resources, available online.

In today’s article, we are going to talk about Mappers, another Drupal 8 concept that is worthwhile learning. Plugin managers use a unique ID to locate a plugin instance or mappers can be used to locate that plugin instance as well, based on application context.

When a plugin is needed by a piece of code that is calling it, but does not know which one it should call, then mappers can help plugin managers accomplish this task, by providing the ::getInstance() method; which is based on additional logic, it returns the correct plugin. The mail manager, which is responsible for sending email for Drupal, is a good example of mappers in action.

The plugin mail manager allows for the possibility of many different ways of sending an email using PHP. Therefore, the manager looks up the application’s configuration to find out which mail system it should use, and an instance of that plugin is returned ready to be used when the ::getInstance() method is called. The presence of a ::getInstance() method is expected, because the PluginManagerInterface extends the MapperInterface.

Alright, now we can shift our conversation and talk about Plugin Derivatives, which are part of the plugin discovery mechanism. A single plugin can act in the place of many, by employing the plugin derivative; which is used when other plugins may be impacted by user data or another dynamic configuration.

When plugin definitions need to be dynamically generated, the Plugin derivative can help developers complete this task. The plugin derivatives allow for multiple plugins of a given type, to be used, based on the application state or configuration.

The \Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator decorator class is used to accomplish this task. Derivatives are allowed by default, with new plugin types, which are based on the DefaultPluginManager. The way that derivatives work is that there are two requirements, the base plugin, and a deriver class.

Variations of the base plugin are dynamically registered with the plugin manager by the driver, using the base plugin as the template. If you would like to use injection in your deriver class, then you would implement the deriver class with \Drupal\Component\Plugin\Derivative\DeriverInterface or \Drupal\Core\Plugin\Discovery\ContainerDeriverInterface.

 

Thank you for reading this article!!!

feature-top
feature-top

Add a Comment

Hernando Cadet

Hi every one, I obtained a bachelor's degree in Bioinformatics back in 2006, from Claflin University, after I received my bachelor's degree, I gained full time employment as a software engineer at a Video Relay Service company, maintaining databases and developing software for a new developed device called the VPAD.

I worked at that company for two years, then I became a web developer, and worked for a magazine for three years. After that job, I worked as a Drupal web developer, as a subcontractor for the NIH, for a year and then left the job to go back to school.

Hernando Cadet

Collaboratively administrate empowered markets via plug-and-play networks. Dynamically procrastinate B2C users after installed base benefits. Dramatically visualize customer directed convergence without

Collaboratively administrate empowered markets via plug-and-play networks. Dynamically procrastinate B2C users after installed base benefits. Dramatically visualize customer directed convergence without revolutionary ROI.