Guide

Drupal Module Developer Guide for Drupal 8, 9, 10, and 11

Drupal Module Developer Guide

Content in this guide

Drupal Module Developer Guide

Learn Drupal module development in this hands-on guide.

What will you learn

  • Extend the Anytown Farmers Market site created in the Drupal User Guide
  • Learn how to extend Drupal with custom code
  • Get hands-on practice in 2 custom modules using Drupal APIs
  • How to add your custom code through key integration points and patterns

Overview

The Drupal Module Developer Guide is the best way to get started with the concepts and APIs involved in writing custom code for Drupal. This guide builds on the concepts and skills covered in the Drupal User Guide.

This guide serves as a starting point for people who are familiar with Drupal site building and basic PHP, but with minimal knowledge of Drupal module development. The topics will help you learn foundational module development skills, introduce the most commonly used APIs, guide you towards diving deeper as needed, and give you the knowledge required to get started creating Drupal modules. The guide is also helpful for people who already have some experience with a current or past version of Drupal, and want to expand the range of their skills and knowledge or update them to the current version.

The guide consists of concept and task tutorials. Concept tutorials are designed to explain a specific critical concept like info files, or plugins, as well as answer questions like, "What is the best way to structure my code?" or "How do permissions affect routes?" Task tutorials provide step-by-step instructions that will walk you through the process of writing 2 different custom modules that address site specific needs. Through these hands-on lessons, you'll learn all the fundamental skills required to start writing custom modules of your own that extend, alter, and enhance a real-world Drupal powered site.

Once you learn the foundations check-out our other guides and dive deeper into specific topics like managing data with the Entity API, defining content with the Render API, or writing tests.

Drupal Module Developer Guide

Preface

This chapter introduces the target audience for the guide, the guiding scenario, and what you can expect to learn. It covers how the content of the guide is organized, and what style conventions we used.

At a minimum you should familiarize yourself with the guiding scenario as it sets the stage for the different coding challenges presented throughout the guide.

Categories
Drupal 8, 9, 10, and 11
2 tutorials
Tutorials in this course

Introduction to Module Development

This chapter starts by explaining what modules are and the role they play in a Drupal application. Then introduces new module developers to essential tools and resources. The chapter ends with 2 tutorials that walk through setting up everything required to be able to follow along with the coding challenges in the rest of the guide.

Hello World

In this chapter you’ll write your first module, create a custom block that adds content to a Drupal page, and learn about how info files provide Drupal with information about a module and its capabilities. In addition to getting to write some real code and have Drupal use it, you’ll get to practice navigating your new development environment and the other module development-related tools introduced in the previous chapter.

Routes and Controllers

This chapter walks through the process of coding a new page and adding a menu item that a site administrator can place in a menu to link to the page. You’ll learn about Drupal’s routing system, how to define a controller, and how roles and permissions are used to restrict access.

Services and Dependency Injection

This chapter introduces the concepts of services and dependency injection. You’ll learn about the role of services in writing maintainable and reusable code, how to use any of the many useful services provided by Drupal core, and how to encapsulate your own custom business logic into a service. This chapter builds on the code started in the previous chapter and refactors it according to Drupal best practices.

Outputting Themable Content from a Module

Anytime a module outputs something to display on the page, it should do so in a way that’s compatible with Drupal’s theme layer. This ensures that themes have the final say in what the page looks like. In this chapter we’ll introduce Drupal’s Render API, Twig template files, CSS and JavaScript asset libraries, and user interface localization utilities. Then we’ll practice using the combination of these things to ensure our module’s output is themable.

Hooks and Events

Hooks and events are 2 ways that modules can alter existing Drupal features, and respond to what other code in Drupal is doing. They are a key part of what makes it possible for custom modules to modify almost any aspect of how Drupal works. In this chapter we’ll learn how to discover, and implement, both of these fundamental APIs.

Plugins

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.

Categories
Drupal 8, 9, 10, and 11

Forms

This chapter builds on earlier chapters about controllers and Render API to introduce the Drupal Form API and demonstrates how to define the fields of a form, handle validation of user input, and manage processing of submitted data. As well as how to alter the structure and behavior of forms provided by other modules.

Working with Data

This chapter defines the different types of data (content, configuration, etc.) that Drupal manages and introduces the Entity and Field APIs. We’ll start a second custom module in this chapter. You’ll learn about how Drupal’s data management layer helps make it possible for custom and contributed modules to all safely interact with the same data. This chapter covers how to perform basic CRUD operations, querying for lists of entities, and utilities that exist to make you more efficient when working with entities in routes and controllers.

Caching

In this chapter we’ll revisit the code written in previous chapters and use Drupal’s Cache API to make it more efficient. You’ll learn important caching-related terminology like cache tags, context, and keys. And how to implement caching of both rendered content and time-consuming logic.

Testing

In this chapter we’ll learn how to configure your local environment to run Drupal’s PHPUnit-based tests. We’ll define the different types of tests (unit, kernel, and functional) that a Drupal module might need. And work through examples of writing tests of each type in order to provide coverage for the features of our 2 custom modules.

Additional Considerations

This chapter introduces additional best practices that module developers should be aware of, including the Drupal coding standards, tips for writing secure code, and tools the community uses to help ensure consistent code quality and standards.

Collaborate with the Community

Drupal, and its ecosystem, are built and maintained by a global community. In this chapter you’ll learn about how you can connect with, collaborate with, and contribute to the community. Especially as it relates to the use and creation of contributed modules.

Final Thoughts

This guide is just the start of learning Drupal module development. In this final chapter we’ll provide guidance on where to go next in order to continue your learning adventure.

Drupal 8, 9, 10, and 11
1 tutorials
Tutorials in this course
“Drupalize.Me has trained thousands of Drupalistas. Their video lessons help to address our constant need for more Drupal talent. Drupal needs training like this to support its growth.”
Dries Buytaert
Drupal Founder and Project Lead
Photo of Dries Buytaert
“They’re easy to understand. They’re thorough. They’re funny. They’re always entertaining, and it makes it easy to learn parts of Drupal and how you can integrate it into your workflow and learn more and have a good time doing it.”
Roger Carr
Drupalize.Me Member
Photo of Roger Carr
“The mission of the Drupal Association is to foster and support the Drupal software project, the community, and its growth. Drupal education, like that provided by Drupalize.Me, is important to this mission.”
Megan Sanicki
Former Exec. Director, Drupal Assoc.
Photo of Megan Sanicki

FAQs

Am I required to sign a contract?
No. You can purchase a membership and/or cancel any time. Drupalize.Me is a pay-as-you-go service.

Can I preview tutorials before joining?
Yes! Just navigate to our tutorial library. Our free tutorials are labeled with a green "FREE" tag.

Can I watch videos on my mobile device?
Yes! Drupalize.Me is a responsive site and can be accessed in the browser on any mobile device. More FAQs