Sunday, January 16, 2022

How to created the Drush command in drupal 8 and 9.

 

Step 1. Create a custom module 

custom_module.info.yml

name: custom-module
type: module
description: Provides examples for writing custom Drush 9 commands.
core: 8.x
core_version_requirement: ^8 || ^9

Step 2. Add drush service

drush.services.yml

services:
  custom_module.commands:
    class: \Drupal\custom_module\Commands\Drush9ExampleCommands
    tags:
      - { name: drush.command }

Step 3.  Include Drush 9 classes
For that, you must create / Complete your composer file like:

composer.json

{
    "name": "drupal/custom_module",
    "description": "A example module for Drush 9 commands.",
    "type": "drupal-module",
    "autoload": {
      "psr-4": {
        "Drupal\\custom_module\\": "src/"
      }
    },
    "extra": {
      "drush": {
        "services": {
          "drush.services.yml": "^9"
        }
      }
    }
  }

Step 4. Implement drush command 

src/Commands/Drush9ExampleCommands.php

<?php
namespace Drupal\custom_module\Commands;
use Drush\Commands\DrushCommands;
/**
 * A Drush commandfile.
 *
 * In addition to this file, you need a drush.services.yml
 * in root of your module, and a composer.json file that provides the name
 * of the services file to use.
 */
class Drush9ExampleCommands extends DrushCommands {
  /**
   * Echos back hello with the argument provided.
   *
   * @param string $name
   *   Argument provided to the drush command.
   *
   * @command custom_module:hello
   * @aliases d9-hello
   * @options arr An option that takes multiple values.
   * @options msg Whether or not an extra message should be displayed to the user.
   * @usage custom_module:hello akanksha --msg
   *   Display 'Hello Akanksha!' and a message.
   */
  public function hello($name, $options = ['msg' => FALSE]) {
    if ($options['msg']) {
      $this->output()->writeln('Hello ' . $name . '! This is your first Drush 9 command.');
    }
    else {
      $this->output()->writeln('Hello ' . $name . '!');
    }
  }
}


This file uses the Annotated method for commands, which means that each command is now a separate function with annotations to define its name, alias, arguments, etc. This file can can also be used to define hooks with @hook annotation. Some of the annotations available for use are:

@command: This annotation is used to define the Drush command. Make sure that you follow Symfony’s module:command structure for all your commands.

@aliases: An alias for your command.

@param: Defines the input parameters. For example, @param: integer $number

@option: Defines the options available for the commands. This should be an associative array where the name of the option is the key and the value could be - false, true, string, InputOption::VALUE_REQUIRED, InputOption::VALUE_OPTIONAL or an empty array.

@default: Defines the default value for options.

@usage: Demonstrates how the command should be used. For example, @usage: mymodule:command --option

@hook: Defines a hook to be fired. The default format is @hook type target, where type determines when the hook is called and target determines where the hook is called.

 

Step 5: Click me to Download

No comments:

Post a Comment

If you have any problem please let me know.