Drupal - How to create a empty module?

The below instructions allows one to create an empty module, and is helpful for anyone just getting started with module building. If you have troubles getting your first module working, or even showing up in Drupal, ensure you've read all the instructions below.

Drupal 8

A project must at least have

  1. A machine name.
  2. An yaml info file named after the machine name, in the form module-machine-name.info.yml with the following attributes:
    1. name: A humanly readable name
    2. type: A type, defining to be a module.
    3. core: The major Drupal core version the module is compatible with, 8.x, in this case.
  3. An empty module file, name in the form module-machine-name.module

Drupal looks for modules in these locations, as seen from the web root:

  1. /modules/
  2. sites/[example.com]/modules
  3. sites/default/modules
  4. profiles/[install-profile]/modules

Technically, Drupal also looks for modules in core/modules, but one should never, place modules there, hence it's not on the list above.

An example module structure, for a module with the machine name helloworld, would look like this:

/modules/helloworld/helloworld.info.yml

/modules/helloworld/helloworld.module

Notice that both the info and module file as named exactly the same as the machine name, which is important.

The module file may be empty, but the info file must contain a few minimum values for Drupal to recognize it as a module. For our helloworld module, that could look like:

name: 'Hello world module to demonstrate module building'
core: 8.x
type: module

If you follow the above instructions, you should be be able to get a new module listed in your Drupal site, although it won't do anything, at this stage.

Drupal 7

A module must at least have

  1. A machine name.
  2. A humanly readable name
  3. An info file named after the machine name.
  4. An empty module file.

To be loadable by Drupal, it must also define which core version it is compatible with.

Further Drupal looks for modules in these locations:

  1. sites/all/modules/
  2. sites/[example.com]/modules
  3. sites/default/modules
  4. profiles/[install-profile]/modules

Technically, Drupal also looks for modules in modules, but one should never, place modules there, hence it's not on the list.

An example module structure, for a module with the machine name helloworld, would look like this:

sites/all/modules/helloworld/helloworld.info

sites/all/modules/helloworld/helloworld.module

Notice that both the info and module file as named exactly the same as the machine name, which is important.

The module file may be empty, but the info file must contain the humanly readable name of the module, and the core version the module is compatible with.

For our helloworld module, that could look like:

name = Hello world module to demonstrate module building
core = 7.x

If you follow the above instructions, you should be be able to get a new module listed in your Drupal site, although it won't do anything, at this stage.


For Drupal 8, since the question mentioned a blueprint, I figure I'd mention the Drupal Console project.

Once that is installed, one can generate module code (and other things like scaffolding for custom entities, plugins, etc).

To generate a basic module from the command line: drupal generate:module. One is then prompted through some basic information gathering, and left with the module files at the end:

Drupal console module generation

Tags:

8