views:

163

answers:

2

hi,

can I automatically add a menu item when I add a node to the page in Drupal?

In other words, can I associate a menu parent with a node content-type, and then automatically add the children if new nodes are added ?

thanks

A: 

Yes.

I am sure there is a module do to something like that, but you could also create your own.

There are two ways you could go about it.

You could use hook_menu() to query for the items you want and return the correct menu structure. You would need to also make sure the menu cache is rebuilt on a node save using hook_nodeapi(). See henricks' comments below about why this is a bad idea

Alternitivly you could use hook_nodeapi() to add custom menu items with menu_link_save().

Edit

hook_menu should return an array of menu items, often these are pretty static however there is nothing wrong with these arrays being dynamically generated.

So you can query the node table to get a list of nodes you want, loop through these items and dynamically create an array which contains the correct menu items.

very roughly:

function example_menu() {
  $result = db_query('select * from node where ...'); // put in your own select items and where clause
  $menu = array();
  while ($row = db_fetch_object($result)) {
    $menu['my_path/' . $row->nid;] = array(
      // See hook menu docs for what to put here.
    );
  }
  return $menu;
}
Jeremy French
hey thanks, I'm considering the first approach. If I understood well, it is more powerful and can automatically add nodes that have been already created and saved before. Could you elaborate more "You could use hook_menu() to query for the items you want and return the correct menu structure." It is not clear how to get the nodes list from hook_menu
Patrick
**One should not use `hook_menu()` for this!** Creating visible menu entries is only a side effect/convenience feature of the hook, **not it's main purpose**. The 'menu' created/manipulated by this hook is the Drupal internal menu system (router table), not the visible UI menus. The visible menu entries should be created via menu_link_save, as per Jeremys second suggestion (this is what hook_menu uses itself to create the 'convenience' entries).
Henrik Opel
I explained the difference of router items vs. menu entries [in another answer](http://stackoverflow.com/questions/1844109/drupal-module-nested-menu-items/1869469#1869469). See also [When and how to use menu_links](http://drupal.org/node/217393).
Henrik Opel
@Jeremy: I'd give a +1 for your second suggestion, and a big -1 for the first, so this cancels out. While there is indeed nothing wrong with dynamically creating multiple entries via `hook_menu` in principle, clogging up the router table with one entry per node is a huge no no, IMHO. (It will impact performance, but more importantly, it will mess up a lot of other functionality, as it would result in nodes having more than one internal path. Many modifications rely on identifiying a node page by its internal path being 'node/[nid]', and this would break with this approach!)
Henrik Opel
Henrik, thanks for the heads up.
Jeremy French
A: 

You should take a look at the Auto Menu module - while the Drupal 6 version is still a dev release, it might cover your needs. If not, you can take it as an example of how to use menu_link_save() to create your own solution.

Henrik Opel

related questions