Drush and Drupal Console: Test-Drive

Drush and Drupal Console: Test-Drive

3 min read

Send to you:

Drush and Drupal Console are the utilities with a command line interface. Both of them drastically speed up website administration and development. After the installation, you’ll have an ability to take the specific actions just entering the command into the terminal window, while the same actions could require several steps in a browser, for example.

Drupal Console works only with Drupal 8, and Drush works with Drupal 6, 7, 8.
We think you should know both of these tools if you will work with Drupal 8.

Comparing Drush and Drupal Console

What can we do using these tools? – Look through the list below.

With Drush you can:

  • Download and install Drupal and contributed modules;
  • Update Drupal and contributed modules;
  • Run updates;
  • Clean cash;
  • Run CRON and Drupal on a light web server;
  • Import, export, and merge configurations;
  • Add users and set up the roles for them and access permission for roles;
  • Make Drupal backup and recovery;
  • Copy database and files to the remote server;
  • Make a copy of twig templates.

With Drupal Console you can:

  • Generate code for:
    — Console commands;
    — Content types;
    — Controllers;
    — Entities;
    — Form alter hooks;
    — Fields types, field widgets, and output widgets.;
    — Images effects;
    — Rest resources;
    — Services;
    — Themes;
  • Switch the website to the maintenance mode and back;
    Run unit tests.

Hope that you are interested and ready to start working. We think that Linux and Mac OS users don’t need telling about terms. But as for Windows users, we’ll recommend them to use Git Bush instead of a standard console. You can download Git for Windows here.This article will be useful for those who has the skills of working with the console. So let’s open our console!

Working with Drush and Drupal Console

Drush and Drupal Console are very similar, but they are two different packages. Therefore they should be installed separately:

  • Here is a guideline on Drush installation.
  • And here is a guideline on Drupal Console installation.

After the installation of packages, make sure that you did everything correctly. Enter Drush version and Drupal version in a console for this. Then you’ll see something like the information showed below. The numbers of versions could be different in your system.

<div class="half-left">
  <p>$ drush version<br>Drush Version : 8.0-dev</p>

Drupal Console

$ drupal --version
Drupal Console version 0.9.1

Now let’s show the power of Drush in Drupal Console using them together. First of all, we’ll use Drush for Drupal 8 download, installation, and run via pm-download (dl) and quick-drupal (qd) commands.

$ drush dl drupal-8.0.x
Project drupal (8.0.x-dev) downloaded to /Users/ga/tmp/intro-drush/drupal-8.0.x-dev.
Project drupal contains:
- 1 profile: standard
- 13 themes: 
- 62 modules: 
$ cd drupal-8.0.x
$ drush qd --use-existing --uri=http://localhost:8383 --profile=standard
You are about to DROP all tables in your ‘…/drupal-8.0.x-dev.sqlite’ database. Do you want to continue? (y/n): y
Starting Drupal installation. This takes a while. Consider using the --notify global option.
Installation complete.  User name: admin  User password: 
Congratulations, you installed Drupal!
Caching 'uri' localhost:8383 in /drush/drushrc.php
HTTP server listening on localhost, port 8383 (see http://localhost:8383/), serving site localhost:8383, logged in as admin...
PHP 5.6.12 Development Server started at Thu Sep 24 15:02:53 2015
Listening on http://localhost:8383
Document root is /drupal-8.0.x-dev
Press Ctrl-C to quit.

After you have seen the message in the terminal shown above, it should be a little pause, and the browser will open the just installed Drupal website in few seconds. Quickly and simply!

Then we’ll use Drupal Console for creating the module for Drupal 8. Type the commands shown below in the terminal.

$ drupal generate:module
Welcome to the Drupal module generator
Enter the new module name: hello
Enter the module machine name [hello]: hello
Enter the module Path [/modules/custom]: /modules/custom
Enter module description [My Awesome Module]: Hello World
Enter package name [Other]: Other
Enter Drupal Core version [8.x]: 8.x
Do you want to add a composer.json file to your module [no]? yes
Would you like to add module dependencies [no]? no
Do you confirm generation [yes]? yes
Generated or updated files
Site path: /drupal-8.0.x-dev
1 - modules/custom/bello/bello.info.yml
2 - modules/custom/bello/bello.module

Perhaps, you thought that you knew how to develop modules for Drupal 8, didn’t you? In any case, you have just created module via Drupal Console using only one single command. It’s cool, but the module lacks its own controller. And the module without the controller in Drupal 8 is just like a module without hook_menu() for Drupal 7. So, let’s go on and make our module do something. We are going to use the features of Drupal Console again:

$ drupal generate:controller
Welcome to the Drupal Controller generator
Enter the module name: hello
Enter the Controller class name [DefaultController]: HelloController
Controller title: Hello World
Enter the action method name [index]: greet
Enter the route path [hello/greet/{param_1}/{param_2}]: hello/greet/{world}
Controller title (empty to start with code generation):
Do you want to generate a unit test class [yes]? yes
Do you want to load services from the container [no]? no
Do you confirm generation [yes]? yes
Generated or updated files
Site path: /drupal-8.0.x-dev
1 - modules/custom/hello/src/Controller/HelloController.php
2 - modules/custom/hello/hello.routing.yml
3 - modules/custom/hello/Tests/Controller/HelloControllerTest.php
[ ] Rebuilding routes, wait a moment please
[ ] Done rebuilding route(s).

Controller generation successfully completed. Let’s look what it generated for us:

$ tree modules/custom/
??? hello
   ??? Tests
   ?   ??? Controller
   ?   	??? HelloControllerTest.php
   ??? composer.json
   ??? hello.info.yml
   ??? hello.module
   ??? hello.routing.yml
   ??? src
       ??? Controller
           ??? HelloController.php

Open HelloController.php file in your favorite editor and replace the things transmitted to a function t() with the data shown in the example below.

class HelloController extends ControllerBase {
  * Greet.
  * @return string
  *   Return Hello string.
 public function greet($world) {
   return [
       '#type' => 'markup',
       '#markup' => $this->t('Hello, @world!', array('@world' => $world))

Now it looks like the right Hello World module. Let’s turn it on using Drush.

$ drush en hello
The following extensions will be enabled: hello
Do you really want to continue? (y/n): y
hello was enabled successfully.

As you can see, it’s pretty simple to manage Drupal and generate code using Drush and Drupal Console. And these powerful tools can become your best friends while working on Drupal 8. If you need any help on Drupal development, we’ll do these works for you with great pleasure. Just fill “Ask Questions” form below, and we’ll contact you.


Article rating (1 Reviews)

Do you find this article useful? Please, let us know your opinion and rate the post!

  • Not bad
  • Good
  • Very Good
  • Great
  • Awesome