I am developing a module which creates a menu with several items. Each item has a field called menu_name where you put the name of the menu where the item will.

his works when I put the name there of menus that already exist or menus that I create by hand before: primary-links secondary-links navigation or menu-MENU_NAME_CREATED.

But what if I want the module to create a menu and put the items him there?

How do I create a menu?

function mymodule_menu(){

 $items = array();

 $items['parent/item1'] = array(
  'title' => t('My item 1'),
  'page callback' => 'mymodule_item1',
  'page arguments' => array(1),
  'access arguments' => array('view mymodule'),
  'menu_name' => 'menu-mynewmenu',
  'type' => MENU_NORMAL_ITEM,
 );

 $items['parent/item2'] = array(
  'title' => t('My item 2'),
  'page callback' => 'mymodule_tem2',
  'page arguments' => array(1),
  'access arguments' => array('view mymodule'),
  'menu_name' => 'menu-mynewmenu',
  'type' => MENU_NORMAL_ITEM,
 );

 return $items;
}

Comments

Drave Robber’s picture

By drupal_execute('menu_edit_menu', bla-bla). There's an example in the first comment here.

jonvk’s picture

You can also directly insert the menu name into the menu_custom table. Have a look at what admin_menu does:

During install, it adds the menu

/**
 * Helper function to ensure 'admin_menu' menu.
 */
function _admin_menu_install_menu() {
  if (db_result(db_query("SELECT menu_name FROM {menu_custom} WHERE menu_name = 'admin_menu'"))) {
    return array('success' => TRUE, 'query' => 'Menu for Administration menu already exists.');
  }
  return update_sql("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('admin_menu', 'Administration menu', '')");
}

and then it can add the items to this menu.