Introduction
Any functional customization of Drupal 8 should be done through a custom module. This article serves as a quick reference for creating a custom module.
Prerequisites
It is assumed that Drupal 8 was installed using Composer. The main difference is that the modules
folder of a Composer based installation is inside the /web
folder, while in the 'traditional' installation the modules
folder is in the root directory.
Step 1 – Create the module folder
The name of the custom module will be My Custom Module
.
First create a folder named my_custom_module
inside the web/modules/custom/
// Go to the custom modules folder
cd web/modules/custom
// Create the custom module folder
mkdir my_custom_module
Step 2 – Create the .info.yml file
Create the my_custom_module.info.yml
file inside the custom module folder
// To to my_custom_module folder
cd my_custom_module
// Create the module .info.yml file
touch my_custom_module.info.yml
Edit my_custom_module.info.yml
to look like this:
name: My Custom Module
description: Custom functionalities.
package: Custom
type: module
core: 8.x
To make it compatible with the upcoming Drupal 9, replace the core: 8.x
with core_version_requirement: ^8 || ^9
name: My Custom Module
description: Custom functionalities.
package: Custom
type: module
core_version_requirement: ^8 || ^9
Step 3 – Enable the custom module
Enable the custom module by either running the drush command:
drush en my_custom_module
or enable it from the UI by navigating to the Extensions section of the admin menu.
Conclusion
These are the absolute minimum requirements to create a custom module. For more details check out the official documentation at https://www.drupal.org/docs/8/creating-custom-modules.