Drupal 9 Custom Module Development

| | 6 min read

Drupal is the most advanced software product that has gained users’ trust.1 Drupal module development is a content management framework (CMF) released almost two decades ago and is still liked by content-heavy website owners. 

While Drupal is designed to be a flexible, extensible platform, it can be challenging to figure out the best techniques for extending it and how your custom module fits into Drupal’s overall architecture. This step-by-step guide will walk you through the process of creating a module to extend Drupal's functionalities.

What is a Drupal Module?

A Drupal module is a collection of files that contain code that extends Drupal functionality by adding new features. The code is built to execute within the context of the site, meaning it can use all the functions and access all variables and structures of the Drupal core. 



What is Drupal Module Development?

Drupal, from its initial release itself, is built as a platform that is extensible. Even many core functionalities are created as modules. If you get a grip on how to develop a Drupal module, there is no limit to what you can do with Drupal. If you check the Drupal module ecosystem, you will find a variety of modules - from a simple redirection functionality to a complex CRM system. If you are new to Drupal, we recommend you go through the Drupal User Guide before you read further.

What are the types of Drupal Modules?

Drupal Modules are divided into three types:

Core - Core modules are the backbone of your Drupal website. These are included with the shipment of each Drupal package. You don’t have to install it manually. It comes along out of the box. All you need to do is enable it and start using it.

Contributed - Contributed modules work in tandem with the backbone to complete your website’s skeletal structure. Contributed Drupal modules can include many features that are not necessarily required. There may be tons of files that are not needed and can slow down the website. It may also affect the core modules of the Drupal core modules.

Fun fact - Over 60,000 websites use modules contributed by us worldwide, and we have held the honour of being one of the top 10 Drupal contributors in the world in the past.2

Custom - Custom module development is compatible with Drupal 8 and Drupal 9, but this will require extensive PHP programming knowledge and Drupal’s API documentation. This feature lets you create custom configurations, field types, field widgets, and field formatters. Custom modules or plugins always meet our requirements in any CMS or Framework as it fulfills specific requirements for your project, which are not available in the community.

It is considered the best because you can add features or drop them as per your requirements. You don’t have to get along with all the features you give, like in the core module. Your website will not get overburdened, and you must maintain only things necessary for your business. 

Can anyone get started with Drupal 9 custom module development?

If your Drupal module development needs are minimal, you should be able to finish it as a typical DIY project. However, you will need professional Drupal 9 custom module development services to develop complicated modules on Drupal 9. To keep it 100, a little bit of prior working experience with Drupal will make a difference.

With that said, let’s get started with the Drupal 9 custom module development.

Here you go!

The selected name of the module is “demo_module”, and the module created will also have the same name. As the case is, begin by creating a folder under the Drupal installation on the path: “sites/all/modules/custom/demo_module.”



Step 01: Create a Directory

Initially, we need to create a directory in the modules folder of drupal root at path [Drupal_Root]/modules/custom. We have made it with the name “zyxware”.

This name would be a machine name. So, this will have all small letters without any space.

Step 02: Initialise Module

In this step, we have to initialise a module which will connect with Drupal core for working as part of drupal 9. We need to create an info file, i.e. zyxware.info.yml at path [Drupal_Root]/modules/custom/zyxware/zyxware.info.yml. The filename should be the same as the module machine name.

name: zyxware - Custom Module 

type: module 

description: ‘This is our first custom drupal 9 module.’ 

package: Custom 

version: 1.0 

core: 9.x 

In this file, we have defined the name, type, description, package, version of module and core of the module, which will explain which version of drupal core this module will interact.

Let’s dig into what’s happening here:

  • name: the human-readable name of the module: appears on the module overview page
  • description: the human-readable description of the module: appears on the module overview page
  • package: the group the module will be placed in: appears on the module overview page
  • type: module (vs profile or theme)
  • core_version_requirement: restricts the module to Drupal 9 only

Step 03: Setup of routes

Now, our next step is to set up routes. We need to create a file, i.e. zyxware.routing.yml which will contain information about routes in YAML format.

In the routing file, you’ll find these :

  • Unique key of each routing, i.e. zyxware.content
  • Path of routing, i.e. /zyxware/page. The page will be accessible by this path http://example.com/zyxware/page.
  • Controller or Form of this path, i.e. DemoController & DemoForm
  • Permission of this routing. ‘access content’ defines that this page is accessible to an anonymous user

Step 04: Create Controller

As we defined in the previous step, here, we’ll create a file at path [Drupal_Root]/modules/custom/zyxware/src/Controller with the name DemoController.php with the following content

Through this code, we have defined a content method in DemoController, which is returning markup type text. We can also produce a template which will camm theme files in our custom module.

Step 05: Create Form

Similarly, above step, we’ll create a file for our form at path [Drupal_Root]/modules/custom/zyxware/src/Form with the name DemoForm.php with the following three methods (getFormId, buildForm, validateForm & submitForm ), which are compulsory in form.

In this code, we’ll define four methods :

  • getFormId: In this method, we have defined form id.
  • buildForm: In this method, we have build form fields, i.e. Full Name & Email Address.
  • validateForm: In this method, we validate that the full name value’s length should be more than ten characters.
  • submitForm: In this method, we are saving the values of our submitted form.

Step 06: Ready to Install

Now, your Drupal 9 custom module development has been concluded and we are ready with the module. You need to login into your drupal site and install a module to extend the admin page from the top navigation bar. Once the module gets installed correctly, you can test paths, i.e. /zyxware/page & /zyxware/form.

If you are getting a ‘Page Not Found’ error, clear the cache by navigating to admin > configuration > performance and test it again.

Now, you are ready with your custom module. You can tinker with the example module to learn more - as they say,

Reading code is the best way to understand a software system.

Wrapping Up

With an understanding of what modules are and why they’re helpful, and how to install and configure them, you're well to take drupal 9 custom module development for your drupal 9 websites to fit your organization's needs. But sometimes, it may be that you can’t find the module of your choice for your Drupal Website. Moreover, you think the available modules do not meet your bespoke requirements. We can help you with our exceptional Drupal Custom Module Development Services in such a case.

We have extensive experience in Drupal Development and Consulting Services or extensions or add-ons to fulfil our clients’ diverse needs & deeds. Let us know if you want to know more about Drupal 9 Custom Module Development!