This chapter is part of the Drupal Module Developer Guide.
Module developers use the Plugin API whenever they need to add to the list of things that the Drupal UI presents to a site builder, who is choosing which option fits their use case. What blocks can I put on the page? How should this field be formatted? What options are available for processing migrated data? This chapter defines what plugins are and how to discover and implement plugin types in a module.
Plugins enable developers to extend and customize functionality through a modular and reusable system. Plugins allow for the creation of interchangeable components that can be managed dynamically at runtime. This tutorial introduces the core concepts of Drupal's Plugin API, including how plugins, such as blocks, are defined, discovered, and used within the system.
In this tutorial, we'll:
- Define plugins in Drupal's context.
- Explain the Plugin system's operation, including types, managers, discovery, and factories.
- Discuss the role of plugins in extending Drupal.
By the end of this tutorial, you should have a high-level understanding of plugins and the Plugin API in Drupal.
Implementing plugins is a common task for Drupal developers. Often you'll need to add custom functionality using an existing plugin type--most likely an annotation-based plugin. This tutorial offers a structured approach to understanding and creating Drupal plugins, with a focus on annotation-based plugins.
In this tutorial, we'll:
- Examine the custom block code we previously wrote, this time from the perspective of the Plugin API.
- Introduce a recipe for creating annotation-based Drupal plugins.
- Demonstrate how to find the necessary information for new plugin creation from existing plugins.
By the end of this tutorial, you will know how to examine the structure of existing plugin classes, so that you know how to implement a plugin of that type.
Implementing plugins often involves accessing Drupal services to use their functions. Since plugins are PHP classes, we can use dependency injection to access services in our class. This tutorial demonstrates injecting dependencies into plugin classes. We'll update the HelloWorldBlock
class to use the current_user
service through dependency injection.
In this tutorial, we'll:
- Learn how to inject dependencies into plugin classes.
- Update the
HelloWorldBlock
class to use dependency injection for accessing thecurrent_user
service.
By the end of this tutorial, you should understand how to use dependency injection within plugin classes.