Skip to main content
Drupal Module Development

Module Development – A Complete Guide

Drupal 10 is one of the most flexible and powerful CMS platforms, and one of its greatest strengths lies in custom module development. Modules allow developers to extend Drupal’s core functionality, integrate third-party systems, and implement custom business logic that goes beyond what contrib modules can offer.

In this blog, we’ll walk through the basics of creating a custom module in Drupal 10, key components involved, and best practices to follow.

Step 1: Create the Module Folder and Files

All custom modules go inside:

web/modules/custom/

Example folder structure for a module named hello_world:

hello_world/
  ├── hello_world.info.yml
  ├── hello_world.routing.yml
  ├── hello_world.links.menu.yml
  ├── src/
  │    └── Controller/
  │         └── HelloWorldController.php
 

Step 2: Define the Module with .info.yml

The .info.yml file tells Drupal about the module.

hello_world.info.yml:
name: 'Hello World'
type: module
description: 'A simple module to demonstrate Drupal 10 custom module development.'
package: Custom
core_version_requirement: ^10
version: 1.0

Step 3: Add a Route with .routing.yml

Define a route (URL path) for your module.

hello_world.routing.yml:

hello_world.content:
  path: '/hello'
  defaults:
    _controller: '\Drupal\hello_world\Controller\HelloWorldController::content'
    _title: 'Hello World'
  requirements:
    _permission: 'access content'

Step 4: Create a Controller

Controllers are PHP classes that return content.

src/Controller/HelloWorldController.php:

<?php

namespace Drupal\hello_world\Controller;

use Drupal\Core\Controller\ControllerBase;

class HelloWorldController extends ControllerBase {
  public function content() {
    return [
      '#type' => 'markup',
      '#markup' => $this->t('Hello World! This is my first Drupal 10 custom module.'),
    ];
  }
}

Step 5: Add a Menu Link (Optional)

If you want a menu item in the navigation:

hello_world.links.menu.yml:

hello_world.menu:
  title: 'Hello World'
  description: 'Link to Hello World page'
  route_name: hello_world.content
  parent: system.main_content
  weight: 100

Step 6: Enable Your Module

Run this command:

drush en hello_world

Then visit:
 /hello – You’ll see your custom page.

Best Practices for Module Development

  1. Follow PSR-4 standards for naming classes.

    1. Namespace must match the folder structure.

      1. Example:

        namespace Drupal\hello_world\Controller; class HelloWorldController { ... }

        File location must be:

        modules/custom/hello_world/src/Controller/HelloWorldController.php
    2. Top-level namespace = module name.

      1. Drupal\hello_worldhello_world is your module folder.

    3. Sub-namespaces = subdirectories inside /src.

      1. Controller namespace → /src/Controller/ folder.

      2. Plugin\Block namespace → /src/Plugin/Block/ folder.

    4. Class name = filename.

      1. HelloWorldController class must be in HelloWorldController.php.

    5. No underscores (_) in class names.

      1. Use CamelCase for classes (e.g., HelloWorldController).

  2. Use services & dependency injection instead of \Drupal::service().

  3. Store configuration with config schema (.schema.yml).

  4. Use hooks (.module file) where necessary.

  5. Write tests for your module to ensure stability.