Create Drush 9 Commands For Drupal 8 | Drupal 8

Create Drush 9 Commands For Drupal 8

Submitted by editor on Thu, 06/21/2018 - 10:48
Question

How To Write Custom Drush 9 Commands For Drupal 8 ?

Drush 9 is finally here with some much-awaited changes. Drush 9 commands are now classes and .inc files are obsolete. Drush commands will now be based on the Annotated Command format. This will change the fundamental structure of custom Drush commands. Drush 9.x is the only supported version of Drush in Drupal 8.4.x and later versions, so it is imperative to learn how to write custom Drush commands for Drush 9.

1. Create a custom module

drush9_example.info.yml

name: Drush 9 Commands Example
description: Provides examples for writing custom Drush 9 commands.
core: 8.x
type: module
package: Examples

2. Add drush service

drush.services.yml

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

3. Include Drush 9 classes

For that, you must create / Complete your composer file like:

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

4. Implement drush command

Drush9ExampleCommands.php

<?php
namespace Drupal\drush9_example\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 drush9_example: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 drush9_example: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.

For a complete list of all the hooks available and their usage, please refer to: https://github.com/consolidation/annotated-command

 

Thanks to : https://www.axelerant.com/resources/team-blog/how-to-write-custom-drush-9-commands-for-drupal-8

Comments

MichaelMay (not verified)

Mon, 07/02/2018 - 10:20

I really affected on your writing. Your post is incredibly little however everything is incredibly open and extremely clear rationalization of topic. It contains really info; your web site is incredibly helpful. Thanks for sharing; I’m wanting forward to additional posts.    Refer buy essays writing service to get perfect writing ideas.

MelissaRedick (not verified)

Tue, 07/17/2018 - 05:59

I am very happy to read this writing. I got more information about drupal by reading this. It is very helpful writing.This is very useful website and every thing explained in cleared manner so every one can understand this site and this is yours plus point.Thank you for sharing this valuable information.And i am a regular reader of your site.I am also a writer and doing work at  one of the best Custom essay writing service 

 

Add new comment