Learn how to build a custom module step by step with this guide for Drupal 11.
Create a directory for your module in modules/custom
. For example, for a module named
mymodule
, create a directory modules/custom/mymodule
.
Create the mymodule.info.yml
file with the following content:
name: My Module type: module description: A custom module for Drupal 11. core_version_requirement: ^11 package: Custom
Create a mymodule.module
file for hooks and functionality:
<?php /** * @file * Contains the functionality for mymodule. */ /** * Implements hook_help(). */ function mymodule_help($route_name, \Drupal\Core\Routing\RouteMatchInterface $route_match) { switch ($route_name) { case 'help.page.mymodule': return t('This is the help text for My Module.'); } }
Enable the module via the UI or with Drush:
drush en mymodule
Clear the cache:
drush cr
Your custom module for Drupal 11 is now ready to use!
When developing a custom Drupal module, the following two files are essential:
hook_help()
function for providing help text. Without this file, your module won't perform any custom actions, even if it's enabled.
Let's say you create a module with only the mymodule.info.yml
file but forget to include the mymodule.module
file. In this case, your module will show up in the Drupal admin interface, and you’ll be able to enable it. However, since there’s no functionality defined in the mymodule.module
file, your module won't do anything custom—like showing help text or adding any specific features.
On the other hand, if you skip the mymodule.info.yml
file, Drupal won’t even recognize your module, no matter what functionality you’ve added in the mymodule.module
file. The module simply won’t be available for activation.
To ensure your custom Drupal 11 module works as expected, remember to include both the mymodule.info.yml
and mymodule.module
files. These files are the foundation of your module, allowing it to be recognized and function properly. By following this guide and adhering to Drupal's file structure standards, you can build powerful modules that enhance your site’s functionality.
With the right setup, your custom modules will not only provide valuable features for your users but also improve the overall performance and SEO of your Drupal site.
If you need assistance creating a custom Drupal theme or module, feel free to reach out to us. We are here to create it for you!
Write to Us