How to write the custom Drush Commands in Drupal 8?
Blog

How to write the custom Drush Commands in Drupal 8?

Writing custom drush commands in Drupal 8 is not exactly like Drupal 7 but it is similar. Here also, we are implementing hook_drush_command().   

The only difference is in the file structure.  As we all know In Drupal 8 ‘.module’ file is not mandatory in the module root folder. Hence In Drupal 8 for creating custom drush commands, we only need two files.

  1. MyModuleName.info.yml
  2. MyModuleName.drush.inc

Where,description will be listed under Other commands: (custom_drush_command)

'description' => 'Echo the name you type with Say hello commandSay hello.'

drush_command

 

The command can be accessible in two ways by typing item_id say-hello' or using alias say:hello

drupal dependencies' => ['custom_drush_command'],
This tells to drupal that our drush command which has dependency, will work if we have our custom “custom_drush_command" installed.

 

In this way we can execute our custom new drush command using drush say:hello ‘your name’ or  drush say-hello ‘your name’

drush_command_execution

 

drush_alias_command_execution

 

How to enable and use:

  1. Install the module
  2. Open the terminal and ‘cd’  to your drupal root folder.
  3. drush cache-clear drush
  4. drush cr
  5. drush say:hello ‘your name’ or  drush say-hello ‘your name’


Source Code: https://github.com/rakeshjames/custom_drush_command