Home >Backend Development >PHP Tutorial >Drupal: How to Create Your Own Drush Command

Drupal: How to Create Your Own Drush Command

William Shakespeare
William ShakespeareOriginal
2025-02-21 10:50:12610browse

Drupal: How to Create Your Own Drush Command

Key Concepts

  • Drush Extensibility: Drush, Drupal's command-line interface, allows extending its functionality with custom commands defined within modules. This enables automating specific Drupal operations.
  • Command Structure: Creating a Drush command involves a .drush.inc file (within your module), the hook_drush_command() function for command definition, and a callback function to execute the command's logic.
  • Arguments and Options: Commands can accept mandatory arguments and optional options, enhancing customization. Arguments are passed as function parameters, while options are retrieved using drush_get_option().
  • Interactive Input and Refinements: Commands can request user input, provide usage examples, and specify dependencies (Drupal bootstrap level, modules, core version).

Drush streamlines Drupal management from the terminal. While offering pre-built commands (module download, enable, update), its true power lies in creating custom commands. This tutorial demonstrates building a Drush command for a simple, illustrative module. The focus is on Drush's command structure, not the module's core functionality. Example code is available in [this repository](repository_link_here - replace with actual link if available).

Our Sample Module

The demo_drush module's functionality is a basic function:

<code class="language-php">function demo_drush_print_statement() {
  drupal_set_message(t('Hello world!'));
}</code>

This function will be used to demonstrate how Drush prints messages to the console. We'll modify it later to showcase various command features.

The Drush Command File (demo_drush.drush.inc)

Create demo_drush.drush.inc within your module's directory. Drush identifies and loads functions from files ending in .drush.inc.

Command Hook and Callback

Drush command architecture comprises two main parts: the hook_drush_command() implementation (defining commands and configurations) and callback functions triggered by commands. Let's start with hook_drush_command():

<code class="language-php">/**
 * Implements hook_drush_command().
 */
function drush_demo_drush_command() {
  $items['drush-demo-command'] = array(
    'description' => 'Demonstrates Drush command functionality.',
    'aliases' => array('ddc'),
  );
  return $items;
}</code>

This defines a command named drush-demo-command (aliased as ddc). The callback function (by default, drush_drush_demo_command()) executes the command's logic:

<code class="language-php">/**
 * Callback for the drush-demo-command command.
 */
function drush_drush_demo_command() {
  demo_drush_print_statement();
}</code>

After clearing the Drush cache (drush cc drush), running drush ddc prints "Hello world!" to the console.

Arguments and Options

Enhance your command with arguments (mandatory) and options (optional). Let's add them to the hook:

<code class="language-php">function demo_drush_print_statement() {
  drupal_set_message(t('Hello world!'));
}</code>

Now, drush ddc error --repeat=10 sets the statement type to "error" and repeats it 10 times. Update demo_drush_print_statement() and the callback function accordingly:

<code class="language-php">/**
 * Implements hook_drush_command().
 */
function drush_demo_drush_command() {
  $items['drush-demo-command'] = array(
    'description' => 'Demonstrates Drush command functionality.',
    'aliases' => array('ddc'),
  );
  return $items;
}</code>

User Input

Handle missing arguments interactively:

<code class="language-php">/**
 * Callback for the drush-demo-command command.
 */
function drush_drush_demo_command() {
  demo_drush_print_statement();
}</code>

Examples in hook_drush_command()

Add examples to the hook:

<code class="language-php">...
'arguments' => array(
  'type' => 'Statement type (error or success).',
),
'options' => array(
  'repeat' => 'Number of statement repeats.',
),
...</code>

Conclusion

This tutorial covers the basics of creating Drush commands. Explore advanced features like bootstrap levels, module dependencies, and core version compatibility in the Drush API documentation.

Frequently Asked Questions (FAQs) (These are already well-written in the input, no need to rewrite them)

The above is the detailed content of Drupal: How to Create Your Own Drush Command. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn