How to Develop a Custom Drupal Module

Last updated on June 25, 20245 min
Eduard Grigalashvili
Technical Writer
Oleg Bogut
Tech Lead

As of today, Drupal offers over 51,000 contributed modules that can dramatically enhance the functionality of your website. From access control to administration to integrations to search engine optimization—the CMS seems to have it all for successful website management. (And if you want to find real gems, check out our article: Best Drupal Modules for Your Website.) However, when it comes to implementing very specific and rare functionality, you may have a hard time finding a fitting ready-made module.

In this case, you should create a custom module in Drupal.

As a Drupal development company with over 12 years of experience in the technology, AnyforSoft is here with a step-by-step guide to Drupal module development. We will explain the basics of how custom modules are made so that you can create a tailored solution that perfectly meets your requirements.

Let's get started!

Drupal 7 vs Drupal 10 Comparison

Before proceeding to Drupal custom module development, let’s compare Drupal 7 and Drupal 10, since many users are still on the 7th version of the CMS. As you probably know, Drupal 7 will reach its end of life on January 5, 2025, so businesses still have some time to migrate to Drupal 10. However, considering many new features and improvements that came with the Drupal 10 release, we recommend switching to the latest Drupal version as soon as possible. If you want to hire a Drupal developer who will provide Drupal 10 upgrade and migration services, feel free to reach out to us through the website form.

Drupal 7 and Drupal 10 differ in many aspects, from programming style to path system to configuration management. These differences in logic mean that the development of a Drupal custom module will also differ between the two versions.

Platform Version Drupal 7 Drupal 10
Programming Style Procedural Object Oriented
Path System hook_menu() Routing
Events System Hooks Events and Hooks
Configuration Management Features module Configuration API
Templates PHPTemplate Twig

Note that this article explains how to create a custom module for Drupal 10.

Creating Our First Custom Module for Drupal 10

So, after analyzing the differences between the Drupal 7 and Drupal 10 versions, let's move to custom Drupal modules development. Below, you will find a comprehensive guide on how to design a tailored solution for your Drupal site.

1. Selecting a name for the Drupal custom module and creating a folder for it

Before creating a custom module in Drupal, you will have to come up with a unique name for your specific solution, as well as the machine name that will be used within the module itself.

After that, make a folder in which your module will be placed. According to the traditions of Drupal 10 development, this folder is placed inside the root directory and has the standard name “modules/custom”.

After you have created the modules category for all your future custom modules, create a category for our particular case. Suppose that our module is called My Module, and the machine name is called my_module, respectively. In this case, the folder should be named my_module too.

2. Creating the files

At this stage, you need to build a file with the extension .info.yml (in our case it will look like my_module.info.yml) and a file with the extension .module (in our case it will be my_module.module).

3. Creating a directory to place the files

The next step in our tutorial will be the creation of a src subfolder to accommodate Controllers, Forms, Services, Plugins, Templates, and other files of our project. This will automatically load the class controllers of your future custom modules.

4. Creating the base controller

In order to create a base controller, you will need to make another directory called Controller in the src folder. In this directory, you need to create the MyController.php file. To begin, write something into this file (for example, a simple program with the output line “This is my first module”) to observe how it works:


<?php

namespace Drupal/my_module/Controller;

use Drupal/Core/Controller/ControllerBase;

class MyController extends ControllerBase {

  public function content() {
    return array( '#type' => 'markup', '#markup' => t('This is my first module'), );
  }

}

5. Creating a route file

Now we need to get our controller to start doing something really useful. To do this, we will now create it (in our case it will be named my_module.routing.yml) and connect it to the route from the URL:


first_module.content:

path: '/first'

defaults:

  _controller: 'Drupal/my_module/Controller/MyController::content'

  _title: 'This is my first module'

requirements:

  _permission: 'access content'

6. Launching the module

So, we are launching our module. In order for the user to receive content, your module in Drupal must receive URL information. The most convenient way to implement all of this is to create a new file with the extension .menu.yml and place it in the root directory. Inside the file, you will need to write the following code:


my_module.admin:

title: 'First module settings'

description: 'A basic module to return a string'

parent: system.admin_config_development

route_name: my_module.content

weight: 100

7. Clearing the cache

Now clear the cache (that way, you'll be able to see your new menu item).

8. Creating a custom block

For these purposes, you will need to create a new plugin. To do this, in the src folder, create a new directory called Plugin, and add another directory called Block inside it. In the Block directory, you create a new file (in our case it will be called MyPluginBlock.php). Here, you either have to manually specify the classes and namespaces to use or they will be pulled up automatically in case you use some IDE during development. Put the following code there:


<?php

namespace Drupal/my_module/Plugin/Block;

use DrupalCoreBlockBlockBase;

class MyPluginBlock extends BlockBase {

}

9. Adding a new method

The next step is to organize the inheritance of the child classes and add a new method inside the HelloBlock class:


class HelloBlock extends BlockBase {

public function build() {
  return array(
    '#markup' => $this->t('This is my first module'),
   );
}

}

10. Clearing the cache again

After clearing the cache, go to the administrative panel. Here you need to add our block to the sidebar. To do this, click Place block and save this option with the needed settings. Now scroll to the bottom of the administration page and click Save blocks.

Voila! Your module is ready!  As you can see, the process is not that difficult.

Conclusion

Now you know how to create a simple module for Drupal. Of course, our article covers only the basics of this process: To design something more sophisticated, you will need technical expertise in the technology.

If you don't want to dive deep into the nuances of custom module creation, you can always count on AnyforSoft. Our Acquia-certified developers will take care of any Drupal-related task, helping you build a tailored solution that fully meets your business requirements.

Contact us today and tell us about your project.

Want to work with us?