Blog Entry

Steps to Create a Custom Drush Command

Image
drupal drush

The name Drush means “The Drupal Shell.” Drush is a command line interface for Drupal.

Drush is useful for developers to execute certain operation through terminal without depending on the user interface. As a Drupal developer every one will know what drush is and we are using Drush command in our day to day life in development and we commonly use “drush cc all” for clearing the Drupal cache.

How about creating our own drush that help us in development?

Creating your own new Drush command is very simple.

Following are the steps
1)    Create a command file called MODULENAME.drush.inc
2)    Implement the hook function MODULENAME_drush_command()
3)    Implement the callback functions that when ever the command will be called. These will be named as drush_MODULENAME_COMMANDNAME().

Step 1: Create a Drush command file [.drush.inc]

If you want to use Drush command for specific module in Drupal you can create the Drush file in the module folder it self.

Image
blog

In this example my module name is update_company and the Drush file name can be “update_company.drush.inc”. If Drush is installed and configured it will scan this file and

You command is available in project and if you want to install the command for over all the project you can place the ”.drush.inc ” in the home .drush folder and also Drush searches for the below location for your drush command file

Drush searches for command files in the below locations:

  • Folders listed in the 'include' option [type drush topic docs-configuration].
  • The system-wide global Drush commands directory, Example  /usr/share/drush/commands
  • In the ".drush" folder in the user's HOME directory.
  • /drush and /sites/all/drush in the current Drupal installation


Note that modules in the current Drupal installation will only be considered if Drush has bootstrapped to at least the DRUSH_BOOSTRAP_SITE level. Usually, when working with a Drupal site, Drush will bootstrap to DRUSH_BOOTSTRAP_FULL; in this scenario, only the command files in enabled modules will be considered eligible for loading. If Drush only bootstraps to DRUSH_BOOTSTRAP_SITE, though, then all Drush command files will be considered, whether the module is enabled or not. Check drush topic docs-bootstrap for more info on Drush bootstrapping.

  • All enabled modules in the current Drupal installation -We are placing the Command file in this place
  • Folders and files containing other versions of Drush will be *skipped* (e.g. Devel.drush5.inc or drush5/devel.drush.inc). Names containing the current version of Drush (e.g. Devel.drush7.inc) will be loaded.


The Drush command file may be used for a particular version of Drupal by adding a Drupal version as a extension ".dVERSION" VERSION here says the numeric base version of Drupal after the name of the Drush command file (e.g. ".d8.drush.inc"). This Drush command file will be called only for the Drupal 8 installation file

Step 2: Implement the hook_drush_command()

The drush_command hook is the important part of the Drush command file. It return the array of items that define how command should work. It is similar to the hook_menu() structure in Drupal. The Items Array define the command by its attribute

function update_company_drush_command() {
   // Company update Dynamically
   $items['update-company-list'] = array(
    'description' => dt('Update the Company list Dynamically.'),
    'aliases' => array('ucl'),
    'examples' => array(
              'drush update-company-list' => 'Make a terrible-tasting sandwich that is lacking in pickles.',
    ),
  );

Here the Drush command “update-company-list” is created and the 'examples' is also create so when ever the user type

drush help update-company-list

The below example will be shown to the user,

Image
blog

'aliases'
Is the shortest form of the Drush command. For example instead of typing

drush help update-company-list

We can use,

drush help ucl to execute the same command.
 
'description'
Description is the short info about the command

Step 3: Implement the callback functions for the command

The 'update_company_list' command in update_company.drush.inc is defined as follows:

/**
* Update the Company list Dynamically.
*/
function drush_update_company_update_company_list() {
//Confirmation will be Y/N when use type “y” the condition will be executed if not it will not
if (drush_confirm(dt("Are you sure you want to update?"))) {  
drush_print("Update started.....");
//Do your code stuff here
drush_print("Update completed succesfully");//Print in the terminal if success
}
}


The function name is computed in such a way that drush_MODULENAME_COMMANDNAME()
Drush will automatically detect the callback of this function based on the function name.

We can also write a callback function to call a particular function.

callback: Name of function to invoke for this command. The callback function name must begin with "drush_commandfile_", where commandfile is from the file "commandfile.drush.inc", which contains the commandfile_drush_command() function that returned this command.

Note that the callback entry is optional; it is preferable not to use it, in which case drush_invoke() will generate the hook function name.

function update_company_drush_command() {
   // Company update Dynamically
   $items['update-company-list'] = array(
    'description' => dt('Update the Company list Dynamically.'),
'aliases' => array('ucl'),
     'callback'=>'drush_callback_fun',
    'examples' => array(
              'drush update-company-list' => 'Update the Company list Dynamically.',
    ),
  );

function drush_callback_fun(){
//Your code here
}

Hope this blog helps:)

Do you have any queries? Drupal Geeks can be reached out @ 312-340-7112 or send an email to [email protected]