Plugin API in Drupal

Plugins in Drupal are reusable, interchangeable pieces of functionality defined by standardized interfaces. Knowing how to use plugins is crucial for developers to create modular, extendable components that enhance site functionality while promoting code reuse and maintainability.

This course covers the fundamental concepts of the Plugin API, including plugin types, discovery mechanisms, and how to implement and manage plugins. Learn to define new plugin types, use plugin managers, and understand the role of plugin factories and derivatives.

Key topics

  • What plugins are, their purpose, related terminology, and how they work
  • Commonly implemented plugin types
  • Discover existing available plugin types that can be implemented
  • Recipe for implementing any plugin type
  • Define new plugin types in a custom module
  • Plugin managers and plugin discovery mechanisms
  • Working plugin derivatives
Tutorials in this course
Categories
Drupal 8, 9, 10, and 11
More information

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.

The system aims to make it easy for developers to allow for management of these components via the user interface, giving more flexibility and control to site administrators.

In this tutorial we'll take a high-level look at the problem the Plugin API is solving and provide a starting point for diving deeper into the various components that make up the Plugin API.

Categories
Drupal 8, 9, 10, and 11
More information

The term plugin type is used as a way to provide a generic name for all plugins managed by the same plugin manager. Example plugin types include block, field widget, and image effect. A plugin type doesn't consist of any code that provides specific functionality. Instead, it provides a way to refer to the system and it describes the central purpose of all plugins of that type.

In this tutorial, we'll cover what the term plugin type means and how it is used when talking about Drupal's plugin system. By the end of this tutorial, you'll understand what a plugin type is and be ready to learn how to define a plugin type in a module.

Categories
Drupal 8, 9, 10, and 11
More information

Knowing how to define a new plugin type will allow you to write modules that are more extensible, and more configurable. In doing so you'll learn more about best practices in decoupling code within your module, and get an in-depth look at how the plugin system works. Even if you're not defining a new plugin type in your own module, understanding how the system works will give you more insight into how many parts of Drupal work. This is essential knowledge for anyone developing modules for Drupal.

By the end of this tutorial you should be able to determine if defining a new plugin type is the right solution to your problem, and then understand the steps involved in doing so.

Categories
Drupal 8, 9, 10, and 11
More information

Knowing which plugin types exist and the use case for each is important when it comes to writing modules that extend Drupal's existing functionality. If you want to add additional functionality, you need to first know which plugin type provides that functionality.

In this tutorial, we'll list some of the more commonly-used plugin types and their use case, show where you can find a more complete list of plugin types provided by core, and provide several methods for discovering existing plugins in your Drupal codebase.

Categories
Drupal 8, 9, 10, and 11
More information

Drupal uses plugins for a lot of different scenarios. Blocks, field types, menu items, breakpoints, and migrations are all examples of plugins. There are a couple of different types of plugins. Before you can create a new plugin of any type, you'll need to figure out where the plugin implementation and its metadata should live in the code base, so that the plugin manager can find plugins, and examine what functionality instances of the plugin type are expected to provide.

In this tutorial we'll:

  • Demonstrate how to figure out what type of plugin you're dealing with.
  • Point to additional resources showing how to implement the specific plugin type once it has been determined.

By the end of this tutorial you should have a better understanding of how to figure out the details required to implement any given plugin type.

More information

As of Drupal 10.2, most plugin types use PHP attributes for discovery and metadata.

In this tutorial, we'll:

  • Provide a recipe for implementing PHP attribute-based plugins.
  • Demonstrate how to figure out where the code and metadata should live for PHP attribute-based plugins.

By the end of this tutorial you should have a recipe for getting started with implementing PHP attribute-based plugins, and a better understanding of how to figure out the details required to implement a given plugin type.

More information

Prior to Drupal 10.2 most plugin implementations required the use of annotations alongside a PHP class. Now Drupal supports and recommends the use of PHP attributes instead. During the transition, most developers will still need to know how to recognize and implement annotation-based plugins.

In this tutorial we'll:

  • Provide a recipe for implementing annotation-based plugins.
  • Demonstrate how to figure out where the code and metadata should live for annotation-based plugins.

By the end of this tutorial you should have a recipe for getting started with implementing annotation-based plugins, and a better understanding of how to figure out the details required to implement a given plugin type.

Categories
Drupal 8, 9, 10, and 11
More information

Many of Drupal's APIs that look like a bunch of configuration in a YAML file (migrations, menu links, etc.) are actually plugins in disguise. The YAML from these files is used as arguments to a generic PHP plugin class which then behaves differently depending on the provided values. As a developer, you probably don't need to know that menu links are plugins, but it can be helpful when debugging or just trying to get a better understanding of the big picture.

In this tutorial, we'll:

  • Learn about how YAML-based plugins work
  • Discuss how to find the implementation details for YAML-based plugins
  • Walk through an example of implementing a YAML-based plugin

By the end of this tutorial, you should be able to recognize a YAML-based plugin definition, and author your own.

Categories
Drupal 8, 9, 10, and 11
More information

A plugin manager is responsible for both the definition of a new plugin type, and for listing and instantiating instances of plugins of the defined type.

In this tutorial we'll:

  • Define what a plugin manager is.
  • Create a list of all plugins of a specific type.
  • Load and instantiate specific plugin instances, so we can use them in our code.
Categories
Drupal 8, 9, 10, and 11
More information

In order for a plugin manager to locate instances of individual plugins within the Drupal code base it needs to know where to look, and how to interpret the data that it finds. This process is called plugin discovery and can be accomplished in several ways.

In this tutorial, we'll look at what plugin discovery is doing at a high level, and then talk about the plugin discovery methods you can choose from when defining a new plugin type.

Categories
Drupal 8, 9, 10, and 11
More information

Learn how the Plugin API takes a given plugin ID and uses it to instantiate and return a fully configured plugin object. In this tutorial we'll look at:

  • What factories are, and the role they serve in the Plugin API
  • The factories available in core
  • Using mappers to dynamically load a plugin when you don't know the ID of the specific plugin you need.
Categories
Drupal 8, 9, 10, and 11
More information

Plugin derivatives allow a single plugin to dynamically generate multiple plugin instances based on configuration or other data. This is useful for situations where user-entered data, or other dynamic configuration, might have an impact on available plugins. Or, put another way, any time you need to be able to dynamically generate plugin definitions.

In this tutorial we'll:

  • Define what plugin derivatives are
  • Understand the use case for derivatives
  • Examine how core uses derivatives in order to demonstrate how you could write your own plugin deriver class
This course appears in the following guides:
Module Development
Learn how to alter Drupal with hooks, plugins, services, and events APIs.

Alter Drupal in Modules