如何在创建节点时以编程方式创建菜单项?

时间:2010-04-08 20:38:59

标签: api drupal

我正在以编程方式创建一些节点,因此:

foreach ($titles as $t) {
    $n = new stdClass();
    $n->type = 'myType';
    $n->uid = 1;
    $n->title = $t;
    $menu = array();
    $menu['link_title'] = $t;
    $menu['menu_name'] = 'primary-links';
    // this attempt at placing the menu item in a particular place in the 
    // menu hierarchy didn't work:
    $menu['parent'] = 'primary-links:867';
    $menu['depth'] = 3;
    $menu['p1'] = '580';
    $menu['p2'] = '867';
    $n->menu = $menu;
    node_save($n);
}

我有一个像这样的菜单结构:

primary-links
    Parent 1
        Child 1
        Child 2
    Parent 2
        Child 3

我希望新菜单项显示为Child 3的子项。我能够在节点的同时创建菜单项,它们出现在正确的菜单中,但不在层次结构中的正确位置。我错过了什么?

3 个答案:

答案 0 :(得分:6)

在drupal 7中你需要设置也启用1(参见:menu_node_save()):

$node->menu = array(
  'link_title' => $node->title,
  'menu_name' => 'main-menu',
  'plid' => 0,
  'enabled' => 1,
);

答案 1 :(得分:4)

我认为你过度复杂了。在过去,当我以编程方式为节点创建菜单项时,我只需设置menu_name,link_title和plid(父链接ID),即:

$menu['link_title'] = $t;
$menu['menu_name'] = 'primary-links';
$menu['plid'] = 867;

菜单模块在调用node_save期间的某个时刻接管,并为您完成剩下的工作。

〜马特

答案 2 :(得分:2)

还必须添加

  

'描述' => ''

到数组,否则我的Drupal 7出错了