Specbee: An Easy Step-by-Step Guide to Writing Your Own Custom Drush 9 (and 10) Commands

An Easy Step-by-Step Guide to Writing Your Own Custom Drush 9 (and 10) Commands
Santhosh Kumar
16 Sep, 2021

If you’re a Drupal professional, Drush needs no introduction. But if you’re just starting out as a Drupal developer or keen on learning about Drupal, you should know that Drush is something you just CANNOT ignore. Short for “Drupal + Shell”, Drush is a Command Line Interface (CLI) tool made exclusively for Drupal. With Drush, you can set up new Drupal websites quickly and easily work with the Drupal installations. It helps you streamline your development and administrative tasks thus improving your productivity.

Drush 9 and Drush 10 core comes shipped with tons of helpful commands that helps you interact with themes, modules, profiles, etc. Some useful commands include – SQL commands, exporting or importing configurations, updates, migrations, cron or clear cache, and much more. It is also extremely extensible, in that you can create your own custom commands. In this article, you will find more information about how to create custom Drush 9 and 10 commands in an easy step-by-step process. 

Note: Drupal 8.4 and higher support Drush 9, while Drupal 8.8 and above support Drush 10.
Custom Drush Command

Drush and Drupal Console

Although Drush and Drupal Console are both super useful CLI tools that make developers’ work easier, they are often used in different scenarios. With Drupal Console being the most recent addition to the Drupal world, it is a very useful tool for new developers to cope up with Drupal’s infamous learning curve. However, our take would be to use both Drush and Drupal Console as when put together they can do so much more in speeding up development and boosting productivity. 

While Drupal Console lets you create custom modules, services, entities, boilerplate content, debugging and more, Drush lets you perform more basic but foundational tasks. These tasks include installing Drupal, interacting with the installation, exporting and importing configurations, download and update contributed modules, Caching, update the database, run cron jobs and much more. For more details on each of their features, please refer to this guide.

When would we need to Create Custom Drush commands?

Although there are many Drush 9 commands that are ready to use for various functionalities, there are always times when they’re not adequate. We create custom Drush commands for many database related processes like entity field value updates, DB updates, importing or exporting data to and from Drupal, and other bulk processes. Also, when we have secure methods to be called, we can opt for Drush command implementation. 

How to Create a Custom Drush Command

The previous versions of Drush the implementation of Drush custom code followed different models. It was based on the hook_drush_command() and made use of .inc files.

In Drush 9, we will no longer use the .inc files or the hook_drush_command(). Drush commands will now be based on Annotated command format. This changed the fundamental structure of custom Drush commands.

Step 1: Create a module

Create a .info.yml file
Custom Drush

 

Step 2: Create a service using services.yml

Create a .services.yml file
Services YML

 

Step 3: Create a Drush service class

Now let’s extend the DrushCommands base class.
Use Drush 

Path to the class file should be: 

/src/Commands/BatchCommands.php

Batch Command 

Under this class, each method can be a command function if it is properly annotated.

Step 4: Create annotated methods

Print Info 

Here are a few of the common annotated commands:

@option 
@usage 
@command 
@param 
@process
@aliases 
@status
@extract

Step 5: Clear cache

Now let’s clear the cache to flush all caches with this command:

drush cr

Start using the new custom command you just created:

drush custom-message 

Custom Message 

Note: Multiple options added, as options is an array value.

Shefali ShettyApr 05, 2017

 


Go to Source
Author: