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.
Asset libraries in Drupal associate CSS and JavaScript files with components, which enhances performance because the assets will only be loaded when they're needed.
In this tutorial, we'll:
- Define asset libraries and their purpose.
- Learn how to define an asset library in a module.
- Examine how asset libraries attach to components.
By the end of this tutorial, you should understand how asset libraries optimize resource loading and facilitate component-based development in Drupal.
Modules can define new asset libraries that include CSS and JavaScript files, and then attach them to the content they output. This process involves defining a new asset library, authoring the related CSS and JavaScript, and using the #attached
render array property to associate the library with the applicable content.
In this tutorial, we'll:
- Define a new asset library in the anytown module.
- Include CSS and JavaScript in the library.
- Attach the new library to the /weather page.
By the end of this tutorial, you should be able to define an asset library and add CSS and JavaScript to a page from a Drupal module.
One of the features of any content management system's architecture is the separation of presentation and data. In Drupal, modules are responsible for figuring out what should be on the page, and themes are responsible for the final look and feel of anything shown in the browser. It's vital for a module to return themeable output, so that the active theme can determine how it's presented.
In this tutorial, we'll:
- Define themeable output.
- Show how modules can avoid embedding presentation data in their output.
- Explain why Drupal favors structured arrays over HTML strings for data presentation.
By the end of this tutorial, you will be able to articulate the role modules play in enabling themes to customize a Drupal site's appearance.
Drupal modules need to ensure that any strings of text they add in code can be localized by Drupal's translation system. When our code creates a new <button>
element with the text "Toggle forecast" it needs to be done in a way that would allow changing that text to another string, like "Alternar pronóstico", depending on the user's preferred language.
In this tutorial, we'll:
- Define the difference between internationalization and localization and how they allow a Drupal site to be translated.
- Explore the utilities available to module developers to ensure that the user interface strings in their code are translatable.
By the end of this tutorial, you should be able to explain how and why to internationalize the code in your Drupal modules.
Are you ready to learn how to extend and customize Drupal sites in code? The Drupal Module Developer Guide will introduce you to extending Drupal with modules. You will learn foundational concepts and get hands-on practice with a variety of APIs used in Drupal coding and development. We'll be extending the site we built in the Drupal User Guide. If you're new to Drupal site building, we recommend that you walk through the Drupal User Guide first.
Drupal offers module developers several methods for creating different types of links, all defined by module configuration. This tutorial explores these types of links, how they relate to each other, and when to use them.
In this tutorial, we'll learn:
- The nature of menu items, action links, and local task links.
- Examples of each link type in Drupal's UI and their application in our scenario.
- Criteria for selecting the appropriate link type for adding the weather page to site navigation.
By the end of this tutorial, you'll understand the different types of links in Drupal and how they apply to adding navigational elements for custom module pages.
Permissions in Drupal control access to features and functions. Modules define permissions, which allow site administrators to grant or restrict access based on user roles. As a module developer, you'll create new permissions to restrict access to your module's custom features, independent of existing permissions defined by other modules.
In this tutorial, we'll:
- Explore how and where permissions are defined within a module.
- Discuss the concept of static and dynamic permissions in Drupal.
By the end of this tutorial, you'll have a clear understanding of how permissions function in Drupal and their implementation in modules.
Modules can define custom permissions to restrict access to specific routes or page sections. This control allows module developers to provide granular access while enabling site administrators to manage user privileges. We'll add a view weekly weather
permission via the anytown module to limit access to the weather page.
In this tutorial, we'll:
- Create an MODULE_NAME.permissions.yml file.
- Define a new permission.
- Use the new permission to restrict access to a route.
By the end of this tutorial, you should be able to define a new permission in a module and use it to control access to a route.
Custom services in Drupal modules encapsulate specific business logic or functionality. In our example, we'll demonstrate moving code required to access a weather forecast API from a controller into a service. This will help make our controller thin and our module code more reusable, testable, and maintainable.
In this tutorial, we'll:
- Explore the advantages of custom services for managing business logic.
- Define the components of a custom service.
By the end of this tutorial, you'll understand why creating custom services is a beneficial practice in Drupal module development.
To access services in Drupal through the service container, you'll need to know the unique machine name of the service. We'll use the example of making HTTP requests to a weather forecast API in the anytown module to demonstrate several methods you can use to identify an existing service's ID.
In this tutorial, we'll:
- Discover existing services and their machine names.
- Take a look at an example service definition.
By the end of this tutorial, you should be able to locate and use existing services in your Drupal module.
Concept: Testing
FreeTesting ensures that code remains reliable and functional. This tutorial introduces the primary types of tests in Drupal: Unit, Kernel, Functional, and FunctionalJavascript -- all executed via PHPUnit. We'll clarify the differences between each type of test and appropriate use cases. As module developers, understanding what to test and how to write tests is vital for robust and maintainable code.
In this tutorial, we'll:
- Identify the primary test types in Drupal and their use cases.
- Emphasize the importance of functional tests in custom module development.
- Introduce the basics of authoring tests in a custom module.
By the end of this tutorial, you should recognize the different types of tests Drupal uses and when and how to use each kind.
Before you can run tests, you'll need to configure your local environment. This setup involves Drupal-specific configuration for PHPUnit and ensuring your environment supports Functional JavaScript tests with a WebDriver client and a compatible browser. The setup process varies based on the development environment. In this tutorial, we're using DDEV as the local environment.
In this tutorial, we'll:
- Install all required dependencies.
- Configure PHPUnit specific to our environment.
- Validate the setup by running a Drupal core test.
By the end of this tutorial, you'll be equipped to run Drupal's PHPUnit tests locally using DDEV.
Functional tests simulate user interactions with Drupal applications, which enables us to test user interfaces and complex workflows. This tutorial guides you through writing functional tests for the anytown module, focusing on custom user registration workflow enhancements.
In this tutorial, we'll:
- Examine functional test structure.
- Test
anytown_form_user_register_form_alter()
customizations. - Discuss the functional test execution environment.
By the end of this tutorial, you'll know how to write functional tests that emulate browser interactions with your Drupal application.