We often use some prebuild structure as a starting point of our project/module and most of the Framework/CMS are now providing their CLI tools which automatically generate boilerplate templates by using some commands, Like ionic generate in ionic framework, php artisan create in Laravel and drupal generate in drupal console.
The basic idea behind those commands is to provide an interface, from which a developer can start writing solutions for a specific problem. Drupal plugin has a similar concept, the Base module defines plugin interface and some utility code. and other modules start by implementing that interface. However, we can define plugins in lots of other ways.
- It’s a new way in Drupal 8 to develop extensible modules.
- It’s a general reusable solution for a recurring problem.
- It provides a way to implement alterable functionality in modules.
Drupal documentation defines plugin as below.
The basic idea of plugins is to allow a particular module or subsystem of Drupal to provide functionality in an extensible, object-oriented way. The controlling module or subsystem defines the basic framework (interface) for the functionality, and other modules can create plugins (implementing the interface) with particular behaviors. The controlling module instantiates existing plugins as needed, and calls methods to invoke their functionality. Examples of functionality in Drupal Core that use plugins include: the block system (block types are plugins), the entity/field system (entity types, field types, field formatters, and field widgets are plugins), the image manipulation system (image effects and image toolkits are plugins), and the search system (search page types are plugins).
Plugins are grouped into plugin types, each generally defined by an interface. Each plugin type is managed by a plugin manager service, which uses a plugin discovery method to discover provided plugins of that type and instantiate them using a plugin factory.
Okay, let’s move forward and implement our custom plugin type.
We will do this in 3 part
- Annotation — Define annotation for the plugin type
- Plugin Manager — Define plugin manager which is…