Create a Drupal module
About code scaffolding
There are a few projects that can help you to create a module, like Module Builder (opens in a new tab) that will scaffold most of the boilerplate code for you, or Examples for Developers (opens in a new tab), code that you can moslty copy and paste and trust that this is a good reference to start with.
CLI tools
drush generate
command (opens in a new tab) is based on the drupal-code-generator (opens in a new tab) project.
Drupal Console (opens in a new tab) is a great tool that helped Drupal 8 developers in many ways, but it's now deprecated (opens in a new tab), mostly in favor of Drush. If you are moving from Console to Drush, Dieter Holvoet did a comparison of commands (opens in a new tab).
Make also sure to review the other Developer Experience related projects.
Here we will cover the basics of creating a module from scratch.
The minimal requirements
- A directory with the name of the module (e.g.
my_module
) - Usually created in modules/custom
- A
.info.yml
file with the name of the module (e.g.my_module.info.yml
) - The
.module
file is optional, but usually present to implement hooks and other global functions (e.g.my_module.module
)
.info.yml structure
name: My module
type: module
description: 'A description of my module'
core_version_requirement: ^9 || ^10
package: Custom
configure: my_module.settings
dependencies:
- drupal:node
- other_module:other_module
Now let's cover how a module can be structured. We won't cover all the possibilities, just the most common ones.
Module files
We just met these two files:
my_module.info.yml
my_module.module
Other files that can be present:
my_module.routing.yml
- Routes definitionsmy_module.services.yml
- Service, event subscribers, etc. definitionsmy_module.libraries.yml
- Define libraries (css, js)my_module.install
- Install, update and uninstall hooks (create, update, delete tables or configuration)my_module.post_update.php
- Post update hooksmy_module.api.php
- Define hooks to be used by other modulesmy_module.permissions.yml
- Define permissionsmy_module.links.menu.yml
- Define menu links, e.g. in the admin menumy_module.links.task.yml
- Define task links, e.g. a tab on the node edit formmy_module.links.action.yml
- Define action links, e.g. a link to delete a nodemy_module.links.contextual.yml
- Define contextual links, e.g. a link to edit a node from the teaser
Module directories
src
directory is used for PSR-4 autoloading, so we can store classes there.
my_module/src
- PHP classes, like services or pluginsmy_module/src/Controller
- Controllers defined in the.routing.yml
filemy_module/src/Form
- Forms defined in the.routing.yml
filemy_module/src/Entity
- Custom entity classesmy_module/src/EventSubscriber
- Event subscribers defined in the.services.yml
filemy_module/src/Plugin
- Plugins
Other directories
my_module/templates
- Twig templatesmy_module/config/install
- Configuration files to be installedmy_module/config/schema
- Schema files to define configurationmy_module/tests
- Unit, Kernel and Functional tests, test modules
Add libraries: CSS and JS
Adding assets (CSS, JS) to a Drupal module via *.libraries.yml (opens in a new tab) - Drupal.org
Read more
Creating modules (opens in a new tab) - Drupal.org