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:
1 2 3 4 5 6 7 8 9 10 11 |
# Run this in your terminal to get the latest project version: curl https://drupalconsole.com/installer -L -o drupal.phar # Or if you don't have curl: php -r "readfile('https://drupalconsole.com/installer');" > drupal.phar # Accessing from anywhere on your system: mv drupal.phar /usr/local/bin/drupal # Apply executable permissions on the downloaded file: chmod +x /usr/local/bin/drupal |
To check if Drupal Console is working, execute drupal list
. You should see a list of commands:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
$ drupal list Drupal Console version 0.10.5 Usage: command [options] [arguments] Options: ... Available commands: ... module ... multisite ... site site:debug List all known local and remote sites. site:install Install a Drupal project site:new Create a new Drupal project theme ... yaml ... |
Downloading Drupal
Let’s start from the beginning. We can actually get any version of Drupal by running drupal site:new
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
$ drupal site:new Enter the directory name when downloading Drupal: > drupal Getting releases for Drupal Select a core release: [0 ] 8.0.2 [1 ] 8.0.1 [2 ] 8.0.0 [3 ] 8.0.0-rc4 [4 ] 8.0.0-rc3 [5 ] 8.0.0-rc2 [6 ] 8.0.0-rc1 [7 ] 8.0.0-beta16 [8 ] 8.0.0-beta15 [9 ] 8.0.0-beta14 [10] 8.0.0-beta13 [11] 8.0.0-beta12 [12] 8.0.0-beta11 [13] 8.0.0-beta10 [14] 8.0.0-beta9 > 0 Downloading drupal 8.0.2 [OK] Drupal 8.0.2 was downloaded in directory /home/ubuntu/workspace/drupal |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
$ drupal site:install Select Drupal profile to be installed: [0] Minimal [1] Standard > 1 Select language for your Drupal installation [English]: > Drupal Database type: [0] MySQL, MariaDB, Percona Server, or equivalent [1] SQLite [2] PostgreSQL > 0 Database Host [127.0.0.1]: > Database Name: > drupal Database User: > {YOUR_DATABASE_USERNAME} Database Pass [ ]: > {YOUR_DATABASE_PASSWORD} Database Port [3306]: > Database Prefix [ ]: > Provide your Drupal site name [Drupal 8 Site Install]: > Drupal Console is Awesome! Provide your Drupal site mail [admin@example.com]: > {YOUR_SITE_MAIL} Provide your Drupal administrator account name [admin]: > {YOUR_USER_NAME} Provide your Drupal administrator account mail [admin@example.com]: > {YOUR_USER_MAIL} Provide your Drupal administrator account password: > {YOUR_USER_PASSWORD} Starting Drupal 8 install process [OK] Your Drupal 8 installation was completed successfully |
Our new Drupal site should now be ready to be worked on:
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
:
1 2 3 4 5 6 |
$ drupal site:maintenance on Operating in maintenance mode on Rebuilding cache(s), wait a moment please. [OK] Done clearing cache(s). |
Now, this is what regular users will see when visiting the website:
Creating a Hello World Module
Defining Module Parameters
First let’s generate code to define the module such as the .info.yml
and the composer.json
files by running drupal generate:module
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
$ drupal generate:module // Welcome to the Drupal module generator Enter the new module name: > Hello World Enter the module machine name [hello_world]: > Enter the module Path [/modules/custom]: > Enter module description [My Awesome Module]: > Say Hello World Enter package name [Custom]: > Enter Drupal Core version [8.x]: > Do you want to generate a .module file (yes/no) [no]: > Define module as feature (yes/no) [no]: > Do you want to add a composer.json file to your module (yes/no) [yes]: > Would you like to add module dependencies (yes/no) [no]: > Do you confirm generation? (yes/no) [yes]: > Generated or updated files Site path: /home/ubuntu/workspace/drupal 1 - modules/custom/hello_world/hello_world.info.yml 2 - modules/custom/hello_world/composer.json |
Installing the Module
Again, we can just enable to module using the command line using drupal module:install {PLUGIN_MACHINE_NAME}
:
1 2 3 4 5 6 7 |
$ drupal module:install hello_world [OK] The following module(s) were installed successfully: hello_world Rebuilding cache(s), wait a moment please. [OK] Done clearing cache(s). |
Generating the Controller
Now, we need to generate a controller that will show the “Hello World” page. Do this using drupal generate:controller
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
$ drupal generate:controller // Welcome to the Drupal Controller generator Enter the module name [hello_world]: > Enter the Controller class name [DefaultController]: > HelloWorldController Enter the Controller method title (leave empty and press enter when done) [ ]: > Hello World Enter the action method name [hello]: > helloWorld Enter the route path [hello_world/hello/{name}]: > /hello/world Enter the Controller method title (leave empty and press enter when done) [ ]: > Do you want to generate a unit test class (yes/no) [yes]: > Do you want to load services from the container (yes/no) [no]: > Do you confirm generation? (yes/no) [yes]: > Generated or updated files Site path: /home/ubuntu/workspace/drupal 1 - modules/custom/hello_world/src/Controller/HelloWorldController.php 2 - modules/custom/hello_world/hello_world.routing.yml 3 - modules/custom/hello_world/Tests/Controller/HelloWorldControllerTest.php Rebuilding routes, wait a moment please [OK] Done rebuilding route(s). |
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:
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
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
$ drupal generate:plugin:block // Welcome to the Drupal Plugin Block generator Enter the module name [hello_world]: > Plugin class name [DefaultBlock]: > Plugin label [Default block]: > Plugin id [default_block]: > Theme region to render Plugin Block [ ]: > Do you want to load services from the container (yes/no) [no]: > You can add input fields to create special configurations in the block. This is optional, press enter to continue Do you want to generate a form structure? (yes/no) [yes]: > Type [ ]: > textfield Input label: > Content Input machine name [content]: > Maximum amount of characters [64]: > Width of the textfield (in characters) [64]: > Description [ ]: > Content to Display Default value [ ]: > Weight for input item [0]: > Type [ ]: > Do you confirm generation? (yes/no) [yes]: > Generated or updated files Site path: /home/ubuntu/workspace/drupal 1 - modules/custom/hello_world/src/Plugin/Block/DefaultBlock.php Rebuilding cache(s), wait a moment please. [OK] Done clearing cache(s). |
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:
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
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
$ drupal create:nodes // Welcome to the Drupal nodes generator Select content type(s) to be used on node creation: [0] Article [1] Basic page > 0 Enter how many nodes would you like to generate [10]: > Enter the maximum number of words in titles [5]: > How far back in time should the nodes be dated?: [0] N | Now [1] H | 1 hour ago [2] D | 1 day ago [3] W | 1 week ago [4] M | 1 month ago [5] Y | 1 year ago > 5 --------- -------------- ---------------------------- --------------------- Node Id Content type Title Created Time --------- -------------- ---------------------------- --------------------- 1 Article Capto Eros Haero Uxor 2016-01-18 03:23:09 2 Article Nunc Voco 2015-02-08 11:23:37 3 Article Abdo Eu Pala Plaga 2015-10-16 02:26:27 4 Article Commoveo Erat Iustum 2015-09-15 05:52:22 5 Article Olim 2015-08-13 10:50:27 6 Article Comis Damnum Ille Vulpes 2015-12-11 03:19:55 7 Article Gravis Probo 2015-04-16 07:38:44 8 Article Nobis Quidem Torqueo Zelus 2015-10-25 06:47:05 9 Article Commoveo Duis Metuo Quidne 2015-05-30 10:21:16 10 Article Illum Mos Obruo Tamen 2015-04-22 01:47:57 --------- -------------- ---------------------------- --------------------- [OK] Created 10 nodes successfully |
Now, you can see some of these articles posted to the front page with images:
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
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
$ drupal generate:theme // Welcome to the Drupal theme generator Enter the new theme name []: > New Theme Enter the module machine name [new_theme]: > Enter the theme Path [/themes/custom]: > Enter theme description [My Awesome theme]: > Plain Theme Enter package name [Other]: > Enter Drupal Core version [8.x]: > Base theme (i.e. classy, stable) [bartik]: > classy Enter the global styling library name [global-styling]: > Do you want to generate the theme regions (yes/no) [yes]: > Enter region name [Content]: > Enter region machine name [content]: > Do you want add another region (yes/no) [yes]: > no Do you want to generate the theme breakpoints (yes/no) [yes]: > Enter breakpoint name [narrow]: > Enter breakpoint label [narrow]: > Enter breakpoint media query [all and (min-width: 560px) and (max-width: 850px)]: > Enter breakpoint weight [1]: > Enter breakpoint multipliers [1x]: > Do you want to add another breakpoint (yes/no) [yes]: > no Do you confirm generation? (yes/no) [yes]: > Generated or updated files Site path: /home/ubuntu/workspace/drupal 1 - themes/custom/new_theme/new_theme.info.yml 2 - themes/custom/new_theme/new_theme.theme 3 - themes/custom/new_theme/new_theme.breakpoints.yml |
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}
:
1 2 3 4 5 6 |
$ drupal theme:install new_theme The New Theme theme has been installed successfully Rebuilding cache(s), wait a moment please. [OK] Done clearing cache(s). |
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:
Deactivating Maintenance Mode
As we are done with our development, stop maintenance mode by running drupal site:maintenance off
:
1 2 3 4 5 6 |
$ drupal site:maintenance off Operating in maintenance mode off Rebuilding cache(s), wait a moment please. [OK] Done clearing cache(s). |
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.
Excellent post! Just discovering it, and its very useful 🙂