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

前端 未结 5 1681
醉梦人生
醉梦人生 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:09

    Here's a helper function to return a whole subtree of a menu, starting at a specified mlid. Some of the other posts only return the direct descendants of the current item; this will return ALL descendants.

    By default it gives you the subtree starting with the current page, but you can pass in any menu tree (as returned by menu_build_tree) and any mlid.

    function _menu_build_subtree($menu=NULL,$mlid=NULL) {
    
      if ($menu == NULL || $mlid == NULL) {
        $parent = menu_link_get_preferred();
      }
      $menu = !is_null($menu) ? $menu : menu_build_tree($parent['menu_name']);
      $mlid = !is_null($mlid) ? $mlid : $parent['mlid'];
    
      foreach ($menu as $branch) {
        if ($branch['link']['mlid'] == $mlid) { 
          return $branch;
        }
        $twig = _menu_build_subtree($branch['below'],$mlid);
        if ($twig) { return $twig; }
      }
    
      return array();
    }
    

提交回复
热议问题