Drupal Console
Blog

Developing Drupal 8 sites in a smarter way - Drupal Console

I am sure You would have already started building sites on D8, by this time. And you would have noticed, how flexible and fast D8 is despite some struggles in the initial stage.

Tools to know about for Drupal 8

There are many supporting tools set to make D8 more faster & easier for developers. Drupal Module Upgrader - In short, we can port a d7 module to d8. Drush - Command line and scripting interface for Drupal Devel - Probably the single most important tool for module development, behind Drush.

And now I would like to share my knowledge on newly introduced and fascinated "Drupal Console". Wondering, what is Drupal Console? This is a command line interface. Wait, Again a CLI? Yes! Drupal Console project, like drush is headed towards full command-line abilities to control Drupal 8 site.  

So, How is console different from drush?

We have console newly introduced for only d8 which is inspired from symphony console, where as Drush is supported by older versions of drupal too.  So, now you might be thinking what is the need to both for drupal 8? Can’t we make use of only one CLI?

Its the features!

Drush:

  • Sync the file system using rsync
  • Import/Export databases.
  • Use drush aliases to manage environment

Drupal Console:

  • Generate code for Module, Content type, Controller, Entity, Rest resource, Service, Theme, Permissions etc.
  • Debug services, routes etc
  • Run unit tests.

They do have few in common too! Rebuilding cache, Managing of modules & configuration and generating own custom commands.

There are some discussions that happened on integrating console with drush, but this is not done. So, for time being, we have to use both drush and drupal console or may be choose one depending on your needs. Okay, Let’s try to integrate drupal console into our development workflow.

Installation of Drupal Console

I have used this guide to install and created a fresh drupal 8 instance. https://www.drupal.org/project/console (Installation and use)

# Create a new Drupal 8 project: 

drupal site:new drupal8.dev

This creates a new drupal 8 project instance.
Navigate to the site you have downloaded, and run following command.

# Install the new site:

drupal site:install

This installs your site instance.

No UI, Created a drupal 8 installation using CLI! Not only D8 installation, but drupal console aims at many other things to make developers work faster. Just start with typing the command and give your requirements as per the steps and your task is achieved in seconds!

Console Commands:

Lets dig out console by trying some interesting commands we might use often. Basic help command, to help us with instructions all the time.

drupal list

# Lists outs all the available commands

The other way round, I would also say drupal console as a learning tool for drupal 8. I am unaware of  how to create a Custom Form in D8. So I just used the following command.

drupal generate:form

You can now see, above command generates the structure required for the form with 3 methods getFormId(), buildForm(), submitForm(). And now it’s easy to create a Form. I have learnt a lot  from console in this way!

Let me share on few other things too i have learnt from Console.

Console for Developers

Assume you created a view with many fields, filters in some instance and now you want to use the same view in another site without creating from scratch.

drupal cev
# Exports a view into specified module.

drupal cev

Enter the module name [custom_module]:

> View to be exported [Archive]:

> test_view

Export view in module as an optional configuration (yes/no) [yes]:

>

Include view module dependencies in module info YAML file (yes/no) [yes]:
>

[+] The following module dependencies were included at
/var/www/html/d8/modules/custom/custom_module/custom_module.info.yml   [-] node   [-] user
commands.views.export.messages.view_exported
- modules/custom/custom_module/config/install/views.view.test_view.yml
- modules/custom/custom_module/config/install/node.type.article.yml

This will create views.view.test_view.yml, with any other dependent yml files under modules/custom/custom_module/config/optional/
If you choose optional as "no", In this case views.view.test_view.yml, with any other dependent yml files under modules/custom/custom_module/config/install/ and make sure you remove any other core yml files if exported along with this. You can still remove the config folder if you dont need it anymore! This way, You can now include this view into a module and reuse it in another site.

Connect/manage database using drupal console

drupal database:client

# This establishes a connection with the available database.

Connection: mysql -A --database=xxx --user=xxx --password=xxx --host=XXXX --port=XXXX

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is xxx
drupal database:drop

# drop all the tables in given database.
drupal database:dump

# dump the structure and contents of a database.

 

Console for Site Maintainers

Even the site maintainers can take an advantage of console to maintain the site in a smarter way.
We know drupal restricts the user to login after a couple of failed login attempts. So, to give access back to the user again, we have to clear those attempts from the database. Drupal console helps to do it in a single command.

drupal user:login:clear:attempts or drupal uscla

drupal uscla

> Enter User Id [1]

# Give the user Id.

> # This clears the failed login attempts of a particular user

Creating Custom Commands

Like drush, You can actually create custom commands too using Console.

drupal generate:command

Enter the module name [custom_module]:

>   Enter the Command name. [custom_module:default]:

> testcommand

Enter the Command Class. (Must end with the word 'Command'). [DefaultCommand]:

> TestCommand Is the command aware of the drupal site installation when executed?. (yes/no) [yes]:

>

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

>

This will generate a Class at modules/custom/custom_module/src/Command/TestCommand.php with 2 methods.
configure --> Configuration settings of the command like name etc.
execute --> Process to be handled when the command is executed.

Now, to execute newly created custom command:

drupal testcommand

# This will do the process that is written in execute method.

Drupal console supports commands for a multisite installation too! We can use the drupal console commands on a multisite installation passing uri as argument.

We can also control the remote site from local with some initial setup(Check here)

Drupal Console as a debugging tool.

Debugging using Console, helps us know what actually is going on the site. Almost all the commands of drupal console have a debug option. Example:

drupal module:debug

#Lists out all the modules with status(enabled/disabled), Version of the module
drupal cron:debug

#Lists out all the modules implementing cron method.

I would say, Drupal console is the best smart tool that makes things more easier for a developer.

 


Reference:
https://drupalconsole.com/docs
https://events.drupal.org/neworleans2016/sessions/writing-command-line-tools-drupal-8-modules