开发者

Creating a menu item that links to its parent

I've been searching low an high to do this. I want to create a no开发者_如何转开发rmal menu item programatically that links to its parent. Similar to the concept of default menu task, but just a normal menu item. Has anyone ever done this?

I want something that I can do with one of the drupal hooks.


You just need to create a new menu-item, that has the same path as its parent.


$items['parent'] = array(
    'title' => 'I am parent',
    'page callback' => 'drupal_get_form',
    'page arguments' => 'get_parent',
    'access arguments' => array('access parent'),
    'type'      => MENU_NORMAL_ITEM,
);

$items['parent/child'] = array(
    'title' => 'I am child link of parent',
    'page callback' => 'drupal_get_form',
    'page arguments' => 'get_children_for_thisparent',
    'access arguments' => array('access children of parent'),
    'type'      => MENU_NORMAL_ITEM,
);

This should do the trick :)


Suppose you want to place it under Main menu,

    $item['form_example'] = array(
        'title' => 'Example menu',
        'description' => 'This is an example menu item',
        'type' => MENU_NORMAL_ITEM,
        'page callback' => 'custom_function',
        'menu_name' => 'main-menu',
    );

    return $item;

You will then get the menu item under Main menu (clearing cache). The trick is adding a key 'menu_name'.

To get the menu_name please open the menu administration page, and click on 'edit menu' on any root menu. URL will be like 'SITE_URL/admin/structure/menu/manage/main-menu/edit'. Look at the url segment just before the last(here main-menu).

For the second case you might want to place it under Home of main menu. The code is.

$item['form_example'] = array(
    'title' => 'Example menu',
    'description' => 'This is an example menu item',
    'type' => MENU_NORMAL_ITEM,
    'page callback' => 'custom_function',
    'menu_name' => 'main-menu',
    'plid' => 218,
);

return $item;

Here, I have added a key plid to associative array, it is the mlid of the menu item('Home' here) for which it will be a child. For this case it will be child of Home menu.

To get the mlid you have to do the same as described above, goto menu administration page and click list links and then click edit on the menu item, e.g. SITE_URL/admin/structure/menu/item/218/edit, thus you can get the mlid and thus its done.

Note. If you change this menu hierarchy or other settings from back-end, you will always have reset option to reset it, and after resetting it will behave like described in the code.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜