Create a Drush command for your Drupal module

Source: Internet
Author: User
Tags drupal

Drush is an essential tool for every Drupal developer. I have also written other articles about Drush. You can search for "drush" in the search box in the upper-right corner of the website ". The previous articles are basically introductory, such as how to use drush to clear the cache and set the default topic. Today, let's take a look at it and integrate drush into our own modules. Isn't it great to customize the operations you need and complete several lines of commands ?! Let's take a look at how to integrate drush into the module.

Initialize a new module (or an existing module ). Assume that the name of the named module is drush demo.

Next, add the following code to drush_demo.module:

/**
* Example function.
*/

Function demo_drush_print_statement ($ type = NULL ){
Drupal_set_message (t ('Hello world! '), $ Type );
}

The above code is like every new programming language, Hello World!

Next, create a drush_demo.drush.inc file, which is also the most important file. The naming rules follow modulename. drush. inc. Remember to add <? Php.

Before writing the drush_demo.drush.inc file, add the hook: hook_drush_command. The function of the hook is to declare a new drush command. Then we define a simple drush command: drush-demo-command. At the same time, give it a ddc for short. The following code:

/**
* Implements hook_drush_command ().
*/
Function drush_demo_drush_command (){

$ Items ['drush-demo-command'] = array (
'Description' => 'demonstrate how Drush commands work .',
'Aliases' => array ('ddc '),
'Arguments' => array (
'Type' => 'The type of statement (error or success ).',
),
'Options' => array (
'Repeat' => 'The number of statement repeats .',
),
'Examples '=> array (
'Drush ddc error' => 'Prints the statement once with the error flag .',
'Drush ddc success -- repeat = 10' => 'Prints the statement 10 times with the success flag .',
),
);

Return $ items;
}

The second part of the drush_demo.drush.inc file is the drush callback function. It usually starts with drush and is followed by a line to link the remaining part. The rest is from the items in the hook_drush_command hook above, for example, drush-demo-command. In combination, the callback function is named drush_drush_demo_command (). The callback function is written as follows:

/**
* Callback for the drush-demo-command
*/
Function drush_drush_demo_command ($ type = FALSE ){

// Check for existence of argument
If (! $ Type ){
$ Options = array (
'Success' => dt ('success '),
'Error' => dt ('error '),
);
$ Type = drush_choice ($ options, dt ('What kind of message you \ 'd like to print? '));
  }

// Check for correct argument
$ Correct_args = array ('error', 'success ');
If (! In_array ($ type, $ correct_args )){
Return drush_set_error (dt ('"@ type" is not a valid statement type. please choose between "success" and "error ". ', array (' @ type' => $ type )));
  }

// Option
$ Repeat = drush_get_option ('repeat', 1 );
If ($ repeat> 1 & is_numeric ($ repeat )){
For ($ I = 0; $ I <$ repeat; $ I ++ ){
Demo_drush_print_statement ($ type );
    }
  }
Else {
Demo_drush_print_statement ($ type );
  }

}

The above is the basic command used to integrate drush into the module. Use the drush cc drush command to clear the drush cache. Try your command: drush ddc!

PS: If you want to do more development, refer to the drush api and the drush official website documentation. Please refer to the article content!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.