In this tuto, I'll show you how to create custom drush commands in Drupal 8.
Create a custom module codimth_drush_command:
add codimth_drush_command/codimth_drush_command.info.yml file:
this file contain basic information for the module.
name: Codimth Drush Command
type: module
description: Create Custom Drush command
package: Codimth
core: 8.x
add codimth_drush_command/drush.services.yml file:
this file declare a service pointing to CodimthDrushCommands.php
class and tag the service with the drush.command
tag.
services:
codimth_drush_command.commands:
class: \Drupal\codimth_drush_command\Commands\CodimthDrushCommands
tags:
- { name: drush.command }
add codimth_drush_command/src/Commands/CodimthDrushCommands.php file:
this file include the entire definition for our custom command.
<?php
namespace Drupal\codimth_drush_command\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.
*
* 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 CodimthDrushCommands extends DrushCommands
{
/**
* Drush command that displays the given text.
*
* @param string $text
* Argument with message to be displayed.
* @command codimth_custom_commands:codimth
* @aliases drush-codimth codimth
* @option uppercase
* Uppercase the message.
* @usage codimth_custom_commands:codimth --uppercase text
*/
public function codimth($text = 'Hello world codimth!', $options = ['uppercase' => FALSE])
{
if ($options['uppercase']) {
$text = strtoupper($text);
}
$this->output()->writeln($text);
}
}
Some of the annotations available for use include:
@command – command definition, which needs to follow the module: command structure;
@aliases – aliases for your commands, separated with spaces;
@param – which defines the input parameters for your command;
@option – which defines the options available for your commands, which should be put in an associative array, where the name of the option is the key;
@usage – example showing how the command should be used.
How to use this custom command:
Example 1:
vendor/bin/drush codimth --uppercase "lorem ipsum"
the result: LOREM IPSUM
Example 2:
vendor/bin/drush codimth --uppercase
the result: HELLO WORLD CODIMTH!
Example 3:
vendor/bin/drush codimth
the result: Hello world codimth!
Next steps
- Clear your Drupal 8 caches. To do this I use this Drush command:
drush cr
if you don’t currently use Drush, I highly recommend using it, or the Drupal Console. - Now, enable the module, and you should be able to see the new drush command you have just created.
- I hope you found this article useful. let me know if you have any questions and I’ll be happy to answer them.
- This code can be found and downloaded from https://github.com/codimth/codimth_drush_command.