Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

The first module

A module is a folder of files that adds new features to the site. The smallest module consists of a single description file. It does nothing, but Drupal already sees it and lets you enable it.

The module folder

Create the folder by hand:

mkdir -p web/modules/custom/blog

The folder name is the machine name of the module. It may contain lowercase Latin letters, digits and underscores, and starts with a letter. This name becomes the prefix of every file and function of the module and a part of the class namespace.

The description file

Create the file blog.info.yml in the module folder:

# web/modules/custom/blog/blog.info.yml
name: Blog
type: module
description: 'A blog page, a subscription form and a latest articles block.'
package: Custom
core_version_requirement: ^10.3 || ^11
KeyMeaning
nameThe name in the list of modules
typeThe kind of extension: module or theme
descriptionThe note under the name
packageThe group in the list of modules
core_version_requirementWhich Drupal versions the module is compatible with

name, type and core_version_requirement are required. The file name must match the folder name: blog/blog.info.yml.

Enabling

vendor/bin/drush en blog

Drush prints that the blog module has been enabled. You can check it in the panel too: open Extend at /admin/modules — the Custom group now has a ticked Blog row.

The list of enabled modules is also shown by a command:

vendor/bin/drush pml --status=enabled --type=module

Dependencies

If the module needs other modules, they are listed in the same file:

# web/modules/custom/blog/blog.info.yml
dependencies:
  - drupal:node

An entry consists of a project name and a module name. drupal:node is the core module for nodes. Now, when blog is enabled, Drupal checks that nodes are enabled and does not let you uninstall them while blog is in use.

Uninstalling

vendor/bin/drush pmu blog

The command uninstalls the module and removes its data: settings and tables. The files stay where they are, and the module can be enabled again.

Creating with a command

Drush can create a module skeleton: drush generate module asks a few questions and creates the folder with files. In the course the files are created by hand — that makes it clearer what each one is for.

Common mistake

The blog.info.yml file is read once and then kept in the cache. If you changed the name, description or dependencies and the panel still shows the old values, run drush cr. The second frequent cause is mismatched names: the folder is blog and the file is my_blog.info.yml. Such a module shows up under the file name, but Drupal does not find the other files with the blog. prefix.

Thanks for reading!

Donate to the site — we work hard for you.