Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Drush command for converting rules from the Drupal Menu Trails module to Menu position module
<?php
/**
* Implementation of hook_drush_command()
*
* @return array
* Command definitions array.
*
*/
function menu_position_migrate_drush_command() {
$items = array();
$items['menu-trails-to-menu-position'] = array(
'description' => "Convert menu trails rules to menu position rules.",
'arguments' => array(
'ruletype' => 'The type of rules to convert if not all',
),
'examples' => array(
'drush mt2mp turkey taxonomy_term' =>
'Convert taxonomy term based rules.',
),
'aliases' => array('mt2mp'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
'core' => array('6'),
'drupal dependencies' => array('menu_position', 'menutrails')
);
$items['menu-position-clear'] = array(
'description' => 'Remove all menu position rules',
'examples' => array(
'drush mp-clear' => 'Clear all menu position rules',
),
'aliases' => array('mpclear'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
'core' => array('6'),
'drupal dependencies' => array('menu_position'),
);
return $items;
}
/**
* menu_position_migrate_convert_rule
*
* Convert a content type or taxonomy rule from menutrails to menu_position.
*
*/
function menu_position_migrate_convert_rule($menu_name, $rule_type, $path, $condition) {
$rule = array();
$rule['menu_name'] = $menu_name;
$rule['plid'] = db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path = '%s'", $path));
// no matching mlid for the path
if ($rule['plid'] === FALSE) {
return FALSE;
}
if ($rule_type == 'content_type') {
$rule['conditions']['content_type']['content_type'] = array($condition => $condition);
$rule['admin_title'] = $condition;
}
elseif ($rule_type == 'taxonomy_term') {
$term = taxonomy_get_term($condition);
// did not get a valid tid
if (!is_object($term)) {
return FALSE;
}
$vocabulary = taxonomy_vocabulary_load($term->vid);
$rule['conditions']['taxonomy']['tid'] = $condition;
$rule['admin_title'] = $vocabulary->name . ': ' . $term->name;
}
$rule['conditions'] = serialize($rule['conditions']);
return $rule;
}
/**
* drush_menu_position_migrate_menu_position_clear
*
* Drush command that clears all the menu position rules in database.
*
*/
function drush_menu_position_migrate_menu_position_clear() {
// delete all existing menu_position rules
module_load_include('inc', 'menu_position', 'menu_position.admin');
$result = db_query("SELECT rid FROM {menu_position_rules}");
while ($row = db_fetch_array($result)) {
menu_position_delete_rule($row['rid']);
}
}
/**
* drush_menu_position_migrate_menu_trails_to_menu_position
*
* Drush command that converts menutrails module's rules to menu position.
*
* @param $ruletype
* What type of rules to convert, one of content_type, taxonomy_term, all.
*
*/
function drush_menu_position_migrate_menu_trails_to_menu_position($ruletype = 'all') {
module_load_include('inc', 'menu_position', 'menu_position.admin');
$menu = variable_get('menutrails_menu', 'navigation');
// simply the order these get added in determines rule precedence
if ($ruletype == 'taxonomy_term' || $ruletype == 'all') {
$types['taxonomy_term'] = variable_get('menutrails_terms', array());
}
if ($ruletype == 'content_type' || $ruletype == 'all') {
$types['content_type'] = variable_get('menutrails_node_types', array());
}
foreach ($types as $type => $rules) {
foreach ($rules as $cond => $path) {
if (empty($path)) {
continue;
}
$rule = menu_position_migrate_convert_rule($menu, $type, $path, $cond);
if ($rule !== FALSE) {
menu_position_add_rule($rule);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment