Skip to main content
Ivan Vidusenko
Drupal Modul Erstellung

With its modular structure of features, Drupal allows a website to be assembled from individual building blocks according to taste. While platforms like WordPress rely primarily on easy installation and handling even in untrained hands, Drupal has historically always had a strength in being able to be customized by developers down to the smallest detail, or even extended by additional modules. It is this strength that makes Drupal a powerful tool for developing customized solutions for unique challenges.

An important first step in the evolution from a simple site builder and content maintainer to a Drupalista is the creation of your own (so-called "custom") modules. While Drupal allows you to build a robust website using only existing modules and themes from the Drupal community ("contributed" modules, or "contrib" for short), the ability to expand the selection of modules yourself is sooner or later essential.

This article assumes that the developer has access to an existing Drupal installation and its source code. On the official website drupal.org and other sites such as StackOverflow you can find resources and instructions for setting up your own Drupal. A rudimentary understanding of PHP is also a prerequisite for writing your own module.

What is a module?

In order to allow developers to make as many changes to Drupal as possible without having to intervene directly in the source code of the core system ("core") - which makes it possible to carry out updates easily without having to deal with conflicts - Drupal relies on making as much of the system as possible readable and modifiable in so-called modules. Any features and customizations that go beyond the core functionality are stored in these modules.

Der Drupal Core und hinzufügbare Module

In itself, a module is nothing more than a new folder under the path "modules/". By default, developers store modules provided by the community in the "modules/contrib" sub-path and their own developments for the website in "modules/custom".

Create Drupal module: Manually

Especially when entering the world of modules, it is advisable to create the first module completely manually. Detailed information and instructions can be found on drupal.org, but here we present the basics again.

First of all, the name of the module must be decided. A distinction must be made here between the "machine name" (lower case, with underscores instead of spaces, without special characters) and the title (human-readable). For our example, we will work with the name "Example Module" (example_module).

Now that we have the name, we create our module folder based on it: "drupal/modules/custom/example_module".

Within the module folder, information must be stored in a specific format to inform the system of the module's existence. This includes, among other things

  • the title of the module

  • the version number of the module

  • the version number of the Drupal core for which the module is intended

  • a categorization of the functions (e.g. "Media")

  • a short description text

This information is stored in a file with the name "MODULENAME.info.yml". "MODULENAME" represents the name of the module to be created, in this example "example_module.info.yml".

This file is in YAML format. Detailed information can be found on this page. What follows is an example skeleton of an info.yml file:

With this folder and this file, the work is already done! Although the module has no function, you can now select and activate it in the Drupal module list.

Create Drupal module: DrupalConsole generator

While manually creating a module is important for learning purposes, this process accompanies a developer through his entire life with Drupal. Every time a new feature or topic is introduced, a new module is usually created for it. In the long run, manual creation can therefore become quite tedious. But as an IT developer, you are equipped to automate and simplify manual processes. This is where DrupalConsole comes into play.

Alongside Drush,DrupalConsole is one of the two major terminal tools used by Drupal developers. While Drush is the proven standard for avoiding cumbersome navigation through the Drupal backend and directly changing settings or initiating processes like cache clear, DrupalConsole first appeared as a competitor and "new standard" for Drupal 8, but has ultimately evolved into a supplement instead, with features that have a different focus than those of Drush.

It should be noted that this method is suitable for experienced developers who are familiar with the command line (terminal).

The generators are particularly valuable with DrupalConsole. These are a series of commands that allow a developer to create and fill in templates of files, folders and classes to speed up important processes such as creating custom modules, entities or services. Since large parts of this work are always the same and only details change, a simple "wizard" process with a series of questions takes the typing out of the developer's hands.

Once you have installed DrupalConsole and are in the terminal of your choice in the Drupal folder, all you have to do is run the tool with the command "drupal generate:module". After a short loading time, DrupalConsole will then ask a series of questions to fill out the module. Once these have been answered, you will then find your brand new module folder in the selected location with everything you need to get started.

Conclusion

If you want to exploit the full potential of Drupal, you should be able to "lend a hand" with your own code developments in order to tackle the unique challenges of your web presence. Creating a module is the most important first step.

Are you still working with an outdated Drupal version? We offer you an upgrade service! Learn more: Drupal 9 Upgrade.