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.
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
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.
We will create a simple module in this tutorial, which welcomes the users when they log in.
$ 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.
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:
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.
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!
Start Growing with Cloudways Today.
Our Clients Love us because we never compromise on these