Importance of drush in drupal projects
Most of the drupal developers know drush . But still I am going to briefly explain the importance of drush in drupal projects . Drush is a command line shell and Unix scripting interface for Drupal. Drush core by default contains lots of useful commands for interacting with drupal modules/themes/profiles. Similarly, it runs update.php, executes sql queries and DB migrations, and misc utilities like run cron or clear cache. Drush also allow the developers to define their custom drush commands . These custom command can be created based on project needs. I had a scenario in one of my old project that we have to import some large data file into drupal database on regular basis . Most of the people can think about the feed module to achieve that functionality . But due to some complex requirement and complex file structure we have decided that lets write our own import logic and execute that import with custom drush command and set that command as cron job . That’s just one example but as per your project need you can define custom drush command as much as you required.
Below is the simple example through which user will get some idea about creating a custom drush command.
/**
* Implementation of hook_drush_command().
*/
function mymodule_drush_command() {
$items = array();
// Name of the drush command.
$items[’list-site-users’] = array(
’description’ => ’Print the list of users in the site’,
’callback’ => ’drush_get_site_users’,
);
return $items;
}
function drush_get_site_users() {
$query = db_select(’users’, ’u’);
$query->fields(’u’, array(’name’));
$result = $query->execute();
while($record = $result->fetchAssoc()) {
print_r($record[’name’]);
}
}
In the above example you can see hook_drush_command() is mandatory hook , inside which you have to define your custom drush command , for our example it is list-site-users . There are lots of other things you can define in this hook but I am not going into the details right now. Then you have write your own logic in the callback functions . So once everything done then you can execute your drush command like this drush list-site-users. Don’t forget to clear the drush cache.