how to get all the menu items below a certain parent in drupal?

前端 未结 5 1680
醉梦人生
醉梦人生 2021-02-01 23:32

I really only need the mlid and title text for the first level below a certain menu item. Here\'s what I\'m doing at the moment. (It works, but I suspect there may be a more dru

5条回答
  •  既然无缘
    2021-02-02 00:01

    Actually there is an easy way to get that information by using menu_build_tree():

    // Set $path to the internal Drupal path of the parent or
    // to NULL for the current path 
    $path = 'node/123';
    $parent = menu_link_get_preferred($path);
    $parameters = array(
        'active_trail' => array($parent['plid']),
        'only_active_trail' => FALSE,
        'min_depth' => $parent['depth']+1,
        'max_depth' => $parent['depth']+1,
        'conditions' => array('plid' => $parent['mlid']),
      );
    
    $children = menu_build_tree($parent['menu_name'], $parameters);
    

    $children contains all information you need. menu_build_tree() checks access or translation related restrictions too so you only get what the user really should see.

提交回复
热议问题