Drupal: automatically add menu items when new node

2019-05-01 02:12发布

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

9条回答
相关推荐>>
2楼-- · 2019-05-01 02:47

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.

查看更多
倾城 Initia
3楼-- · 2019-05-01 02:48

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;
}
查看更多
Juvenile、少年°
4楼-- · 2019-05-01 02:48

There's also the Menu Position module that allows to put content under specific menu entries, depending on their content type, their language and taxonomy. It also has a small API to add other criteria.

查看更多
登录 后发表回答