Drupal:添加新节点时自动添加菜单项 [英] Drupal: automatically add menu items when new nodes are added
问题描述
换句话说,我可以将菜单的父项与节点内容相关联吗? -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屋!