Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Create Drush 9 Commands For Drupal 8

By editor , 21 June, 2018
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

Tags

  • Drupal 8
  • Code
  • Drush
  • Add new comment
Create a custom DRUSH command on drupal 8
Useful Drush commands

Comments2

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

MichaelMay (not verified)

8 years 5 months ago

Drush 9 Commands For Drupal 8

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.

  • Reply

Anonymous (not verified)

3 years 10 months ago

Command "d9" is not defined.

Command "d9" is not defined.

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer