Step-by-Step Guide to Creating a Custom Module in Drupal 9

29 / Apr / 2023 by Rajveer Singh 0 comments
  1. Choose a module name: Choose a unique name for your module that reflects its purpose. For this example, we’ll use “custom_module”.
  2. Create the module folder: Create a folder with the same name as your module in your Drupal installation’s “modules” directory.
  3. Create the module file: In the module folder, create a file with the same name as your module but with the “.info.yml” extension. This file is used to define metadata about your module, such as its name, description, dependencies, and version. Here’s an example “” file:
name: Custom Module
type: module
description: Implements custom functionality for Drupal 9
core_version_requirement: ^9
  1. Define module functions: Create a file with the same name as your module, but with the “.module” extension. This file is where you’ll define your module’s functions, hooks, and other custom code. Here’s an example “custom_module.module” file:

use Drupal\Core\Routing\RouteMatchInterface;

* Implements hook_help().
function custom_module_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case '':
$output = '';
// Generate output.
return $output;

return '';

In this example, we’re implementing hook, “hook_help()”

  1. Implement custom functionality: Add functions and hooks to your module file to define the custom functionality you want to implement.
  2. Enable your module: Log in to your Drupal 9/10 site as an administrator, go to the “Extend” page, and enable your module.

Once your custom module is enabled, you can test and refine its functionality as needed.

Keep in mind that this is just a basic example, and the specific code and functionality will depend on the nature of your module and the features you want to implement. It’s also important to follow Drupal coding standards and best practices when developing your module to ensure its stability, maintainability, and compatibility with other modules and Drupal updates.


Leave a Reply

Your email address will not be published. Required fields are marked *