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.
Drupal's Entity API enables us to define custom content entity types. It provides a structured approach to store custom data. Creating a custom entity makes sense when built-in entity types like nodes or taxonomy terms don't meet the specific requirements of a project. Custom entities allow for custom data structures, and use Drupal's core features such as access control, Views integration, and JSON:API support. Using the Entity API to create custom content entities ensures your custom data will be compatible with other Drupal modules.
In this tutorial, we'll:
- Discuss use cases for custom content entities.
- Get a high-level overview of defining a custom entity type.
- Provide additional resources where you can learn more about defining custom entity types.
By the end of this tutorial, you'll understand the use case for custom content entities and how to begin defining one.
To implement a custom vendor attendance status feature, we need to add new fields to the Vendor content type. This tutorial will guide you through adding these fields and discuss the considerations for choosing between Drupal's UI for data modeling versus code-based alterations of entity types.
In this tutorial, we'll:
- Add new fields to the Vendor content type required for the vendor attendance status feature.
- Discuss the pros and cons of modeling data with Drupal's Field UI compared to using hooks for modifying entity base fields.
By the end of this tutorial, you'll know how to update the Vendor content type with necessary fields and understand why this approach suits our specific case.
This guide was written, and is maintained, by Drupalize.Me. For more high quality written and video Drupal tutorials created by our team of experts, check out the collection of Drupalize.Me Guides.
Drupal's Form API (FAPI) is a comprehensive framework for managing forms within Drupal. The Form API extends the Render API, providing a structured approach to form creation, validation, and submission. It offers an abstraction layer over HTML forms and HTTP form handling, simplifying the process of capturing and processing user input securely and efficiently. Forms are integral to content management systems like Drupal, enabling user interactions ranging from content creation to configuration settings. For module developers, using the Form API is essential for building interactive and dynamic websites.
In this tutorial, we'll:
- Discuss the relationship between the Form API and the Render API.
- Highlight the significance of forms in Drupal and the role of the Form API in managing them.
- Outline the life cycle of a Drupal form, from definition to processing, including the role of form controllers.
By the end of this tutorial, you should grasp the fundamentals of the Form API and be prepared to construct and manage forms in Drupal modules.
One of the most powerful features of Drupal's Form API is the ability to alter nearly any aspect of the build, validate, or submit workflow in your custom code. Implementing hook_form_alter
is a common task for Drupal module developers, allowing them to modify forms to customize administrative or user-facing interfaces.
In this tutorial, we'll:
- Explore the purpose and use case for
hook_form_alter
and related hooks. - Learn how to target specific forms.
- Discover how to identify the form you wish to alter.
By the end of this tutorial, you should be able to select and implement the appropriate form alter hook to modify any form in Drupal.
Drupal's Menu system allows module developers to define navigational links, offering flexibility to site builders for configuration and arrangement. By linking to routes rather than URLs, these links remain functional, even when route definitions change.
In this tutorial, we'll:
- Create a MODULE_NAME.links.menu.yml file.
- Define a new menu item link for the /weather page in our anytown module.
- Configure Drupal to display the link in the primary navigation.
By the end of this tutorial, you'll be able to define new menu item links and integrate them into your site's configuration.
The submitForm()
method in a form controller is responsible for handling submitted data. This method can save data to the database (including updating configuration), trigger workflows based on user input, and redirect users after form processing. By the time data reaches submitForm()
, it has been validated and is ready for use.
In this tutorial, we'll:
- Add a
submitForm()
method to the form controller. - Save user-provided settings to the database.
- Confirm successful data submission to the user.
By the end of this tutorial, you'll understand how to implement custom submit handling logic in a form.
Hooks in Drupal enable modules to alter or extend the behavior of Drupal core or other modules. By implementing functions with specific names, modules can intervene at various points in Drupal's execution flow. This tutorial introduces hooks, their implementation, and their significance in module interaction.
In this tutorial, we'll:
- Define what hooks are.
- Explore how hooks are implemented in modules.
- Understand when modules should define and invoke new hooks.
By the end of this tutorial, you'll understand the concept of hooks as a means to alter Drupal's behavior.
Controllers are where you place the custom logic to dynamically generate the content of a page that a visitor sees when they visit a URL. When Drupal receives an incoming request, the HttpKernel identifies the appropriate route for the requested path, and the routing system matches this route with a controller. Controllers generate responses to these requests.
In this tutorial, we'll:
- Understand what a controller is in PHP and in the context of a Drupal module.
- Learn how to identify and interpret the role of a controller class.
By the end of this tutorial, you should be able to identify a controller class in a Drupal module and understand its role in generating responses.
Concept: Routes
FreeAs a module developer, you use routes defined in a module to add new URLs and tell Drupal which code to execute to build content for the page at those URLs. Central to this process is Drupal's routing system, built upon Symfony's Routing component.
In this tutorial, we'll:
- Introduce Drupal's routing system.
- Learn how modules can define new routes.
- Describe the roles that routes serve in a module.
By the end of this tutorial, you should understand each parameter of a route definition in a module's MODULE_NAME.routing.yml file.
As a development framework, Drupal core includes services for doing common things like accessing the database, sending email, or making HTTP requests. You can make use of these services to perform certain tasks, instead of recreating them in your own code. This means less custom code that you need to write and maintain. Generally speaking, almost everything that Drupal does is actually done by one of these objects. In Drupal, these objects are called services and in order to make working with them easier, they are primarily accessed through the service container.
In this tutorial, we'll:
- Explain the role of services in Drupal.
- Introduce the service container.
By the end of this tutorial, you'll understand the basics concepts of services and the service container in Drupal module development.
To follow along with the examples in this guide you'll need to have a copy of the Anytown Farmer's Market site that resembles what you would have if you went through all the chapters in the Drupal User Guide. If you did not follow along with the Drupal User Guide example, or if you need a fresh start, this tutorial will walk you through setting up a copy of the site based on a backup. This guide follows a project-based learning approach, and this setup is essential for ensuring that you can effectively follow along with the subsequent tutorials.
In this tutorial, we'll:
- Locate the codebase, backup database, and backup public files directory, required to restore a copy of the Anytown Farmer's Market site.
- Walk through setting up the site in DDEV using the provided backup.
By the end of this tutorial, you should have a working copy of the Anytown Farmer's Market site on your local development environment.
Drupal uses PSR-4 namespaces to autoload the correct PHP class from a file, accommodating variations in site structures. As a module developer, it's important to understand PSR-4, as it dictates the location within your module directory for most of your custom code.
In this tutorial, we'll:
- Define the PSR-4 namespace standard.
- Explore its use in Drupal.
- Learn to read a namespace and locate the corresponding PHP file.
By the end of this tutorial, you should be able to recognize a PSR-4 namespace, determine the related fully-qualified class name, and the location of the corresponding PHP file.
A Drupal module encapsulates files and directories that serve a specific purpose and follow Drupal's standards and conventions. This tutorial describes the anatomy of a Drupal module, focusing on the placement and purpose of different file types.
In this tutorial, we'll:
- Explain where Drupal looks for modules and where you should place your custom module.
- Describe the standard file and directory types in a module.
By the end of this tutorial, you should be able to identify and understand the purpose of various files and directories within a Drupal module and know where to correctly place them.
To use a hook in a module, you'll need to know how to find the API documentation for the hook. The API documentation for a hook describes the hook's purpose, provides the function signature, and defines each of its parameters.
In this tutorial, we'll:
- Explore methods for listing available hooks.
- Guide you to the documentation for specific hooks.
By the end of this tutorial, you'll be able at find possible hooks for your task, and understand the documentation for a hook.
Template files are used by modules when they need to add custom HTML to the content they output. The most common example is wrapping your output in one or more <div>
tags to give it additional structure and context. Using custom template files in a Drupal module requires defining a new theme hook, creating the template file, an associating the appropriate data with the template file via a render array.
In this tutorial, we'll:
- Learn how to add a custom Twig template file to a module.
- Update the
WeatherPage
controller to use the new template file.
By the end of this tutorial you should be able to create and use custom template files in a module.
The event system in Drupal enables different components to interact and communicate. Through events, a component can announce important actions to interested modules, providing a flexible way to extend functionality. This system is central to Drupal's event-driven architecture.
In this tutorial, we'll:
- Define events and their operation.
- Provide examples of dispatching and subscribing to events.
- Contrast events with hooks.
By the end of this tutorial you should be able to understand the event system's fundamentals in Drupal, and how modules can use it for extending and modifying functionality.
Modules enable developers to customize Drupal without modifying the core software. To ensure a stable and upgradeable core system, Drupal defines integration points and patterns that developers can use to customize the system. Modules contain code (PHP, JavaScript, Twig, CSS, YAML, etc.) that can extend, alter, and enhance Drupal's features and functionality.
In this tutorial, you'll learn:
- What Drupal modules are and their role in Drupal architecture
- How modules interact with Drupal core to extend or alter site functionality
- About the types of modules: core, contributed, and custom
By the end of this tutorial, you should be able to explain what modules are in Drupal, and understand their role in defining a Drupal site's functionality.
A development environment is any copy of your site that operates separately from the live site. Development environments allow you to make changes, try new modules, write new code, and test new ideas on a copy of your site instead of the real thing. Using a development environment ensures any changes you make happen in a sandbox where you’re not affecting anyone trying to use the live version of your application. As a module developer, it's common practice to set up a copy of your Drupal application on your own computer for development and testing purposes.
In this tutorial we'll learn:
- What a development environment is and why it's needed
- The specific requirements for Drupal, and a recommendation (DDEV) for those seeking a straightforward solution
- Other commonly-used extras that enhance the development experience
By the end of this tutorial you should be able to describe the requirements of a Drupal development environment and know how to get started setting one up for yourself.