
Learn how to create a custom module in Drupal 11 with this step-by-step guide, including code examples, troubleshooting tips, and best
Introduction: Why Custom Modules Matter in Drupal 11
Drupal is a powerful, flexible Content Management System trusted by developers worldwide for building enterprise level websites. While its core and contributed modules provide robust functionality, real power lies in the ability to extend it with custom modules.
In Drupal 11, custom modules allow us to:
- Add features to your project’s exact needs
- Interact with APIs or other systems
- Customize content types, permissions, routes, and more
Let’s walk through how to create your a custom module in Drupal 11, even if you’re just getting started.
Step 1: Setting Up the Module Directory
First, navigate to your custom modules directory:
cd web/modules/custom
Create a new folder for your module. Let’s call it hello_world:
mkdir hello_world
cd hello_world
Your folder structure should now look like:
web/
└── modules/
└── custom/
└── hello_world/
Step 2: Create the info File
The .info.yml file tells Drupal about your module. Create a file named: hello_world.info.yml inside the module folder:
name: 'Hello World'
type: module
description: 'A simple custom module for Drupal 11.'
core_version_requirement: ^11
package: Custom
dependencies: []
version: 1.0.0
Step 3: Add a Basic Controller
hello_world/src/Controller/HelloWorldController.php
Add this code into controller file:
<?php
namespace Drupal\hello_world\Controller;
use Drupal\Core\Controller\ControllerBase;
class HelloWorldController extends ControllerBase {
public function hello() {
return [
'#markup' => $this->t('Hello, Drupal 11!'),
];
}
}
Step 4: Define a Route
Create a file named hello_world.routing.yml:
hello_world.hello:
path: '/hello-world'
defaults:
_controller: '\Drupal\hello_world\Controller\HelloWorldController::hello'
_title: 'Hello World'
requirements:
_permission: 'access content'
Step 5: Enable the Module via Admin UI
Go to /admin/modules and install the newly created module.
Congrats! You’ve now created your first custom module in Drupal 11.
Learn more about Drupal from Official site Drupal.org

0 Comments