Step-by-Step Guide to Creating a Custom Module in Drupal 9
- Choose a module name: Choose a unique name for your module that reflects its purpose. For this example, we’ll use “custom_module”.
- Create the module folder: Create a folder with the same name as your module in your Drupal installation’s “modules” directory.
- 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 “custom_module.info.yml” file:
name: Custom Module type: module description: Implements custom functionality for Drupal 9 core_version_requirement: ^9
- 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:
<?php use Drupal\Core\Routing\RouteMatchInterface; /** * Implements hook_help(). */ function custom_module_help($route_name, RouteMatchInterface $route_match) { switch ($route_name) { case 'help.page.custom_module': $output = ''; // Generate output. return $output; default: return ''; } }
In this example, we’re implementing hook, “hook_help()”
- Implement custom functionality: Add functions and hooks to your module file to define the custom functionality you want to implement.
- 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.