开发者

Drupal 7 - Menu to UL

开发者 https://www.devze.com 2023-02-22 10:19 出处:网络
all. This question probably has a devilishly simple answer but it has kept me occupied for several hours.

all. This question probably has a devilishly simple answer but it has kept me occupied for several hours.

I have my main menu and it's corresponding block in a Drupal site I am building. Like all other Drupal menus it contains a bunch of links to various parts of the site. I can assign it's block to a region and the menu links come out all nice and formatted with a title thing and little bullet points. The problem though is that I am making a custom theme for this website and I need to be able to work with the links without all the cruft added, preferably i开发者_如何学JAVAn something simple like an ul.

Is there any function that takes a menu and produces an ul containing all the links?

Maybe there is some way you can reduce the menu's block to just an ul.

I have been experimenting with theme_menu_tree(...) and theme(...) to no avail.

Thank you!


I find you can do most changes through CSS such as setting <H2> titles to display: none and setting <LI> tags to float: left for a horizontal navbar.


But... if you want to build your own menu from the Drupal data, here is some code from a site I'm working on. It builds a two-level menu. I'm sure you could simplify this code even further if you need.

//----------- primary menu (horizontal with drop-downs) -------------------------

$params = array('max_depth' => 3);
$menu = menu_build_tree('main-menu', $params);
$variables['menu'] = $menu;

$html = '<ul>';

foreach($menu as $item_menu) { //for each main element

    $isSecondLevel = isset($item_menu['below']) && !empty($item_menu['below']);

    if ($isSecondLevel) {
        $html.= '<li>';
    } else {
        $html.= '<li class="sg">';
    }

    $html.= '<a class="topLevel" href="'.url($item_menu['link']['link_path']).'">';         
    $html .= $item_menu['link']['link_title'];
    $html .= '</a>';

    //is there any sub elements to display
    if ($isSecondLevel) {
        $html.= '<ul>';

        foreach($item_menu['below'] as $item_submenu) { //for each sub element
            $isThirdLevel=isset($item_submenu['below']) && ! empty($item_submenu['below']) ? 'main-menu-third_level' : '';

            $html.= '<li>';
            $html.= '<a href="'.url($item_submenu['link']['link_path']).'">';

            $html.= $item_submenu['link']['link_title'];

            $html.= '</a>';
            $html.= '</li>';
        }
        $html.= '</ul>';    
    }
    $html.= '</li>';
}
$html.= '</ul>';
$variables['main_menu_html'] = $html;

This code was placed inside function pinkribbon_process_page(&$variables) in template.php. Menu is printed in the template by calling <?php echo $main_menu_html ?>

Simon.

P.S. Others, please feel free to edit this code for clarity/simplicity.


I advice you to use

menu_tree_output

like this:

print render(menu_tree_output(menu_build_tree('main-menu', $parameters)));


You could call menu_build_tree and look at it's output and build a ul from it. However, despite the default menu output having loads of "cruft" it is a ul and should be themeable with CSS.

If you really want to build the menu yourself, I would reverse engineer another module that does so like Nice Menus

0

精彩评论

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

关注公众号