Skip to content

Instantly share code, notes, and snippets.

@prashantdsala
Created March 6, 2023 10:46
Show Gist options
  • Save prashantdsala/3ab53a3573f4a2e07683bdd5506c268f to your computer and use it in GitHub Desktop.
Save prashantdsala/3ab53a3573f4a2e07683bdd5506c268f to your computer and use it in GitHub Desktop.
How to write a custom drush command in Drupal
<?php
namespace Drupal\custom_drush_command\Commands;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
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.
*
* See these files for an example of injecting Drupal services:
* - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php
* - http://cgit.drupalcode.org/devel/tree/drush.services.yml
*/
class CustomDrushCommandCommands extends DrushCommands {
/**
* Command description here.
*
* @param $arg1
* Argument description.
* @param array $options
* An associative array of options whose values come from cli, aliases, config, etc.
* @option option-name
* Description
* @usage custom_drush_command-commandName foo
* Usage description
*
* @command custom_drush_command:commandName
* @aliases foo
*/
public function commandName($arg1, $options = ['option-name' => 'default']) {
$this->logger()->success(dt('Achievement unlocked.'));
}
/**
* An example of the table output format.
*
* @param array $options An associative array of options whose values come from cli, aliases, config, etc.
*
* @field-labels
* group: Group
* token: Token
* name: Name
* @default-fields group,token,name
*
* @command custom_drush_command:token
* @aliases token
*
* @filter-default-field name
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
*/
public function token($options = ['format' => 'table']) {
$all = \Drupal::token()->getInfo();
foreach ($all['tokens'] as $group => $tokens) {
foreach ($tokens as $key => $token) {
$rows[] = [
'group' => $group,
'token' => $key,
'name' => $token['name'],
];
}
}
return new RowsOfFields($rows);
}
}
services:
custom_drush_command.commands:
class: \Drupal\custom_drush_command\Commands\CustomDrushCommandCommands
tags:
- { name: drush.command }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment