How to create a custom Drush command in Drupal

By ankit_debnath
Create a custom Drush command in Drupal

Drush, often known as "Drupal Shell," is a sophisticated command-line interface (CLI) application built exclusively for managing and administering Drupal websites. It allows developers and site administrators to do a variety of activities from the command line, significantly speeding up workflows. Drush commands allow you to do typical site maintenance activities such as module installation and uninstallation, cache clearing, configuration export and import, database changes, and much more—all without having to enter into the Drupal admin interface.

This tool is especially useful in circumstances when several repetitive activities must be completed, as it simplifies complex procedures into single commands, lowering the danger of human error. Whether you work on a large-scale Drupal project or a smaller site, Drupal Drush streamlines administrative duties.

In this guide, we will walk through how to extend Drush by creating custom Drush commands tailored to your specific project needs, allowing you to automate even more tasks and further improve your Drupal development workflow.

Creating the Drush Command

Step 1:
Create a custom module by adding a folder in modules/custom/. Inside this folder, create an .info.yml file to define the module details.

Step 2:
Create a drush.services.yml file in the module's root directory to register the custom Drush command service.

Step 3:
Create a command class in the src/Commands/ directory. In this class, define the logic for your custom Drush command.

Step 4:
Enable the custom module by running the Drush command to enable it.

The folder structure for creating the custom Drush command will be like this -

Drush command

The image depicts the folder structure of a custom Drush command module in Drupal. Let me explain the purpose of each file and why this structure is used:

1.custom_drush.info.yml
This file provides metadata about the custom module, such as its name, description, dependencies, and version. Drupal requires this file to register the module properly in the system. Without the .info.yml file, Drupal wouldn’t recognize this as a valid module.

2.drush.services.yml
This file defines the service related to the Drush command. It registers the class that contains the custom Drush commands, allowing Drush to find and execute the commands when invoked. Services in Drupal are used to manage reusable components, and this file helps tie the custom Drush command to the Drupal service container.

3.src/Commands/CustomCommands.php
This is the core PHP class where the actual custom Drush commands are defined. The class contains methods that implement the commands and their logic. Each method can have annotations like @command and @aliases to define the command name and its shorthand.

The folder structure is essential to maintain Drupal’s standards for organizing code. The src/Commands directory follows Drupal's PSR-4 autoloading standards, which helps Drupal locate the classes that contain the logic for the custom Drush commands. By using this structure, you ensure that your module is easily maintainable, follows best practices, and integrates seamlessly with Drupal’s ecosystem.

First, we need to create a custom module to write the Drush command.

create a custom module

To write the Drush command, we need to create a drush.services.yml file.

In that file, we need to add the following content:

drush.services.yml

To create a custom services.yml file at first we need to write the services under which we need to add the service key (here custom_drush.custom_command).

1.Class - Here we have to write the class name where we have defined our custom Drush command.

2.Arguments - When we need to use some services we can add it to arguments and then define them in the constructor. For reference, see the below picture.

3.Tags - It is used to indicate that a service should be registered or used in some special way, or that it belongs to a category. Here, we are creating a custom Drush command, so we mentioned drush.command. In case of custom event subscribers, we need to write event_subscriber.

Then we need to create this class (Here it is CustomCommands).

CustomCommands

In the class, we are fetching the total count of nodes in the site.

The annotations use in the getNodeCount() function are :

1.@command: This is the Drush command we need to run to call this function and get the result.

2.@aliases: As the name suggests, these are the aliases of the Drush command, which we can use to call the function instead of writing the whole command. You can also use multiple aliases separated by commas.

Executing the command

Now, the custom Drush command is ready. When you type Drush in your terminal, you can see that the custom Drush command is listed alongside other Drush commands.

If it's not getting listed, make sure that the module is enabled.

Now, run drush node-count or drush nc (which is the alias).

run drush node-count or drush nc 

Conclusion

Creating custom Drush commands increases flexibility and automation while administering a Drupal site. Following the methods provided in this article will allow you to customize Drush's capabilities to match the specific requirements of your project. This is especially beneficial for repetitive procedures or processes that are difficult to accomplish via the user interface. Custom Drush commands not only save time, but also ensure that actions are performed consistently across several contexts. As you become more experienced with Drupal Drush, you will be able to construct more complicated commands, thereby improving your Drupal development workflow.

Share this blog:

Profile picture for user ankit_debnath
ankit_debnath
I am a Drupal developer with expertise in custom module development, performance optimization, and integrating third-party services.