如何在 Drupal 中更改父菜单项而不破坏子菜单项?

发布于 2024-10-06 23:38:42 字数 847 浏览 0 评论 0原文

我想在节点更新时更改菜单树的父级。我正在使用名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭水袖舞倾城 2024-10-13 23:38:42

我通过尝试 API 发现了这一点:

<?php
    $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path='%s'", "node/".$node->nid);
    $oldItem = db_fetch_array($result);
    $oldLinkItem = menu_link_load($oldItem[mlid]);
    $oldLinkItem[plid] = $plid;
    menu_link_save($oldLinkItem);
?>

只需要弄清楚在保存新菜单项时我不必处理那些“p0”到“p8”。 Drupal 似乎会自动移动孩子们。顺便说一句,我可以使用更可靠的方法来获取节点菜单项的 mlid。

I figured it out by experimenting with the API:

<?php
    $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path='%s'", "node/".$node->nid);
    $oldItem = db_fetch_array($result);
    $oldLinkItem = menu_link_load($oldItem[mlid]);
    $oldLinkItem[plid] = $plid;
    menu_link_save($oldLinkItem);
?>

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.

掩于岁月 2024-10-13 23:38:42

使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文