How to create custom module in Drupal 9?

Drupal Academy
2 min readJan 28, 2023

--

Drupal is a powerful and most used content management system (CMS) that allows you to create custom modules to extend its functionality. In this blog post, we will walk through the process of creating a custom module in Drupal 9.

First, let’s create a new folder in the “modules/custom” directory of your Drupal installation and name it “hello_world”. It is standard practice to create “custom” directory inside the “modules” directory.

Inside the “hello_world” directory, create a new file called “hello_world.info.yml” and add the following code:

It is required file to create custom module.

File: hello_world.info.yml

Next, create a new file called “hello_world.routing.yml”, this file is used to create URLs to access the page. Inside this file add the following code:

File: hello_world.routing.yml

This code defines a new route for your custom module, which will be accessible at the path “/hello-world” on your website.

Now, let’s create the controller that will handle this route. To create controller, create two directories inside "hello_world" module directory. “src/Controller” and inside the Controller folder, create the “HelloWorldController.php” file

File: HelloWorldController.php

This controller defines a single method called “message” that returns a render array with the message “Hello World” on the frontend of the website.

Finally, you need to enable the module from the Drupal’s administration interface and can access the route ‘/hello-world’ URL it will show a “Hello World” message on your page.

You can clone the code from this GitHub repo Hello World Module Link.

Also, you can watch the below Drupal video tutorial on How to create custom module in Drupal 9?

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Drupal Academy
Drupal Academy

Written by Drupal Academy

Learn how to build amazing website with our in-depth articles on Drupal's component and solutions for common challenges. https://www.youtube.com/@drupal-academy

No responses yet

Write a response