Create custom module in Drupal 11

Create a Custom Module in Drupal 11

Learn how to build a custom module step by step with this guide for Drupal 11.

Steps to Create a Drupal 11 Module:

  1. Create a directory for your module in modules/custom. For example, for a module named mymodule, create a directory modules/custom/mymodule.

  2. 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
              
  3. 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.');
      }
    }
              
  4. Enable the module via the UI or with Drush:

    drush en mymodule
  5. Clear the cache:

    drush cr

Your custom module for Drupal 11 is now ready to use!

Required Files for a Custom Drupal 11 Module

When developing a custom Drupal module, the following two files are essential:

  1. mymodule.info.yml – This file provides crucial metadata for your module, such as its name, description, and compatibility with Drupal core. It's essential for Drupal to recognize and register your module. Without this file, Drupal won't detect your module, and you won’t be able to enable it.
  2. mymodule.module – This file is where the functionality of your module resides. It contains hooks and custom logic, like the hook_help() function for providing help text. Without this file, your module won't perform any custom actions, even if it's enabled.

What Happens if Files Are Missing?

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.

Conclusion

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.

Need Help with Your Drupal Theme or Module?

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