Image
Rusty mechanical arm with gear and lever, in a grassy field.

drush generate module walk-through

The best place to start when familiarising yourself with "drush generate..." commands is "drush generate module" because this provides the module framework onto which other generate commands can build files, e.g. "drush generate plugin:manager". This is often a dev's first experience of "drush generate..." and first impressions are important!

Setup

Obviously you will need to have Drush installed on your Drupal website to use this command, in most cases all you need is to run composer require drush/drush but otherwise I suggest reading Installing and Upgrading on drush.org.

Command

In its simplest form, run drush generate module

The generator will then ask a number of questions to correctly name and locate the necessary boilerplate file additions and updates.

Module Name

$ drush generate module

 Welcome to module generator!
––––––––––––––––––––––––––––––

 Module name:
 ➤ 

Firstly enter the human-readable name of the module you wish to create, e.g. "My Module".

Module Machine Name

 Module name:
 ➤ My Module

 Module machine name [my_module]:
 ➤ 

Next provide the machine name of the module, in most cases you can simply press enter to accept the default which is shown in brackets ([my_module]) and is based on the module name you have just entered. The machine name should be made up of characters, digits and underscores. There are plenty of examples where the machine name of a module is not the same as the human-readable name, e.g. the machine name of the Internationalisation module is "i18n".

Module Description

 Module name:
  My Module

 Module machine name [my_module]:
  

 Module description:
  

Enter a short description of the module. This will be used in the list of modules to install/uninstall in the Extend section of your site (/admin/modules). Perhaps review this page for examples to guide what you could use.

Package

 Module name:
 ➤ My Module

 Module machine name [my_module]:
 ➤ 

 Module description:
 ➤ My custom module

 Package [Custom]:
 ➤ 

The Package groups similar modules together on the Extend page. Similar to choosing the Module Description, review the Extend page for examples to guide you.

Dependencies

 Module name:
 ➤ My Module

 Module machine name [my_module]:
 ➤ 

 Module description:
 ➤ My custom module

 Package [Custom]:
 ➤ Fields

 Dependencies (comma separated):
 ➤ 

Dependencies are other modules on which your module depends. These could be core modules (e.g. Field) or contrib modules (e.g. AI). Type the module names separated by commas, e.g. "field,ai", or leave blank if there are no dependencies.

Module File

 Module name:
 ➤ My Module

 Module machine name [my_module]:
 ➤ 

 Module description:
 ➤ My custom module

 Package [Custom]:
 ➤ Fields

 Dependencies (comma separated):
 ➤ field, ai

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

Drupal modules no longer require a .module file so I recommend that you accept the default option "No", unless you know in advance that you will be writing hooks.

Install File

 Module name:
 ➤ My Module

 Module machine name [my_module]:
 ➤ 

 Module description:
 ➤ My custom module

 Package [Custom]:
 ➤ Fields

 Dependencies (comma separated):
 ➤ field, ai

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

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

Similar to the module file, I recommend that you accept the default option "No", unless you know that you will require a .install file.

README.md File

 Module name:
 ➤ My Module

 Module machine name [my_module]:
 ➤ 

 Module description:
 ➤ My custom module

 Package [Custom]:
 ➤ Fields

 Dependencies (comma separated):
 ➤ field, ai

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

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

 Would you like to create README.md file? [No]:
 ➤ Yes

I recommend that you do opt to create a README.md. This file serves as the primary documentation for the module and can be useful to users and future developers. The generate command produces a skeleton document that is useful when edited in conjunction with the drupal.org README.md template documentation.

Files

Having answered the questions you are presented with a list of the files that have been created, e.g.:

 The following directories and files have been created or updated:
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  /web/modules/my_module/my_module.info.yml
  /web/modules/my_module/README.md

For details of the README file see the relevant section above.

.info.yml

name: 'My Module'
type: module
description: 'My custom module'
package: Fields
core_version_requirement: ^10 || ^11
dependencies:
  - drupal:field
  - ai:ai

The "info yaml file" contains the answers you provided which is necessary to notify Drupal of the module's existence and provide information for the UI admin pages. For more information I recommend reading the documentation on drupal.org.

Command Options

--destination=path/to/module - by default the module files will be placed in WEBROOT/modules, it is highly likely that you will want to override this, e.g. --destination=modules/custom

It is possible to use the command with eight "--answer" options, now that we know the inputs required for the module generator, as follows:

drush generate module \
  --answer="My Module" \
  --answer=my_module \
  --answer="My custom module" \
  --answer=Fields \
  --answer=ai,field \
  --answer=No \
  --answer=No \
  --answer=Yes

Reference

Details of this command can be found in my new Reference section.

 

Note that the details of this article are based on Drush 13.x (tested on Drush 13.6.2). Be aware that the questions (and their order) could differ in past and future versions of Drush.

Tags

If you have any comments or a particular topic in mind for a future article, or if you'd like to explore contracting possibilities, please get in touch.