Automatically creating new menu items Drupal 7

Drupal breadcrumbs implementation is a never-ending source of frustration for many website developers. A few modules exist which more or less successfully attempt to get Drupal to display meaningful breadcrumbs on every page.

One of these modules is menu_breadcrumb, which displays the breadcrumb trail based on current node's menu path. Very useful and sufficient in most use cases.

Limitations and automatic menu modules

The only problem with menu_breadcrumb is when you're not happy to grant users the administer menu permission to create and assign menu items for the content they create.

Several Drupal modules exist which work around the permission restriction and automatically create menu items for newly created content nodes. To name a few, Auto Menu and Automatic Menu, both of which are either only available for Drupal 5.x, or their Drupal 6.x implementation is still in beta development stage.

Rules and Drupal API

Fortunately, a quick and easy way to create menu items programmatically doesn't even require a standalone module. All we're going to need is the Rules module and a bit of Drupal API knowledge.

The Rules set of modules allows site administrators to define conditionally executed actions, such as arbitrary PHP code execution, based on occurring events, such as node creation. For the Drupal API part, we'll use a single function menu_link_save.

menu_link_save requires an item configuration parameter, whose key names mirror the column names of Drupal's menu_links database table.

The solution

As mentioned above, we're going to use the Rules module. It's installation is pretty straightforward and requires no additional configuration. Let's move right to the setting up our rule.

Say we want to create a menu item under Primary Links menu's Blog item every time a new node of type Story is created.

Navigate your browser to Administer » Rules » Triggered rules page, add a new rule and configure it along the lines of the image:

Screen-shot of Drupal Rules' configuration screen of a rule which would execute custom PHP code at each creation of a node of type Story

Finally, we're getting to the PHP snippet itself, which we'll use to automatically create a new menu item every time this rule is triggered. Click Execute custom PHP code and insert the following fragment of code:

$item = array(
  'link_path' => 'node/' . $node->nid,
  'link_title' => $node->title,
  'plid' => 426,
  'hidden' => 1
);
menu_link_save($item);

Just a few remarks on otherwise pretty simple piece of code.

  • menu_link_save function will only accept normalized path as a link path. When using anything nontrivial for the path, such as the loaded node's path property, always use drupal_get_normal_path() to normalize its value!
  • menu_name is the the machine-readable name of the menu. You can get that from Administer » Site building » Menus page as the last component of the menu customization URL.
  • Value of plid is the menu link id of the item you want your item to be a child of. Again, easy to find out, just navigate your browser to the menu's List items page and figure out the menu link id of each menu item from its edit link URL.
  • It's up to you whether you want the menu item hidden or displayed by default. I'm using the automatically created menu items just to give menu_breadcrumb a when putting together the current page's breadcrumb trail. I don't want the menu items to be visible to the users, that's why i set the hidden value to 1.

Source: http://jan.tomka.name/blog/programmatically-creating-menu-items-drupal

Updated: 2020-07-15 | Posted: 2012-06-21