Drupal 8 Plugin System

D
  1. Drupal 8 Configuration Management
  2. Paragraphs Module
  3. Building Configuration Forms
  4. Drupal 8 Migration
  5. The New Block System in Drupal 8
  6. Services and Dependency Injection
  7. Cron Queuing in Drupal 8
  8. Drupal 8 Plugin System
  9. Attaching JS and CSS Assets in Drupal 8
  10. Configuring your Local Drupal 8 Development Environment
  11. Creating a Custom Content Entity Type
  12. Introduction to Drupal 8 Theming
  13. Logging in Drupal 8
  14. Dependency Injection / Service Container Example

There is a lot of new terminology in Drupal 8 and for anyone not familiar with Object Oriented programming, some of it can be downright confusing. For myself, I have worked with OOP for quite a while, however I have not spent as much time writing custom modules for Drupal 7. While I am familiar on the surface with the hook system and API at a very novice level, I have never really had to do any intense back end development in D7. Now that I am working in Drupal 8, things are quite different with my projects at work. Thankfully my OOP background has filled in some of the gaps, however my lack of Drupal 7 module development knowledge has proven to be a burden.

In Day 8 we are looking at the Plugin System and I have to be honest I was a little bit confused when I started looking at plugins. Part of the reason for this was down to my lack of knowledge of Drupal 7 development concepts, most notably, hook_info. In addition to this, I had just gotten my head around the concept of services in Drupal 8 and found a blur between what I envisioned to be a service vs what could be considered a plugin. Thankfully I was able to find some answers.

Plugins vs Services

The best description I could find of a plugin with Drupal 8 was found in the Drupal.org Why Plugins? article. The author describes a plugin as something that would typically be exposed via a user interface which users can then configure, interact with, or select which implementation of functionality they desire. This contrasts a service which is ‘intended to be something that modules do programmatically to other modules.’ The Lullabot article, Creating a Custom Filter in Drupal 8 provides a nice analogy for a plugin manager to that that of an ice cream shop and the plugins themselves, ice cream, flavors, accounting systems for the business, etc.

If you’re like me and are not as well versed in Drupal 7 module development, you might not have realized that the new Drupal 8 plugin system is essentially a replacement for hook_info (a handy hook that allowed for organizing of other hooks as well as separating their logic out of the .module file which could become large and unmanageable for more sophisticated modules. In addition to the above, unlike services, plugins are setup to be ‘discoverable’ by Drupal via annotations. The Talking Drupal podcast illustrates this well in their Intermediate Module Development Concepts episode. When researching plugins, I found a really good video from DrupalCon Barcelona by Kris Vanderwater, who walks through setting up a basic example plugin called plugin_message.

As of writing, in Drupal 8.2, there are currently sixteen plugin managers in Drupal 8, each of which are responsible for providing access to various plugins within their grouping that can be used and extended in your own Drupal project. You can also write your own plugin managers and plugins by extending DefaultPluginManager and Plugin respectively.

plugin.manager.actionDrupal\Core\Action\ActionManager
plugin.manager.archiverDrupal\Core\Archiver\ArchiverManager
plugin.manager.blockDrupal\Core\Block\BlockManager
plugin.manager.conditionDrupal\Core\Condition\ConditionManager
plugin.manager.display_variantDrupal\Core\Display\VariantManager
plugin.manager.element_infoDrupal\Core\Render\ElementInfoManager
plugin.manager.entity_reference_selectionDrupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager
plugin.manager.field.field_typeDrupal\Core\Field\FieldTypePluginManager
plugin.manager.field.formatterDrupal\Core\Field\FormatterPluginManager
plugin.manager.field.widgetDrupal\Core\Field\WidgetPluginManager
plugin.manager.mailDrupal\Core\Mail\MailManager
plugin.manager.menu.contextual_linkDrupal\Core\Menu\ContextualLinkManager
plugin.manager.menu.linkDrupal\Core\Menu\MenuLinkManager
plugin.manager.menu.local_actionDrupal\Core\Menu\LocalActionManager
plugin.manager.menu.local_taskDrupal\Core\Menu\LocalTaskManager
plugin.manager.queue_workerDrupal\Core\Queue\QueueWorkerManager

Source: core.services.yml

About the author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

About Author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

Follow Me