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


The cyber world is constantly changing and making progress; therefore, many humans are left behind, because technology does not wait to be discovered by them so that they can make technology useful in their lives. Therefore, people are left behind, because they are not using their most important tool to learn new things and grow, most people don’t use their brain.

In today’s article we are going to talk a bit more about Drupal 8, specifically Plugins and Plugin Discovery Methods. The plugin discover can be used by the plugin manager, to locate instances of individual plugins within the code base. The plugin discovery can also aid the plugin manager, to know where to look and how to interpret the data it finds.

The meta data of all plugins of a specific type, can be located with the process provided by the Plugin discovery. Let’s say you want to build out a module, which provides custom blocks to users, which they can place on their website. Then you can use the BlockPluginManager method to provide such functionality to your module.

A pattern that any custom module can follow, to place PHP code that would make use of the block plugin, and find data about the block plugin, is defined by the  BlockPluginManager method. The following piece of code which is a class \Drupal\Component\Plugin\Discovery\DiscoveryInterface, implements all discovery mechanism, and  getDefinitions(), getDefinition() and, hasDefinition($plugin_id) methods are also provided by the code above.

Overriding the DefaultPluginManager::getDiscovery() method will allow a plugin manager to define, which method should be used. The most commonly used methods are annotation and YAML; therefore, a developer should default to annotation methods, when determining which method to use. When creating a new plugin manager, it is recommended that the \Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery class is used; in core this is the most commonly uses of the discovery component. The PSR-4 standard is used to discover plugins and provide the plugin manager with a file that contains the definition of the plugin discovered. The information provided to the plugin manager by PSR-4 is in the form of a class, and annotations in the @dockblock for the class. As a developer, you only have to provide a PHP sub-namespace and a class which would extend the  Drupal\Components\Annotations\Plugin class, if you wish to use annotated class discovery. The information that should be enter into the annotation for a plugin of this type should also be defined by the class used to extend the Drupal\Components\Annotations\Plugin class. A good a example of an annotation discovery can be seen in the code below, which looks for any class in the Drupal{module}\Plugin\Block namespace.

MyPluginManager::discovery = new AnnotatedClassDiscovery('Plugin/Block', $namespaces, 'Drupal\block\Annotation\Block');

The class also has an annotation which follows the following form, which is provided by the  Drupal\block\Annotation\Block. Class. The \Drupal\Core\Plugin\Discovery\YamlDiscovery class is used for YAML discovery, which uses a .yml file. The .yml file is usually located in the root directory of an enable module, to load a list of plugins available and their metadata. The definition of menu items, breakpoints, and contextual links, are usually the reason, why one would use YAML discovery. In the following code example, a list of plugins of the given type and their meta-data are being defined in the .yml file for the service.

MyPluginManager::discovery = new YamlDiscovery('sandwich', $module_handler->getModuleDirectories());

One or more plugin instances can be defined using a single .yml file, in this case; and individual plugin instances can reside in their own .yml files, by using the  \Drupal\Core\Plugin\Discovery\YamlDirectoryDiscovery  class.

Another type of discovery is done through hook discovery, which allow the gradual transition from Drupal 7 style info hooks to Drupal 8 annotated class discover. The hook discovery in Drupal 8 is provided by the \Drupal\Core\Plugin\Discovery\HookDiscovery class, and it follows the same pattern of Drupal 7’s info hook pattern.

All PHP function in hook discover follow a specific naming convention, and an associative array is expected to be returned. Plugins of a given type are found by the system, through the associative array returned by PHP functions. In the following code example, individual plugin instance can be found, from a list of block plugins, which is aggragated and returned when the code looks for any  {module name}_block_info() functions and calls them.

MyPluginManager::discovery = new HookDiscovery($this->moduleHandler, 'block_info');

Another plugin discovery method is the static discovery, which uses manual discovery of plugins and not the dynamic methods, we saw with the other three methods. The \Drupal\Component\Plugin\Discovery\StaticDiscovery class, provides the static discovery method. Manual registration of plugin instances, with the plugin manager, would have to be done, to implement a plugin of a given type. The code below shows us how you would implement static discovery: 

MyPluginManager::discovery = new StaticDiscovery();


Thank you for reading this article!!!


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.