Misc Drush commands
I've finally gotten around to using Drush (mostly because I got a Mac -- Drush+Cygwin made me procrastinate). There are a few day-to-day tasks I perform that aren't in Drush core, but which are easy to add to Drush via .drush.inc files. I'll upload my ocdevel.drush.inc file here and update this blog post any time I add new commands (see the "Commands" section of Drush's README for how to install this file for your own sites). Right now it only supports adding menu items (primary links, secondary links, etc) via the following command:
- drush add-menu-item {menu} {title} {path} [description]
- o example: drush add-menu-item primary "Home" "<front>"
I'm planning on just adding fundamental Drupal site-configuration stuff and then creating a project-page out of the end-result. Hit me up if you have requests.
<?php
// $Id: example.drush.inc,v 1.3 2009/04/15 01:13:45 weitzman Exp $
/**
* @file
* Example drush command.
*
* Shows how to make your own drush command.
*
* You can copy this file to any of the following
* 1. A .drush folder in your HOME folder.
* 2. Anywhere in a folder tree below an active module on your site.
* 3. In an arbitrary folder specified with the --include option.
*/
/**
* Implementation of hook_drush_command().
*
* In this hook, you specify which commands your
* drush module makes available, what it does and
* description.
*
* Notice how this structure closely resembles how
* you define menu hooks.
*
* @See drush_parse_command() for a list of recognized keys.
*
* @return
* An associative array describing your command(s).
*/
function ocdevel_drush_command() {
$items = array();
// the key in the $items array is the name of the command.
$items['add-menu-item'] = array(
// the name of the function implementing your command.
'callback' => 'ocdevel_add_menu_item',
// a short description of your command
'description' => "Adds a menu item to Primary, Secondary, etc",
);
// more commands here ...
return $items;
}
/**
* Implementation of hook_drush_help().
*
* This function is called whenever a drush user calls
* 'drush help <name-of-your-command>'
*
* @param
* A string with the help section (prepend with 'drush:')
*
* @return
* A string with the help text for your command.
*/
function ocdevel_drush_help($section) {
switch ($section) {
case 'drush:add-menu-item':
return dt("Usage: drush add-menu-item {menu} {title} {path} [optional: description]; example, drush add-menu-item primary \"Home\" \"<front>\"");
}
}
/**
* Example drush command callback.
*
* This is where the action takes place.
*
* In this function, all of Drupals API is (usually) available, including
* any functions you have added in your own modules/themes.
*
* To print something to the terminal window, use drush_print().
*
*/
function ocdevel_add_menu_item() {
$commands = func_get_args();
$title = $commands[1];
$path = $commands[2];
$description = $commands[3]?$commands[3]:'';
switch (drush_drupal_major_version()) {
case 5:
switch($commands[0]){ //primary, secondary, navigation, etc
case 'primary': $pid=2; break;
case 'navigation':
case 'default': $pid=1; break;
}
$values=array(
'title'=>$title,
'path'=>$path,
'pid'=>$pid,
'description'=>$description,
'weight' => db_result(db_query("SELECT MAX(weight) FROM {menu} WHERE pid=%d", $pid)) + 1,
);
drupal_execute('menu_edit_item_form', $values, 'add');
cache_clear_all('*', 'cache_menu', TRUE);
break;
case 6:
switch($commands[0]){ //primary, secondary, navigation, etc
case 'navigation': $pid='navigation'; break;
case 'secondary': $pid='secondary-links';break;
case 'primary':
case 'default': $pid='primary-links'; break;
}
$form_state['values']['menu']=array(
'link_title' => $title,
'link_path' => $path,
'description' => $description,
'parent' => $pid.':0',
'weight' => db_result(db_query('SELECT MAX(weight) FROM {menu_links} WHERE plid=0 AND menu_name="%s"',$pid)) + 1,
'op' => t('Save'),
);
module_load_include('inc', 'menu', 'menu.admin');
drupal_execute('menu_edit_item', $form_state, 'add', NULL, NULL);
break;
}
}
?>| Attachment | Size |
|---|---|
| ocdevel.drush_.inc | 2.59 KB |
