Say I need to have the same menu item appear in two different menu trees or at two different level of the same menu. How do I properly "duplicate" a menu item using hook_menu()
?
To my understanding, if the original item has its path corporate/contact-us
, then putting in the hook_menu()
the same path will override the original rather than create a copy. Am I right? How do I clone, or should I say: alias, a menu item programmatically?
(And I need to do that for each language defined for my site.)
I have used this piece of code, using hook_menu_alter()
which for some reason did not work; once an item has been created it did not change its label and produced buggy, combined paths (/en/polish/path). Hence the attempt to unset the custom item first.
function MYMODULE_menu_alter(&$items) {
global $language;
$custom_items = array(
'pl' => 'firma/kontakt',
'en' => 'corporate/contact-us',
);
foreach($custom_items as $langcode => $path) {
unset($items[$path]);
}
switch ($language->language) {
case 'en':
$items['corporate/contact-us'] = array(
'title' => 'Contact Us',
'page callback' => 'node_load',
'page arguments' => array(285),
'access callback' => TRUE,
'menu_name' => 'main-menu',
'type' => MENU_NORMAL_ITEM,
);
break;
case 'pl':
$items['firma/kontakt'] = array(
'title' => 'Kontakt',
'page callback' => 'node_load',
'page arguments' => array(286),
'access callback' => TRUE,
'menu_name' => 'main-menu',
'type' => MENU_NORMAL_ITEM,
);
break;
}
}
Similarly, the following hook_menu
approach did not work for me, with similar problems. This one was even worse in that the 'fake paths' with '2' in the title where visible to the user in the status bar.
function sint_newsnavi_menu() {
global $language;
switch($language->language){
case 'en':
$items['corporate/contact-us2'] = array(
'title' => 'Contact Us',
'page callback' => 'drupal_goto',
'page arguments' => array('node/285'),
'access callback' => TRUE,
'menu_name' => 'main-menu',
'type' => MENU_NORMAL_ITEM,
);
break;
case 'pl':
$items['firma/kontakt2'] = array(
'title' => 'Kontakt',
'page callback' => 'drupal_goto',
'page arguments' => array('node/286'),
'access callback' => TRUE,
'menu_name' => 'main-menu',
'type' => MENU_NORMAL_ITEM,
);
break;
}
return $items;
}