Drupal Console: An Overview of the New Drupal CLI
Drupal Console is the new CLI (Command Line Interface) for Drupal. This tool can help you to generate boilerplate code, as well as interact with, and debug Drupal 8. From the ground up, it is built to utilize the same modern PHP practices that have been adopted in Drupal 8.
Drupal Console takes advantage of the Symfony Console and other well-known third-party components like Twig, Guzzle, and Dependency Injection among others. By embracing those standard components, we’re fully participating in the PHP community, building bridges and encouraging the PHP community to join the Drupal project and allow us to reduce the isolation of Drupal.
Why is Drupal Console important?
Drupal is infamous for having a steep learning curve, complete with its own language of “Drupalisms”. While Drupal 8 simplifies and standardizes the development process, it is more technically advanced and complex than its predecessor.
Managing the increasing complexity of Drupal 8 could be a daunting task for anyone. Drupal Console has been designed to help you manage that complexity, facilitating Drupal 8 adoption while making development and interaction more efficient and enjoyable. Drupal Console was created with one goal in mind: to allow individuals and teams to develop smarter and faster on Drupal 8.
Drupal Console features
In this blog post, I will mention some of the most common features and commands of Drupal Console, to serve as a good introduction.
Install Drupal Console
[gist-embed data-gist-id="3c20c6acb6efd882e0295ff65db3d7d4" data-gist-file="install-drupal-console.sh" data-gist-hide-line-numbers="true" data-gist-show-loading="false"]
Copy configuration files
The init command copy application configuration files to the user home directory. Modifying this configuration files is how the behavior of the application can be modified.
Validate system requirements
The check command will verify the system requirements and throw error messages if any required extension is missing.
Install Drupal 8
The easiest way to try Drupal 8 on your local machine is by executing the chain command and pass the option --file=~/.console/chain/quick-start.yml
The chain command helps you to automate command execution, allowing you to define an external YAML file containing the definition name, options, and arguments of several commands and execute that list based on the sequence defined in the file.
In this example, the chain command will download and install Drupal using SQLite, and finally, start the PHP's built- in server. Now you only need to open your browser and point it to 127.0.0.1:8088.
Generate a module
The generate:module command helps you to:
- Generate a new module, including a new directory named hello_world at modules/custom directory.
- Creates a hello_world.info.yml file at modules/custom/hello_world directory.
Generate a service
The generate:service command helps you to:
- Generate a new service class and register it with the hello_world.services.yml file.
Generate a Controller
The generate:controller command helps you to:
- Generate a new HelloController Class with a hello method at src/Controller directory.
- Generate a route with a path to /hello/{name} at hello_world.routing.yml file.
Generate a Configuration Form
The generate:form:config command helps you to:
- Generate a SettingsForm.php class at src/Form directory,
- Generate a route with path to /admin/config/hello_world/settings at hello_world.routing.yml
- Register at the hello_world.links.menu.yml file the hello_world.settings_form route using system.admin_config_system as parent.
This command allows you to add a form structure to include form fields based on the field API. Also generates a buildForm and submitForm methods with the required code to store and retrieve form values from the configuration system.
NOTE: The parent route system.admin_config_system for the menu_link can be selected from the command interaction.
Debug Services
The container:debug command displays the currently registered services for an application. Drupal contains several services registered out-of-the-box plus the services added by custom and contributed modules, for that reason I will use peco a simplistic interactive filtering tool to make this debug easier.
Debug Routes
The router:debug command displays the currently registered routes for an application. Similar to debugging services. In this example, I will use peco to make this debugging task easier.
Create Data
The create:nodes command creates dummy nodes for your application.
Drupal Console provides a YAML to execute using the chain command. This file contains instructions to execute create:users, create:vocabularies, create:terms and create:nodes command using one command.
[gist-embed data-gist-id="3c20c6acb6efd882e0295ff65db3d7d4" data-gist-file="drupal-chain-create-data.sh" data-gist-hide-line-numbers="true" data-gist-show-loading="false"]
Modify the Drupal Console language
The settings:set command helps to change the application configuration in this example using the arguments language es we can set Spanish as the application language. After switching the default language the interface is translated.
All of the available commands
The list command can be used to show all of the available commands. A print screen was not included because the more that 130 commands make the image huge to show on this blog post.
For the full list of commands, you can visit the documentation page at http://docs.drupalconsole.com/en/commands/available-commands.html
What makes Drupal Console unique
- Has been built to utilize the same modern PHP practices adopted by Drupal 8.
- Generates the code and files required by Drupal 8 modules and components.
- Facilitate Drupal 8 adoption while making development and interaction more efficient and enjoyable.
- Allow individuals and teams to develop smarter and faster on Drupal 8.
- Help developers to understand Drupal 8 with the "--learning" flag.
- Fully multilingual and translatable, just like Drupal 8 itself.