Power of Drupal Console in Drupal 8
Blog

Power of Drupal Console in Drupal 8

Drupal Console is command line tools, help us to speed up the development tasks for Drupal websites. After installing console, you will be able to perform actions simply by typing commands into a terminal, actions that usually takes multiple steps in a web browser (Drupal Installation/ Field addition on Bundle), or Writing a basic code.
Just to add here, Drupal Console works with Drupal 8 only, whereas Drush runs on

Below are the syntax for Console, With Drupal Console you can generate boilerplate code for modules, themes, controllers, forms, blocks, and much more.
        
        Node
              drupal create:nodes
        Controller
              drupal generate:controller
        Entity
        Form alter hook
        Module
              drupal generate:module
        Block
              drupal generate:plugin:block
        Field type, widget and formatter
        Image effect
        Rest resource
        Service
        Theme

              drupal generate:theme
        Switch maintenance mode on or off
              drupal site:maintenance on
        Run unit tests

There are few similar tools available e.g.

Module Builder: (Generates Drupal 6, 7, or 8 module scaffolding)
Drupal Module Upgrader: (Converts modules from Drupal 7 to Drupal 8; generates static help file with links to relevant change records)
Drupal 8 Tools: (Drupal code generator written in bash)
Drush: (Interact with Drupal installation via CLI, create aliases, create custom commands)

Why it’s Unique?

Drupal Console use modern PHP practices introduced into Drupal 8, includes object-oriented PHP. Console isn't a Drupal module, but was built with the Symfony Console Component and other libraries, such as Twig, to generate PHP, YAML, and other file types used in Drupal 8 module development.

It is a designed for anyone using or planning to use Drupal 8. At the moment, it is used via a CLI, but there are plans to make it accessible through the Drupal administration interface.

Drupal Console provides a number of commands for creating module temporary structure and boilerplate code. For any command, you will be asked a series of questions about what you want. In that case files are created and inside these files, classes — complete with namespacing and use statements — are created for you with the naming convention you specified in the command's prompts.


Let’s Have a look

For installation please refer here.
Once you have done with installation type drupal in terminal and you’ll get below list

s1

To pull out all the commands and option for console type drupal list & you will get list of options and available commands.

s2

As i’m going to show you how can we create boilerplate code “Generating Custom form in Module
Before we start with building a custom form we need to generate a module first

Step 1: Run drupal generate:module

s3

// Welcome to the Drupal module generator

 Enter the new module name:
 > Contact us

 Enter the module machine name [contact_us]:
 > [you can provide own machine name or else it will take default [contact_us]]

 Enter the module Path [/modules/custom]:
 > [you can provide own path or else it will take default [/modules/custom]]

 Enter module description [My Awesome Module]:
 > Creating custom contact us form with few fields

 Enter package name [Custom]:
 > [you can provide own choice or else it will take default [Custom]]

 Enter Drupal Core version [8.x]:
 > [you can provide own Core version or else it will take default [8.x]]

 Do you want to generate a .module file (yes/no) [no]:
 > [you can provide own choice or else it will take default [no]]

 Define module as feature (yes/no) [no]:
 > [you can provide own choice or else it will take default [no]]

 Do you want to add a composer.json file to your module (yes/no) [yes]:
 > [you can provide own choice or else it will take default [yes]]

 Would you like to add module dependencies (yes/no) [no]:
 > [you can provide own choice or else it will take default [no]]

 Do you confirm generation? (yes/no) [yes]:
 > [you can provide own choice or else it will take default [yes]]

Once we pressed yes we will get below files

Generated or updated files
 Site path: /var/www/html/drupal-8.0.2
     1. modules/custom/contact_us/contact_us.info.yml
     2. modules/custom/contact_us/contact_us.module
     3. modules/custom/contact_us/composer.json

s4

Finally we have done with module creation using Drupal Console. using Console we can reduce the time and energy to develop custom module or site configuration, which takes more when we go through step by step procedure in ui level.

Git Source: https://github.com/xaiwant/D8ConsoleForm/tree/master/contact_us

Download the Drupal Guide
Enter your email address to receive the guide.
get in touch