1

I am working with Drupal 7. I have created a custom module that supplies several menu items using hook_menu. I am adding one of these menu items to the main menu:

$items['my-chapter'] = array(
    'title' => 'My Chapter',
    'page callback' => 'mymodule_my_chapter',
    'file' => 'includes/pages.inc',
    'menu_name' => 'main-menu',
    'weight' => '110',
    'access callback' => TRUE,
    'type' => MENU_NORMAL_ITEM,
  );

Adding the menu item to the main menu works fine using this code. But I have a secondary menu that also needs this link. Is there a way to add it to another menu in addition to the main menu I'm already adding it to? Adding another $items['my-chapter'] will obviously just overwrite the previous one and the 'menu_name' only takes a single string.

Can anyone shed some light on how/if this can be done? Thanks.

3 Answers 3

2

I'm pretty sure you are looking for menu_link_save() as adding the menu_name in hook_menu() just puts this callback in that menu instead of defaulting it into the navigation menu. So, after creating it above, with or without the menu_name, you should be able to add it to any other menu(s) with menu_link_save().

3
  • Thanks for the pointer to the menu_link_save() function. This reads like it should work, I'm just not sure of an appropriate place to call this in my module. Setting up this menu should be a one time event. Adding this duplicate link I need should only happen after the menu links are created and definitely not every time the module is in play. There is no admin to this module that would toggle this on and off firing the function on an isolated event. Any thoughts?
    – demarts
    Commented Apr 18, 2012 at 16:53
  • I suppose you could put it inside a menu_alter() although it might not really belong there, but at least it would only be called on menu rebuilds, and if you surrounded it with some variable_get()/variable_set() logic, it might only be called once. Or maybe when you are done with development, you could put it in your MODULE.install?
    – Jimajamma
    Commented Apr 18, 2012 at 17:08
  • I don't think the MODULE.install would be the best place to put it as the link has to be defined first, and that happens after hook_install() I believe. So I went with your first supposition and appropriated the hook_menu_alter(). As you pointed out, it might not really belong there and does feel a little hackish using it for not quite what it was intended, but in the absence of any other thoughts on the matter, it does work. I do a check for the entry in the DB, and if the entry doesn't exist I use menu_link_save() to add it. It should only fire when the menus are rebuilt. Thanks.
    – demarts
    Commented Apr 18, 2012 at 18:56
0

You can simply add another entry, such as the following one.

$items['my-chapter-2'] = array(
  'title' => 'My Chapter',
  'page callback' => 'mymodule_my_chapter',
  'file' => 'includes/pages.inc',
  'menu_name' => $menu_name,
  'weight' => '110',
  'access callback' => TRUE,
  'type' => MENU_NORMAL_ITEM,
);

Replace $menu_name with the name of the menu where the menu item should appear.

Drupal just needs to know the page callback to call, and the users just see the menu title. In this way, you can have two menu items showing the same page.

0

Maybe hook_enable() would be a more appropriate place for using menu_link_save() as it is only fired once and after the .module file is fully loaded, so your custom menu item would already be in place. You will also need to add 'customized' => 1, to your menu link array otherwise it wont be added. ref.. Don't forget to the remove the menu link in hook_disable(). They should both be placed in the .install file of your module.

hook_enable()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.