Drupal Console: Generate Module & Theme Code

Drupal Console Featured

Drupal Console is software which allows you to alter your Drupal installation through the command line. According to the official website, “The Drupal Console is a CLI tool to generate boilerplate code, interact and debug Drupal 8.” Unlike Drush, Drupal Console is specifically for Drupal 8, the latest major release.

Although Drupal Console and Drush share many capabilities such as clearing the cache, generating one-time login links, or un/installing modules/themes, one distinct functionality that comes out of the box with Drupal Console is that it can generate boilerplate code for modules, themes, controllers, forms, blocks, and much more.

Installing Drupal Console

Setting up Drupal Console is just as easy as executing the following commands found on the homepage of the website:

To check if Drupal Console is working, execute drupal list. You should see a list of commands:

Downloading Drupal

Let’s start from the beginning. We can actually get any version of Drupal by running drupal site:new:

Installing Drupal

Now, you might be thinking to go visit the running website to install Drupal. Wrong! Well, right you can do that. But, it is also possible via Drupal Console by simply running drupal site:install inside the drupal directory:

Our new Drupal site should now be ready to be worked on:
Drupal Home

Activating Maintenance Mode

It is best to set a website in production to maintenance mode when being worked on. Be sure to log in first, so you can view the development of the site. Then you can turn on maintenance mode with drupal site:maintenance on:

Now, this is what regular users will see when visiting the website:

Drupal Maintenance

Creating a Hello World Module

Defining Module Parameters

Drupal Generate Module Directory Structure
First let’s generate code to define the module such as the .info.yml and the composer.json files by running drupal generate:module:

 

 

Installing the Module

Again, we can just enable to module using the command line using drupal module:install {PLUGIN_MACHINE_NAME}:

Generating the Controller

Now, we need to generate a controller that will show the “Hello World” page. Do this using drupal generate:controller:

Drupal Hello World Directory Structure

This will automatically generate our controller file, routing file, and even our Test file.

Finished Module

Now, if we visit http://www.mydrupalwebsite.com/hello/world, we should see:

Drupal Hello World 1

This way of generating modules is much quicker than normal. You can compare it to normally creating a Hello World module.

Creating a Block

We can also make a block to go along with this module and have it hold a configuration value. This can be done by running drupal generate:plugin:block:

Drupal Plugin Block Directory Structure

If we now go to Admin > Structure > Block layout > Sidebar second > Place Block, we can see our newly generated block there and add it to the sidebar:

Drupal Place Block
Drupal Configure Block
Drupal Default Block

Generating Random Nodes

If we are just testing our website for development and need some content, nodes can be easily generated using drupal create:nodes:

Now, you can see some of these articles posted to the front page with images:

Drupal Front Page

Generating a Theme

Apart from generating modules and content, we can also generate themes. Let’s generate a basic theme that extends the classy base theme by running drupal generate:theme:

Drupal Theme Directory Structure

Three new files should have been generated in the themes folder. You should now be able to install the theme by running drupal theme:install {THEME_MACHINE_NAME}:

 

 

 

The theme still needs to be set as default before we can see it in action. However, it will just be a plain style-less theme because it just extends classy:

Drupal New Theme

Deactivating Maintenance Mode

As we are done with our development, stop maintenance mode by running drupal site:maintenance off:


 

This post just covered the surface of what all Drupal Console can do. It can do so much more such as generating Forms, Permissions, and even most of the features from Drush. Remember to explore all the commands by running drupal list. It is a tool every Drupal developer should be taking advantage of. You can learn more of the commands and shortcuts on the Drupal Console Documentation.

Author: Akshay Kalose

A teenager, who is interested in Computer Science, Information Technology, Programming, Web Designing, Engineering and Physical Sciences.

One thought on “Drupal Console: Generate Module & Theme Code”

Leave a Reply

Your email address will not be published. Required fields are marked *