Is there a simple example anywhere that demonstrates creating a new menu from Drupal 7 module code?

I'd like there to be a new menu with a fixed set of options when a user loads the module I'm developing.

So far my efforts with the menu API have not produced happy results. (Had success creating a theme with custom regions, module creates blocks for them, etc, but no luck creating a menu from API or finding a Drupal 7 example on how to do it.)

Comments

nevets’s picture

Here is the code from the menu module in menu.admin.inc

function menu_edit_menu_submit($form, &$form_state) {
  $menu = $form_state['values'];
  $path = 'admin/structure/menu/manage/';
  if ($form['#insert']) {
    // Add 'menu-' to the menu name to help avoid name-space conflicts.
    $menu['menu_name'] = 'menu-' . $menu['menu_name'];
    $link['link_title'] = $menu['title'];
    $link['link_path'] = $path . $menu['menu_name'];
    $link['router_path'] = $path . '%';
    $link['module'] = 'menu';
    $link['plid'] = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :link AND module = :module", array(
      ':link' => 'admin/structure/menu',
      ':module' => 'system'
    ))
    ->fetchField();

    menu_link_save($link);
    menu_save($menu);
  }
  else {
    menu_save($menu);
    $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path", array(':path' => $path . $menu['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
    foreach ($result as $m) {
      $link = menu_link_load($m['mlid']);
      $link['link_title'] = $menu['title'];
      menu_link_save($link);
    }
  }
  drupal_set_message(t('Your configuration has been saved.'));
  $form_state['redirect'] = $path . $menu['menu_name'];
}

which could be adapted to your needs.

DrupalFrank’s picture

Awesome tip nevets! My custom menu is now appearing after I added this function to my module based on the detail in your posting...

function mymodule_create_custom_menu()
{

    $menu = array();
    $menu['menu_name'] = "frank-presentation-tabs";
    $menu['title'] = "Presentation tabs";
    $menu['description'] = "The <em>Presentation tabs</em> menu contains links to presentation detail";
    menu_save($menu);

}

Now I'll move into the more complex looking items portion of the menu creation process. I'll want the items to look like tabs. Will post questions if I get stuck, and post solution if I find it.

LarsKramer’s picture

hook_menu

See also the Examples module for developers which is mentioned in Creating modules - a tutorial: Drupal 7.x.

DrupalFrank’s picture

Thanks Lars Kramer for the Examples module tip. I installed the menu_example and am looking it it now. (Already went through the tutorials on Drupal.org, but somehow missed that.)

yareckon’s picture

Just a note that using menu_save and hook_menu are two different things. The first is creating a menu through the menu module that exists in the database, just like a menu that was created by hand in the GUI.

If you wanted to (for some reason) write a script to create 20 menus full of test content that could then later be edited through the menu gui, this would be what you wanted to use.

Using hook_menu is for defining menu structures in code... normally this is for building the url structure and routing that a module needs. This is the right way to go if you are building admin pages, or a callback structure.