How to create custom Drush command in Drupal 9 | 10

Many people asked, do you know how to create custom drush command? This is very basic concept, we should know this.

So, In this tutorial se are going learn how to create custom Drush command in drupal 9 or drupal 10. For that we need to create a custom drush module. The file structure will be like below.

Create a custom module name: custom_drush inside web/module/custom/custom_drush

inside custom_drush directory create file.. custom_drush.info.yml and paste below code.

name: Custom Drush Example
package: custom
core_version_requirement: ^9 || ^10
description: This is testing Custom Drush module
type: module

Now, create custom_drush.services.yml inside custom_drush and paste below code.

services:
  custom_drush.commands:
    class: \Drupal\custom_drush\Command\CustomCommands
    tags:
      - { name: drush.command }

create src/Command directory inside custom_drush, and inside command directory create one custom controller php class and file name will be like. CustomCommands.php and paste below code.

<?php
namespace Drupal\custom_drush\Command;

use Drush\Commands\DrushCommands;


/**
 * Drush command file.
 */
class CustomCommands extends DrushCommands {

  /**
   * A custom Drush command to displays the given text.
   * 
   * @command simple-text
   * @param $text Argument with text to be printed
   * @option uppercase Uppercase the text
   * @aliases simtxt
   */
  public function printMe($text = 'Hello world!', $options = ['uppercase' => FALSE]) {
    if ($options['uppercase']) {
      $text = strtoupper($text);
    }
    $this->output()->writeln($text);
  }

}
  • @command simple-text here simple-text is full command name
  • @param $text which will be in method.
  • @option uppercase which additional option provided by this command.
  • @aliases simtxt, here simtxt is aliases we can run printMe by using drush simtxt or drush simple-text
Custom Drush simple Command.

We can write any code inside method, for your understanding… i am adding Prime Number program and Odd Even check program.

<?php
namespace Drupal\custom_drush\Command;

use Drush\Commands\DrushCommands;


/**
 * Drush command file.
 */
class CustomCommands extends DrushCommands {

  /**
   * A custom Drush command to displays the given text.
   * 
   * @command simple-text
   * @param $text Argument with text to be printed
   * @option uppercase Uppercase the text
   * @aliases simtxt
   */
  public function printMe($text = 'Hello world!', $options = ['uppercase' => FALSE]) {
    if ($options['uppercase']) {
      $text = strtoupper($text);
    }
    $this->output()->writeln($text);
  }

  
  /**
   * A custom Drush command to displays the given text.
   * 
   * @command prime
   * @param $num Argument with text to be printed
   * @option num number till prime need to be print.
   * @aliases pn
   */
  public function printPrime($num = 20, $options = ['num' => 0]) {
    if ($options['num'] > 0 ) {
    $num  =  $options['num'];
    }

    for($i = 2;$i<=$num;$i++)
    {
        $isPrime = true;
        
        for($j = 2;$j <=($i/2); $j++) 
        {
            if($i % $j == 0)
                {
                    $isPrime = false;
                }
        }
        if($isPrime == true)
        {
            $this->output()->writeln($i);
        }	
    }
  }


  /**
   * A custom Drush command to displays the given text.
   * 
   * @command odd-even
   * @param $num Argument with text to be printed
   * @option num number till prime need to be print.
   * @aliases oe
   */
  public function oddEvent($num = 30, $options = ['num' => 0]) {
    if ($options['num'] > 0 ) {
    $num  =  $options['num'];
    }

    if($num % 2 == 0){
        $this->output()->writeln($num . ' is even.');
    }else{
        $this->output()->writeln($num . ' is odd.');
    }
    
  }

}
drush create custom module drupal 8
Drush Custom Command.

You can view whole source code on GitHub

Pradip Mehta

I am a well-organized professional in Drupal Development and PHP web development with strong script handling knowledge or automation process with PHP. I have advanced computer skills and am a proficient multitasker.

This Post Has 4 Comments

  1. Abhishek

    I Love you 😘

Leave a Reply