The Drupal plugin system allows a particular module or subsystem to provide functionality in an extensible, object-oriented way. The controlling module defines the basic framework (interface) for the functionality, and other modules can create plugins (implementing the interface) with particular behaviors. Plugins are grouped into plugin types. 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.
Plugins are one of the ways that module developers can write code that extends Drupal. The Drupal Plugin API allows a module to provide functionality in an extensible, object-oriented way. Plugins are used to solve the problem where you need to allow one or more modules to provide additional "things" that implement the same interface but provide distinctly different functionality. In most cases, you also want to provide a site administrator with a list of those "things" so she can choose and configure the appropriate functionality for her use case.
Plugins are grouped into plugin types which are managed by a plugin manager service. As a module developer, knowing how to implement an existing plugin type should be considered required knowledge.
Many frameworks use the term "plugin" to describe a bundle of code and other elements that can be installed in order to get access to the features they provide. In Drupal these are called modules, and plugins are one of the ways a module can add new features.
Example tasks
- Define a custom block in a module
- Add a new field type, widget, or formatter
- Create a custom content entity type
- Define a new plugin type in order to allow other modules to extend your module's capabilities in the same way Drupal core is extended
- Define custom Views plugin
Confidence
Plugins were introduced in Drupal 8. The process for implementing plugins is relatively unchanged since before Drupal 8 was released, so older examples are likely to still be relevant. Resources labeled for Drupal 8 should still apply to Drupal 9.
Drupalize.Me resources
Views plugins
External resources
- Plugin API Guide (drupal.org)
- Community documentation for the Plugin API. Contains lots of useful information which can be somewhat hard to follow without a defined order. Use this when you have specific questions about plugin implementation, but you already know the basics.
- Plugin API Documentation (api.drupal.org)
- Developer-centric documentation for the Plugin API. Use this as a quick reference for reminders on how to define a new Plugin or Plugin Type. Or if you want a deep-dive into the code that makes up the Plugin API.
- Examples for Developers project (drupal.org)
- Contains the plugin_type_example module which demonstrates how to create a custom plugin type. This also teaches you a lot about how the Plugin API works. This example code was reviewed, updated, and had tests added by Drupalize.Me while creating our own content on the Plugin API. Use this if you want something you can copy/paste as a starting point, or if you learn best from reading code.
- DrupalCon Los Angeles 2015: An Overview of the Drupal 8 Plugin System (youtube.com)
- Recording of a presentation by Joe Shindelar covering the Plugin API. A good overview of the entire Plugin API, useful for learning some basic terminology, information about why the Plugin API is used, and an overview of how all the parts fit together.
- Drupal 8 API: plugins (jpstacey.info)
- Written overview of the Plugin API with examples of adding a new plugin type and implementing plugins of an existing type. J-P provides a good overview of the inner workings of the Plugin API, as well as providing information about Annotations and OOP conventions. This is one of the best free written tutorials on Plugins we're aware of.