开发者

How to change the parent menu item without breaking the children in 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") {
    $pl开发者_开发知识库id = 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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜