15

What is the most basic valid module one can create in Drupal?

It would be useful to have a simple blueprint available, for anyone just getting started with Drupal.

0

2 Answers 2

26

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.

5
  • and also sites/default/... just saying ...
    – tenken
    Feb 16, 2013 at 0:03
  • You're absolutely right, fixed it. :) Which reminds me I should check up on the order and make sure it's correct.
    – Letharion
    Feb 16, 2013 at 0:03
  • For drupal 8 you now only need the module-machine-name.info.yml. The .module file is not required anymore to have an enabled module. Not that it's very useful but it's possible :)
    – devunder
    Oct 3, 2014 at 10:50
  • 1
    D8: .module and .profile files are no longer required. drupal.org/node/2217931
    – sobi3ch
    Nov 19, 2015 at 20:29
  • 1
    Drupal 8: What about drupal generate:module ?
    – Screenack
    Dec 25, 2015 at 22:18
8

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.