1

I want to create some menu items, i have code for it

   $item = array(
  'link_path' => 'guarantee/validto' ,
  'link_title' => "meeeeeeeeeeeeeeeeeeeeeeeeeeeeeenu",
  'plid' => 0,
  'hidden' => 1,
        'weight' => 0,
        'menu_name' => 'main-menu',
        'module' => 'guarantee',
   );
   menu_link_save($item);

But nowhere i can find where i should place this part of code. Can someone could suggest me where to place it to create manu items? I have my moudle enabled/installed and i need to create 10 menu items like above. What will be the best solution? is

  • Your code looks fine to me. You have to keep in mind that you clear your (menu) cache after saving this menu item. You can do it by calling menu_cache_clear_all() just after menu_link_save. – AjitS Aug 6 '14 at 10:07
  • Ok, but i don't know where i should place it. One of my test is to place code in mymodule_update_7000 and then fire http//mysite/update.php. Its working..., but i was thinking that there shold be better place. So when i enable module, menu items should be created and accessible... – mbahojlo Aug 6 '14 at 10:13
  • What appears on the menu path for which the menu item is being defined? What type of page is it? Node page? View page? – AjitS Aug 6 '14 at 14:08
  • AjitS: it is simple hook_menu item i render form when user will go to guarantee/validto – mbahojlo Aug 7 '14 at 7:36
2

You don't need to put in the update function. It can go anywhere in your module just Remember to clear the cache after you update the module so you can see it.

If you're adding new menu items in a module update you should put:

cache_clear_all('*', 'cache_menu', TRUE);

In the mymodule_update_7000 function and it will clear the menu cache automatically when you update the module.

Additionally: You might be better using hook_menu for the menu items in your module.

something like this:

function mymodule_menu() {
  $items['guarantee/validto'] = array(
    'title' => 'meeeeeeeeeeeeeeeeeeeeeeeeeeeeeenu',
    'type' => MENU_NORMAL_ITEM,
    'weight' => 0,
    'menu_name' => 'main-menu',
    'page callback'   => 'drupal_goto',
    'page arguments' => array('guarantee/validto'),
  );
  return $items;
}

It can go anywhere you like in your mymodule.module file but generally goes near the top. Again, you don't need to put in the update function but remember to clear the cache after.

  • Thanks: Tony Dunlop, setting 'menu_name' => 'main-menu' solved my problem, without specify this, menu item was going always to navigation, now it go to main-menu. Thanks a lot. – mbahojlo Aug 7 '14 at 9:04
  • Thanks, I've been using it for years thought it was best to start giving something back :) – munkiepus Aug 7 '14 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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