如何在 Drupal 中更改父菜单项而不破坏子菜单项?
我想在节点更新时更改菜单树的父级。我正在使用名为“rules”的模块来访问更新事件,并且我的代码如下:
if ($node->taxonomy[1] == "1") {
$plid = 440;
} else if ($node->taxonomy[1] == "2") {
$plid = 379;
}
if($plid) {
db_query("UPDATE {menu_links} SET plid='".$plid."', p1='".$plid."' WHERE link_path='"."node/".$node->nid."'");
}
问题是这个更新的 $node 下也有一些子菜单项,在主菜单中以及当我更新时更改父节点的节点,其子节点最终会上升一级,并且不会随当前菜单项移动。有没有一种简单的方法可以将整个菜单树从一个父菜单项移动到另一个父菜单项?
我希望
-parent1
--child1
---sub-child1
---sub-child2
-parent2
是这样的:
-< strong>parent1
-parent2
--child1
---sub-child1
---sub-child2
你能帮我吗?有 Drupal 方式来做这件事吗? =)
我知道这段代码做了很多假设,但我只需要它用于一种特定情况
I want to change the parent of a menu tree on a node update. I'm using the module named "rules" to access the update event and I have this code as follows:
if ($node->taxonomy[1] == "1") {
$plid = 440;
} else if ($node->taxonomy[1] == "2") {
$plid = 379;
}
if($plid) {
db_query("UPDATE {menu_links} SET plid='".$plid."', p1='".$plid."' WHERE link_path='"."node/".$node->nid."'");
}
The problem is that this $node which is updated also has some child menu items under it, in the primary menu and when I update the node to change the parent, its children just end up one level higher and don't get moved with current menu item. Is there an easy way to move a whole menu tree from one parent menu item to another?
I want this,
-parent1
--child1
---sub-child1
---sub-child2
-parent2
to be this:
-parent1
-parent2
--child1
---sub-child1
---sub-child2
Can you help me? Is there a Drupal-way of doing this? =)
I know that this code makes a lot of assumptions but I need it just for one specific case
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过尝试 API 发现了这一点:
只需要弄清楚在保存新菜单项时我不必处理那些“p0”到“p8”。 Drupal 似乎会自动移动孩子们。顺便说一句,我可以使用更可靠的方法来获取节点菜单项的 mlid。
I figured it out by experimenting with the API:
Just needed to figure out that I didn't have to deal with those "p0" to "p8" when saving the new menu item. Drupal seems to automatically move the children. By the way, I could use a more reliable way of getting mlid of nodes menu item.
使用Drupal API来执行这些操作;你会发现它更容易。如果您想使用数据库查询来完成此操作(我绝对建议您不要这样做),您将编写一个执行树移动的递归算法。
Use the Drupal API to perform these operations; you will find it much easier. If you wanted to do it using database queries (which I must absolutely advise you do not), you would write a recursive algorithm that performed the move of the tree.