How to create modules in Drupal?

Drupal module means an extension that can be used to extend the core functionality. There are a lot of contributed modules available in Drupal.org. Most of the time, our requirements are satisfied with those contributed modules. But sometimes we have to create our own custom modules.

Here I am explaining how to create a custom module. This blog is based on the Drupal 10 version.

There are three ways to create modules.

1. Manually

If you are new in Drupal development please use this method to create modules.

  • Create a folder under your-project-folder/web/modules/custom directory Eg: refactor
  • Add your-project-folder/web/modules/custom/refactor/refactor.info.yml file
  • Add necessary informations on refactor.info.yml file
name: Refactor

type: module

description: 'Providing custom project handlers.'

package: Custom

core_version_requirement: ^8 || ^9 || ^10

name key provides text name of module which is displayed in admin module listing page.

description is the brief of the module.

type key value should be ‘module’

core_version_requirement key specifies that this module is compatible with versions mentioned here. The Core_version_requirement requirement only works with 8.7.7 or greater. If your moule need to work with lower version then you have to use core key ie, core: 8.x

package key can be used to group the modules in admin module module listing page. This is not a required key. But it would be nice to place your module under a package.

On the above nametypecore_version_requirement or core are required key. The others are optional.

List of other key can be used in .info.yml file

Dependencies  -: This key is used to add the list of dependent modules.

Eg: dependencies:

       - drupal:file

test_dependencies  -: Same like Dependencies, but it is used to add list of test dependency modules.

configure: refactor.settings  This is used to specify module configuration file. You can add refactor.settings.yml file under refactor/config directory. It is not required to mention configure key in .info.yml file

php: 8.2 This key is used to specify the minimum PHP version required to run this module.

hidden: true  If you want to hide the module from admin module listing

required: true If you don’t want to remove the  module once installed.

There are some other keys which will be added automatically when we submit this module to Drupal.org.

.info.yml file the only file required to install the module. But I would recommend to add .module ie your-project-folder/web/modules/custom/refactor/refactor.module file to keep it standard.

2. Using Drush

Drush is a command line tool for drupal development.You can install drush via composer require --dev drush/drush

Run the below code after installing drush

vendor/bin/drush generate module

This will ask a lot of options like below.    

Welcome to module generator!

Module name [Web]:

refactor

Module machine name [test]:

refactor

Module description [Provides additional functionality for the site.]:

Providing custom project handlers.

Package [Custom]:

custom

Dependencies (comma separated):

Would you like to create module file? [No]:

yes

Would you like to create install file? [No]:

Would you like to create libraries.yml file? [No]:

Would you like to create permissions.yml file? [No]:

Would you like to create event subscriber? [No]:

Would you like to create block plugin? [No]:

Would you like to create a controller? [No]:

Would you like to create settings form? [No]:

That’s it. Drush will create a module under the web/custom folder automatically.

3. Using Drupal console

Drupal console is another command line tool for drupal which is inspired by symfony console.

You can install Drupal console via

composer require drupal/console:~1.0 \
–prefer-dist \
–optimize-autoloader \
–sort-packages \
–no-update

Run the below code after installing Drupal console

vendor/bin/drupal generate:module

This will ask the following options

// Welcome to the Drupal module generator

Enter the new module name:

refactor

Enter the module machine name [refactor]:

refactor

Enter the module Path [modules/custom]:

custom

Enter module description [My Awesome Module]:

Providing custom project handlers.

Enter package name [Custom]:

custom

Enter Drupal Core version [8.x]:

8.x

Do you want to generate a .module file? (yes/no) [yes]:

yes

Define module as feature (yes/no) [no]:

Do you want to add a composer.json file to your module? (yes/no) [yes]:

Would you like to add module dependencies? (yes/no) [no]:

Do you want to generate a unit test class? (yes/no) [yes]:

Do you want to generate a themeable template? (yes/no) [yes]:

Do you want proceed with the operation? (yes/no) [yes]:

That’s it. Console will create a module under the web/custom folder automatically.

This is just a basic example of how to create a module. You can add functionalities based on your requirements.