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 annotating class methods with #[Hook]
attributes, or 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.
To become an effective module developer, you'll need to know how to navigate and use Drupal's extensive documentation. In this tutorial, you'll learn about the types of documentation available to Drupal developers, including API references, community-contributed documents, and best practices for using these resources.
Drush, the Drupal Shell, is a utility for module developers and administrators. While it does ship with DDEV, it doesn't come standard with Drupal core and must be installed separately via Composer.
In this tutorial we'll:
- Install Drush with Composer
- Verify that Drush is working
By the end of this tutorial, you should have a working copy of the Drush utility installed in your development environment.
One of the primary ways that modules extend Drupal is by adding dynamic pages with content generated by application-specific logic. This tutorial looks at Drupal's process of handling a user's request and transforming it into a complete web page. We'll answer questions like:
- How does Drupal use Symfony's HttpKernel to process requests?
- How do user-configured blocks and the theme layer contribute to the page's content and appearance?
- What is the role of routes, controllers, and responses in this process?
In this tutorial, we'll:
- Follow the journey of a user request from URL to an HTML response in Drupal.
- Define the role of routes, controllers, and responses in Drupal's request handling.
- See how blocks and the theme layer contribute to the final output of a Drupal page.
By the end of this tutorial, you should be able to describe Drupal's request flow, understand the interaction of its key components in building a page, and appreciate the modular architecture that drives Drupal's flexibility.
Guiding Scenario
FreeWe'll be working with the Anytown Farmers Market site, which we built in the Drupal User Guide. Our goal is to extend and enhance this site using custom modules, to fulfill project-specific requirements. We use a narrative approach, which we hope will help you better understand the real-world applications of module development in Drupal.
There are a number of tools that help make your Drupal development experience enjoyable and efficient. They aren't required, only recommended. We'll introduce 5 tools: PhpStorm, Devel, Xdebug, Drush, and the Stage File Proxy module. Each tool serves a unique purpose, from streamlining coding processes to improving local site performance.
In this tutorial we'll learn:
- The purpose and benefits of using PhpStorm in Drupal development.
- How Devel provides valuable debugging and development assistance.
- The role of Xdebug in code debugging and analysis.
- The importance of Drush for command-line site management.
- How the Stage File Proxy module optimizes local development environments.
By the end of this tutorial, you should be able to understand how these tools contribute to the Drupal module developer experience.
Template files in Drupal modules provide the default HTML markup for the visual presentation of a module's data. Be aware that themes are likely to override the template with site-specific customizations. This template should contain only minimal markup to ensure functionality, and document the variables fed into the template.
In this tutorial, we'll:
- Explain the role of Twig template files in modules.
- Show how modules declare and use template files.
- Recognize how a render array can specify a template.
By the end of this tutorial, you should be able to articulate how and when a module should define a new template file.
Controllers in Drupal should return renderable arrays that contain the content to display for the page. Doing so makes it easier for the theme layer to override the output and customize it for a specific site.
In this tutorial, we'll:
- Convert the
WeatherPage
controller to use a renderable array instead of hard-coded HTML for displaying the weather forecast. - Verify our updates.
By the end of this tutorial, you should understand how to structure content as a render array within a controller.