Drupal:添加新节点时自动添加菜单项 [英] Drupal: automatically add menu items when new nodes are added

查看:91
本文介绍了Drupal:添加新节点时自动添加菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



换句话说,我可以将菜单的父项与节点内容相关联吗? -type,然后如果添加了新节点,则会自动添加子节点?



谢谢

解决方案

是的。



我相信有一个模块可以做到这样,但你也可以创建自己的。



有两种方法可以参与。



您可以使用 hook_menu()来查询所需的项目并返回正确的菜单结构。您还需要确保使用 hook_nodeapi()保存节点上重建菜单缓存。
看下面的henricks的评论,为什么这是一个糟糕的主意



替代地,你可以使用hook_nodeapi()来添加自定义菜单项 menu_link_save()



编辑



hook_menu应该返回一个菜单项数组,通常这些都是非常静态的,但是这些数组是没有错的动态生成



所以您可以查询节点表以获取所需的节点列表,循环遍历这些项目,并动态创建包含正确菜单项的数组。



非常粗略:

  function example_menu(){
$ result = db_query('select * from node where ...'); //放入你自己的选择项目,where子句
$ menu = array();
while($ row = db_fetch_object($ result)){
$ menu ['my_path /'。 $ row-> nid;] = array(
//查看钩子菜单文档,这里有什么?
);
}
return $ menu;
}


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

解决方案

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;
}

这篇关于Drupal:添加新节点时自动添加菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆