Over 10 years we helping companies reach their financial and branding goals. Onum is a values-driven SEO agency dedicated.

CONTACTS
Drupal Development

Drupal Module Development Tutorial: Build Your First Custom Module

Drupal’s flexibility and power stem largely from its modular architecture, allowing developers to extend and customize functionality through custom drupal module development. If you’re already familiar with Drupal theme development, creating custom modules is the next logical step to truly master the Drupal ecosystem. In this comprehensive tutorial, you’ll learn how to build your first custom Drupal module from scratch, understand how modules interact with themes, and discover best practices that will set you up for success in your Drupal module development journey.

By the end of this guide, you’ll have the knowledge to create modules that enhance your Drupal theme development workflow and add custom functionality to your Drupal sites. Whether you’re building client websites or contributing to the Drupal community, drupal module development is an essential skill in your toolkit.

Drupal Module Development

Understanding Drupal’s Architecture: Modules vs. Themes

Before diving into drupal module development, it’s important to understand how modules and themes work together in Drupal’s architecture.

What Are Drupal Modules?

Modules in Drupal are packages of code that extend, enhance, or add functionality to your site. They’re the building blocks that add features beyond Drupal’s core capabilities. Modules can:

  • Add new features (like contact forms, media handling, or e-commerce)
  • Modify existing functionality
  • Integrate with external services
  • Alter how content is displayed or processed

How Modules Interact with Themes

While themes control the presentation layer of your Drupal site, modules provide the functionality that themes can leverage. This relationship is crucial to understand:

  • Themes determine how content looks visually
  • Modules determine what features are available and how they work
  • Modules can provide blocks, fields, and entities that themes can style
  • Custom modules can enhance theme capabilities by providing additional template variables

In the Drupal 10 era of 2025, this separation of concerns has become even more emphasized, with themes focusing purely on presentation through Twig templates and modules handling all business logic.

Prerequisites for Drupal Module Development

Before you start building your first module, ensure you have:

  1. A local development environment with Drupal 10.x installed
  2. Basic knowledge of PHP programming
  3. Familiarity with Drupal’s hook system
  4. Command line access to your development environment
  5. A code editor (VS Code with Drupal extensions recommended as of 2025)
  6. Git version control (optional but recommended)
  7. Composer for dependency management

If you’re already doing Drupal theme development, you likely have most of these prerequisites covered already.

Setting Up Your Development Environment

The latest best practices in 2025 for Drupal module development recommend using DDEV, Lando, or Docker-based environments for consistent development experiences.

To prepare your environment:

Ensure your Drupal installation is in development mode by setting this in your settings.php:

Enable module development support:

Clear Drupal caches after changes:
bash
drush cr

Planning Your First Custom Module

Before writing any code, you should plan your module by defining:

Module Purpose and Functionality

For this tutorial, we’ll create a simple yet practical “Content Highlighter” module that will:

  • Allow content editors to highlight specific portions of text
  • Add a color picker for selecting highlight colors
  • Provide a custom block to display highlighted content
  • Integrate with Drupal themes to ensure proper styling

Module Requirements

Our module will:

  • Create a custom field type for highlighted text
  • Provide a configuration form for default settings
  • Include a custom block type
  • Add CSS that themes can override

This simple module will demonstrate key concepts while providing actual utility for content creators.

Creating the Module’s Directory Structure

Drupal modules follow a specific directory structure. First, create your module directory:

This structure follows Drupal 10’s latest conventions as of 2025, ensuring maintainability and compatibility.

Writing the Module’s Info File

The .info.yml file is the module’s identity card. Create content_highlighter.info.yml:

Note that in 2025, Drupal 10’s module dependencies use semantic versioning, which is reflected in our dependency on the color_field module.

Implementing Your Module’s Basic Functionality

Now let’s implement the core functionality. We’ll focus on the main files one by one:

The Module File

Create content_highlighter.module:

The Install File

Create content_highlighter.install to handle installation and updates:

The Libraries File

Create content_highlighter.libraries.yml to define CSS and JS assets:

Create the JavaScript File

Create js/highlighter.js:

Creating a Custom Block

Now let’s create the custom block that displays highlighted content:

Creating the Template File

Create templates/highlighted-content.html.twig:

How Custom Modules Enhance Drupal Module Development

Custom modules like our Content Highlighter provide powerful ways to extend Drupal theme development capabilities. Here’s how modules and themes work together in the Drupal ecosystem:

Providing Theme Variables

Our module provides Twig templates with variables that themes can use. This allows theme developers to:

  • Override the highlighted content template in their theme
  • Apply custom styling to highlighted elements
  • Extend the functionality with theme-specific features

Adding Theme-specific Libraries

The module’s CSS and JS can be extended or overridden by themes using library-extend or library-override in the theme’s .info.yml file:

Creating Theme-aware Configuration

Our module’s configuration allows site builders to select colors that match the site’s theme palette, ensuring visual consistency.

Exposing Blocks for Theme Placement

By providing a custom block, our module allows theme developers to place highlighted content in any region defined by the theme, offering layout flexibility.

Testing Your Module

Before installing your module, verify your code quality with:

Test functionality by:

  1. Visiting the configuration page (Structure > Content Highlighter Settings)
  2. Creating content with highlighted text
  3. Placing the Highlighted Content block in a region
  4. Checking that highlights display correctly with your theme

Debugging Common Issues

When developing Drupal modules, you may encounter these common issues:

  • Cache-related issues: Remember to rebuild cache with drush cr after changes
  • Hook implementation errors: Verify hook names and parameters match Drupal’s API
  • Theme function errors: Ensure template variables are properly defined
  • Database schema issues: Check entity and field definitions
  • JavaScript errors: Use your browser’s developer tools to debug

The Devel and WebProfiler modules are invaluable for debugging in 2025’s Drupal ecosystem.

Next Steps and Advanced Techniques

After mastering this basic module, consider these advanced techniques:

  1. Implement Config Entities: Store complex configuration with fields
  2. Create Content Entities: Develop fully fieldable highlight entities
  3. Add REST Integration: Expose your module’s functionality via REST API
  4. Implement Unit Tests: Ensure module reliability with PHPUnit tests
  5. Create Admin UI Components: Build advanced interfaces using modern JS frameworks
  6. Add Migration Support: Support upgrading from earlier versions
  7. Develop a Complementary Theme: Create a theme that showcases your module’s features

Conclusion: Taking Your Drupal Module Development to the Next Level

You’ve now created your first custom Drupal module that enhances your site’s functionality while integrating seamlessly with Drupal theme development. The Content Highlighter module demonstrates fundamental concepts that you can apply to more complex modules in the future.

By understanding how modules and themes work together, you’re well-equipped to create powerful Drupal solutions that combine beautiful presentation with robust functionality.

Ready to share your work with the Drupal community? Consider contributing your module to Drupal.org or joining a contribution event to collaborate with other developers.

For more advanced Drupal theme development techniques that leverage custom modules, contact the YuzTech team, where we dive deeper into creating theme-module pairs that work in perfect harmony.

Author

zhafran