This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Try the fastest hosting platform with pay-as-you-go pricing & 24/7 expert support! MIGRATE NOW →

How to create a Drupal 8 module via Drupal console

Updated on February 1, 2021

3 Min Read

You don’t have to be an expert to know that the Drupal 8 module contains drastic modifications when compared to its predecessor.  More importantly, there is a notable difference when you create your own modules.

Although creating modules in Drupal 8 is a complex task, it can be made easy through a magnificent CLI tool for Drupal, called the Drupal Console. Drupal Console is a tool built around the symfony framework and follows the OOP methodology.

Create Module with Drupal Console Banner

In case you’re wondering why use Drupal Console when we already have Drush. Well, the former can be used to generate boilerplate code out of the box, which is a huge benefit considering the OOP aspect of Drupal 8. So without further ado, let’s find out how to easily create Drupal 8 modules.

Install Drupal Console

To install Drupal Console, you need to run three commands:

curl https://drupalconsole.com/installer -L -o drupal.phar

Move it to your local bin, so that it can be used anywhere on your system: mv drupal.phar /usr/local/bin/drupal

Make it executable: chmod +x /usr/local/bin/drupal

Module Command

Once you’ve installed Drupal Console, you need to run it inside the folder of your Drupal site. Simply navigate to your folder via the ‘cd’ command. Eg: cd user/site/drupal8.

Now we can start creating the module. This can be done through the generate:module command.

Generating Module

We will create a simple module in this tutorial, which welcomes the users when they log in.

First type:

$ drupal generate:module

After executing the command, don’t change the defaults for any options except for the following two:

  • Enter Module Description: The description of your module
  • Do you want to generate a .module file: yes

Now if you navigate to the the /modules/custom directory, you should see your welcome module’s folder there.

Check Module Folder

To check the module’s details, navigate to the module folder and you will find a file name with the following pattern ‘module_name.info.yml’. This file is required by Drupal 8 to know that the module exists.

Install Module

It is now time to install our module. Thanks to Drupal Console, there’s no need to go inside the admin panel->modules. You can install the module directly through Drupal Console by executing the following command:

$ drupal module:install welcome

Create Controller and route

Next we need a controller to return the message and a route to map a URL to the controller. Both the controller and the route can be created through Drupal Console through the following command:

$ drupal generate:controller

Upon executing the command, you will get the following messages:

Enter the module name: welcome

Enter the Controller class name: WelcomeController

Enter the Controller method title (leave empty and press enter when done): Welcome Controller

Enter the action method name: welcome

Enter the route path: /welcome

Enter the Controller method title (leave empty and press enter when done): (empty)

Do you want to generate a unit test class (yes/no): no

Do you want to load services from the container (yes/no): no

Do you confirm generation? (yes/no): yes

Subsequently, the following two files would automatically generate in your module folder:

  1. /modules/custom/welcome/src/Controller/WelcomeController.php
  2. /modules/custom/welcome/welcome.routing.yml

Now if you go to the following address: www.yoursite.com/welcome (‘yoursite’ is your site’s name), you should be brought to the page where your module is supposed to be generated.

Conclusion

There you go! This is the beauty of Drupal Console. Without any coding, Drupal Console has created a module, a controller, mapped a URL route to it and has created a URL path /welcome. This is just a very short example of how Drupal Console makes your work easier. Hope you found this walkthrough useful!

 

Share your opinion in the comment section. COMMENT NOW

Share This Article

Hamza Zia

Hamza is a Drupal Community Manager at Cloudways - A Managed Drupal Hosting Platform. He loves to write about Drupal and related topics. During his free time, he can be seen obsessing over Football, Cars, Android and Gaming.

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now