1

我一直在让自己发疯,试图操纵我的主菜单的输出以与 Twitter Bootstrap 的下拉菜单兼容,我想知道是否有人做过类似的事情或有诀窍来帮助我和让我免于为此失去更多的睡眠。就像 Bootstraps 菜单的例子一样,我有一些看起来像这样的东西:

<ul class="nav">
    <li class="active"><a href="/">Home</a></li>
    <li class=""><a href="/news">News</a></li>
    <li class=""><a href="/sports">Sports</a></li>
    <li class=""><a href="/opinion">Opinion</a></li>
    <li class="dropdown">
        <a data-toggle="dropdown" class="dropdown-toggle" href="#">Special Sections <b class="caret"></b></a>
        <ul class="dropdown-menu">
          <li><a href="/special/welcome">Welcome</a></li>
          <li><a href="/special/orientation">Orientation</a></li>
          <li><a href="/special/housing">Housing</a></li>
        </ul>
    </li>
    <li class="dropdown">
        <a data-toggle="dropdown" class="dropdown-toggle" href="#">Media <b class="caret"></b></a>
        <ul class="dropdown-menu">
          <li><a href="/media/video">Video</a></li>
          <li><a href="/media/photo">Photo</a></li>
        </ul>
    </li>
</ul>

我试过使用主题钩子:

<?php 
    print theme('links', $primary_links, array('id' => 'nav', 'class' => 'nav links primary-links')); 
?>

与template.php 中的phptemplate_menu_itemphptemplate_menu_item_link函数一起使用,但到目前为止,我只能成功复制顶层菜单。

作为参考:Drupal Override Custom Menu Template

提前致谢!

4

1 回答 1

1

我可以通过调用自定义 menu_tree 函数来做到这一点:

print bootstrap_menu_tree('primary-links');

在我的主题的 template.php 中有这个:

function bootstrap_menu_tree($menu_name = 'navigation') {
  static $menu_output = array();

  if (!isset($menu_output[$menu_name])) {
    $tree = menu_tree_page_data($menu_name);
    $menu_output[$menu_name] = bootstrap_menu_tree_output($tree);
  }
  return $menu_output[$menu_name];
}

function bootstrap_menu_tree_output($tree, $dropdown_menu = false) {
  $output = '';
  $items = array();

  // Pull out just the menu items we are going to render so that we
  // get an accurate count for the first/last classes.
  foreach ($tree as $data) {
    if (!$data['link']['hidden']) {
      $items[] = $data;
    }
  }

  $num_items = count($items);
  foreach ($items as $i => $data) {
    $extra_class = array();
    if ($i == 0) {
      $extra_class[] = 'first';
    }
    if ($i == $num_items - 1) {
      $extra_class[] = 'last';
    }
    $extra_class = implode(' ', $extra_class);
    if ($data['below']) {
      $link = theme('menu_item_link', $data['link'], true);
      $output .= theme('menu_item', $link, $data['link']['has_children'], bootstrap_menu_tree_output($data['below'], true), $data['link']['in_active_trail'], $extra_class);
    }
    else {
      $link = theme('menu_item_link', $data['link']);
      $output .= theme('menu_item', $link, $data['link']['has_children'], '', $data['link']['in_active_trail'], $extra_class);
    }
  }
  return $output ? theme('menu_tree', $output, $dropdown_menu) : '';
}





// theme_menu_tree()
function phptemplate_menu_tree($tree, $dropdown_menu = false) {
  $class = 'nav';
  if($dropdown_menu) $class = 'dropdown-menu';
  return '<ul class="'.$class.'">'. $tree .'</ul>';
}

// theme_menu_item()
function phptemplate_menu_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {
  $class = ($menu ? 'dropdown' : ($has_children ? 'dropdown' : ''));
  if (!empty($extra_class)) {
    $class .= ' '. $extra_class;
  }
  if ($in_active_trail) {
    $class .= ' active-trail';
  }
  return '<li class="'. $class .'">'. $link . $menu ."</li>\n";
}

function phptemplate_menu_item_link($link, $dropdown_toggle = false) {
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }

  if($dropdown_toggle) {
    // add class
    if (isset($link['localized_options']['attributes']['class'])) {
      $link['localized_options']['attributes']['class'] .= ' dropdown-toggle';
    }
    else {
      $link['localized_options']['attributes']['class'] = 'dropdown-toggle';
    }
    // add toggle
    $link['localized_options']['attributes']['data-toggle'] = 'dropdown';
    // add carat
    $link['title'] .= '<b class="caret"></b>';
    $link['localized_options']['html'] = true;
  }

  return l($link['title'], $link['href'], $link['localized_options']);
}
于 2013-02-25T02:40:40.697 回答