Custom Drush Commands Primer

Public

Showcases how easy it is to implement custom Drush commands. Name the file as toolkit.drush.inc and make it executable ($ sudo chmod a+x toolkit.drush.inc). Put this file in the ~/.drush folder so that you will have now a ~/.drush/toolkit.drush.inc file.

Get raw version
php
  1. /**
  2.  * Implements HOOK_drush_command().
  3.  */
  4. function toolkit_drush_command() {
  5. // Placeholder for the commands to be registered.
  6. $items = array();
  7.  
  8. // Command for "Clear Cache All".
  9. // The 'cca' array key is what you type in the terminal, like: drush cca.
  10. // This will be a shortcut for drush cc all.
  11. $items['cca'] = array(
  12. 'description' => "Similar to hitting the 'Clear cache' button in Drupal.",
  13. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  14. );
  15.  
  16. // Command for "Node Load".
  17. $items['nl'] = array(
  18. 'description' => "Load a node given its node ID.",
  19. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  20. );
  21.  
  22. return $items;
  23. }
  24.  
  25.  
  26. /**
  27.  * Implements drush_HOOK_COMMANDNAME.
  28.  *
  29.  * Purpose: Clear Drupal's entire cache.
  30.  * Syntax: drush cca
  31.  * Related Command: drush cc all.
  32.  */
  33. function drush_toolkit_cca() {
  34. // Call the Drupal API's function for clearing the site's cache.
  35. drupal_flush_all_caches();
  36.  
  37. // Display some notifications in the terminal.
  38. drush_print("All caches have been cleared! Ready for more action!");
  39. }
  40.  
  41.  
  42. /**
  43.  * Implements drush_HOOK_COMMANDNAME.
  44.  *
  45.  * Purpose: Load a target node to analyze its properties/values for remote debugging.
  46.  * Syntax: drush nl NODE_ID
  47.  * Related Command: drush php-eval 'var_export(node_load(NODE_ID));'
  48.  */
  49. function drush_toolkit_nl($nid) {
  50. // Call the Drupal API's function for loading a node.
  51. $node_object = node_load($nid);
  52.  
  53. // Display the object's attributes in terminal.
  54. // drush_print(var_export($node_object) is redundant.
  55. // drush_print($node_object) will not work since drush_print is for simple texts only.
  56. var_export($node_object);
  57. }