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
:
Then visit:
/hello
– You’ll see your custom page.
Best Practices for Module Development
Follow PSR-4 standards for naming classes.
Namespace must match the folder structure.
Example:
File location must be:
Top-level namespace = module name.
Drupal\hello_world
→hello_world
is your module folder.
Sub-namespaces = subdirectories inside
/src
.Controller
namespace →/src/Controller/
folder.Plugin\Block
namespace →/src/Plugin/Block/
folder.
Class name = filename.
HelloWorldController
class must be inHelloWorldController.php
.
No underscores (
_
) in class names.Use CamelCase for classes (e.g.,
HelloWorldController
).
Use services & dependency injection instead of
\Drupal::service()
.Store configuration with config schema (
.schema.yml
).Use hooks (
.module
file) where necessary.Write tests for your module to ensure stability.