This post is part of a series of posts on making changes in Drupal programmatically rather than in the Drupal interface.

In this tutorial, we will look at saving menu items programmatically. This can be done in the .install file of a site deployment module in order to automate this task rather than having to manually assign the roles in the Drupal UI.

Standard menu item

Standard menu items are relatively straight forward. You build up an array with the title of the link, path and the menu you want to save the item under (main-menu in this case). If the menu item doesn’t have a parent, the plid (parent link ID) is 0. You then save the menu item using menu_link_save();

function site_deployment_update_7000() {
  $item = array (
    'link_title' => 'Example Link Title',
    'link_path' => 'example/path',
    'menu_name' => 'main-menu',
    'weight' => 0,
    'plid' => 0,
  );
  menu_link_save($item);
}

Node path

Saving a menu item for a node is a bit more complicated, especially if you know the node alias but not the node path. The node path is the one with the node ID (e.g. node/33). The menu system doesn’t want to save the path alias, it wants the real node path. So in this case, you can use drupal_lookup_path() to get the path from the alias. The first argument for drupal_lookup_path() is source, which will ensure it returns the node system URL. And then use that as the link_path. The router_path is simply node/% whenever you are saving a menu item for a node.

When saving node paths, it is important to note that the node must exist and be published for it to appear in the actual menu.

function site_deployment_update_7001() {
  $path = drupal_lookup_path('source', 'example-node-alias');
  $item = array (
    'link_title' => 'Example Link Title',
    'link_path' => $path,
    'router_path' => 'node/%',
    'menu_name' => 'main-menu',
    'weight' => 0,
    'plid' => 0,
  );
  menu_link_save($item);
}

Parent and child menu items

Things get trickier if you want to save a parent menu item and child menu items at the same time.

The parent menu item is much the same, except that you store the result of menu_link_save in a variable ($plid). This will be the ID of the menu item, which you can use in the parent link ID (plid) in the child menu items.

$path = drupal_lookup_path('source', 'example-node-alias');
$item = array (
  'link_title' => 'Example Link Title',
  'link_path' => $path,
  'router_path' => 'node/%',
  'menu_name' => 'main-menu',
  'weight' => 0,
  'plid' => 0,
);

$plid = menu_link_save($item);

Here is the first child menu item, using the parent ID ($plid) from the menu link just saved:

$item = array (
  'link_title' => 'Example2 Link Title',
  'link_path' => 'path/to/example2',
  'menu_name' => 'main-menu',
  'weight' => 0,
  'plid' => $plid,
);

Programmatically saving a menu item

Here is the full example, where we are saving the parent menu item and two children.

function site_deployment_update_7002() {
  $path = drupal_lookup_path('source', 'example-node-alias');
  $item = array (
    'link_title' => 'Example Link Title',
    'link_path' => $path,
    'router_path' => 'node/%',
    'menu_name' => 'main-menu',
    'weight' => 0,
    'plid' => 0,
  );

  $mlid = menu_link_save($item);

  $item = array (
    'link_title' => 'Example2 Link Title',
    'link_path' => 'path/to/example2',
    'menu_name' => 'main-menu',
    'weight' => 0,
    'plid' => $plid,
  );

  menu_link_save($item);

  $item = array (
    'link_title' => 'Example3 Link Title',
    'link_path' => 'path/to/example3',
    'menu_name' => 'main-menu',
    'weight' => 0,
    'plid' => $plid,
  );

  menu_link_save($item);
}

Hide the menu item

You want to save a new menu item but immediately disable it. This isn’t as crazy as it sounds. I have done this recently on a client project. The link is live on the staging site, but the client wants it to exist on live but disabled for now until more nodes are created. They can simple enable it when ready.

To do this, add the hide property and give it a value of 1. 1 means hide (disable), 0 means do not hide (enable).

function site_deployment_update_7003() {
  $item = array (
    'link_title' => 'Example Link Title',
    'link_path' => 'path/to/example',
    'menu_name' => 'main-menu',
    'weight' => 0,
    'plid' => $plid,
    'hide' => 1,
  );
  menu_link_save($item);
}