We’ve written extensively (we’re into this stuff) about how Drupal 8 has not just modernized digital experiences for the user but also eased out development work for Drupal developers. In this article we’re going to discuss two such Drupal 8/9 utility tools that have massively simplified and improved the way you work with Drupal code, modules and installations - Drupal Console and Drush.
What is Drupal Console
Since the advancement of Drupal 8, it has become necessary to add a lot of boilerplate code to build a new module. Of course, you can simply copy and paste the code, but doing so repeatedly might open doors to a lot of errors. It’s ok. We’re human. Luckily, we have Drupal console - a suite of tools that you run on a CLI (Command Line Interface) that lets you automatically generate boilerplate code. It leverages Symfony Console and other third party components to generate most of the code needed to build a Drupal 8/9 module.
What is Drush
Drush (Drupal + Shell) is the original CLI tool for Drupal. It lets you spin up new Drupal websites easily and helps you streamline development and administrative tasks. Drush comes shipped with many easy-to-implement commands that lets you interact with your Drupal installations, themes, modules and more. Some of the other tasks that you can perform with Drush are exporting/importing configurations, caching, updating contrib modules, updating the database and much more.
Drush works well with Drupal 6/7/8/9 and offers advantages of modern development practices. It helps save development time, both during migration of existing Drupal modules and while creating new ones thereby increasing productivity. Learn how to create your own custom Drush commands in this article.
Installing Drupal Console
Drupal console can be installed through various methods like composer, curl, phar etc. Here is an example of installing it using composer:
Install globally via composer
composer global require drupal/console
Site specific installation
1. Navigate to Drupal site in CLI:
cd /path/to/drupal_site
2. Execute composer require command:
composer require drupal/console:~1.0 --prefer-dist --optimize-autoloader
Installing Drush
Drush can be installed through various methods as well like composer, curl or phar. Here is an example of installing it using composer:
Install globally via composer
composer global require drush/drush
Site specific installation
1. Navigate to Drupal site in CLI:
cd /path/to/drupal_site
2. Execute composer require command:
composer require drush/drush
Drupal Console Commands and Usage
Check out some of the most popular and helpful Drupal Console commands and when they can be used.
-
Drupal Debug Command
- To identify the breakpoints while working on themes:
drupal debug:breakpoints {theme_name}
Example: drupal debug:breakpoints bartik
- To easily find the class and name of the service when working with services:
drupal debug:config:settings
- To identify the fail point when you have painful broken blocks:
drupal debug:plugin block broken
-
Generate Custom Modules
Create custom modules within a minute using this command:
drupal generate:module \
--module="modulename" \
--machine-name="modulename" \
--module-path="/modules/custom" \
--description="My Awesome Module" \
--core="8.x" \
--package="Custom" \
--module-file \
--composer \
--test \
--twigtemplate
-
Generate Custom Form
Custom forms requirements are very common across projects. Creating custom form easily with this command:
drupal generate:form \
--module="modulename" \
--class="DefaultForm" \
--form-id="default_form" \
--config-file \
--path="/modulename/form/default"
-
Generate Config Form
As like custom forms, we require administrator configured forms as well. With this command you can create them easily:
drupal generate:form:config \
--module="modulename" \
--class="DefaultForm" \
--form-id="default_form" \
--config-file \
--path="/modulename/form/default"
-
Generate Controller
With custom forms you will need controllers. You can easily create custom controllers with this command:
drupal generate:controller \
--module="modulename" \
--class="DefaultController" \
--routes='"title":"ControllerMethod",
"name":"modulename.default_controller_hello",
"method":"hello", "path":"/modulename/hello/{name}"' \
--test
-
Generate Custom Entity
Creating a custom entity does not have to be time consuming, With this command you can create it quickly and easily:
drupal generate:entity:content \
--module="modulename" \
--entity-class="DefaultEntity" \
--entity-name="default_entity" \
--base-path="/admin/structure" \
--label="Default entity" \
--is-translatable \
--revisionable
--has-forms
-
Generate Custom Service
This command helps you to create custom services:
drupal generate:service \
--module="modulename" \
--name="modulename.default" \
--class="DefaultService" \
--interface \
--interface-name="InterfaceName" \
--path-service="/modules/custom/modulename/src/"
-
Create Content like user:create
To create a user from the back-end you don’t need to open the browser and login with admin and then create an account. Simply use this command to create a user in a jiffy:
drupal user:create username password \
--roles='authenticated' \
--email="username@testing.com" \
--status="1"
-
Generate Plugin like block
Drupal Console is also capable of generating many plugins like blocks. Here is an example of creating a custom block:
drupal generate:plugin:block \
--module="modulename" \
--class="DefaultBlock" \
--label="Default block" \
--plugin-id="default_block" \
--theme-region="header" \
--inputs='"name":"inputtext", "type":"text_format", "label":"InputText", "options":"",
"description":"Just an input text", "maxlength":"", "size":"", "default_value":"",
"weight":"0", "fieldset":""'
For more Drupal console commands and usage check these Available commands.
Most Popular Drush Commands
drush cr | To rebuild Drupal cache |
drush cim | To import all the configuration files |
drush cex | To export configuration files |
drush uli | To login using one-time login url |
drush en {module_name} | Enables any module either custom or contrib or core |
drush pmu {module_name} | Disables and uninstalls any module |
drush pm:list | Lists all modules with the package, module name, version and status |
drush updb | To update the database after upgrade |
drush watchdog:list(ws) | Lists all the watchdog messages |
drush watchdog:tail(wt) | Continuous tail watchdog messages |
drush watchdog:tail --type=php | Continuous tail watchdog messages with particular severity type |
A Few More Cool Drush Commands
drush status | Provides an overview of the current Drupal installation |
drush generate | As like Drupal Console, the drush generate command helps to generate entities, plugins and more |
drush pm:security | To check Drupal composer package for security updates |
drush migrate:status | Lists all migrations with current status |
drush user:password {username} {password} | Sets user password |
drush user:block | Helps block the user |
drush user:role:add | Helps add user roles |
drush sql-dump | Exports the Drupal database as SQL using mysqldump or equivalent. |
The two command line tools in focus today - Drupal Console and Drush - are quickly becoming every Drupal developers’ favorite gear because they are great time-savers and improve productivity. Both of them are highly extensible, in that you can create custom commands catered to your requirements. If you found this article helpful, you might want to subscribe to our newsletter where you get fresh and interesting articles on Drupal, Open-Source and Technology delivered to your inbox every week!