【问题标题】:Restrict menu tree to first level将菜单树限制在第一级
【发布时间】:2011-03-07 11:02:09
【问题描述】:

我一直在努力让我的主要链接只显示第一级条目(根)。我的 template.php 中有以下代码:

我尝试将 $level 变量更改为 0 但没有效果。我不知道在哪里(见鬼)停止递归。

function supertheme_navigation_links($menu_name, $level = 2) {
  // Don't even bother querying the menu table if no menu is specified.
  if (empty($menu_name)) {
    return array();
  }
  // Get the menu hierarchy for the current page.
  $tree_page = menu_tree_page_data($menu_name);
  // Also get the full menu hierarchy.
  $tree_all = menu_tree_all_data($menu_name);
  // Go down the active trail until the right level is reached.
  while ($level-- > 0 && $tree_page) {
    // Loop through the current level's items until we find one that is in trail.
    while ($item = array_shift($tree_page)) {
      if ($item['link']['in_active_trail']) {
        // If the item is in the active trail, we continue in the subtree.
        $tree_page = empty($item['below']) ? array() : $item['below'];
        break;
      }
    }
  }
  return supertheme_navigation_links_level($tree_page, $tree_all);
}


/**
* Helper function for supertheme_navigation_links to recursively create an array of links.
* (Both trees are required in order to include every menu item and active trail info.)
*/
function supertheme_navigation_links_level($tree_page, $tree_all) {
  $links = array();
  foreach ($tree_all as $key => $item) {
    $item_page = $tree_page[$key];
    $item_all = $tree_all[$key];
    if (!$item_all['link']['hidden']) {
      $l = $item_all['link']['localized_options'];
      $l['href'] = $item_all['link']['href'];
      $l['title'] = $item_all['link']['title'];
      if ($item_page['link']['in_active_trail']) {
        if (empty($l['attributes']['class'])) {
          $l['attributes']['class'] = 'active-trail';
        }
        else {
          $l['attributes']['class'] .= ' active-trail';
        }
      }
      if ($item_all['below']) {
        $l['children'] = supertheme_navigation_links_level($item_page['below'], $item_all['below']);
      }
      // Keyed with unique menu id to generate classes from theme_links().
      $links['menu-'. $item_all['link']['mlid']] = $l;
    }
  }
  return $links;
}
/**
* Return a themed set of links. (Extended to support multidimensional arrays of links.)
*/
function supertheme_links($links, $attributes = array('class' => 'links')) {
  $output = '';

  if (count($links) > 0) {
    $output = '<ul'. drupal_attributes($attributes) .'>';

    $num_links = count($links);
    $i = 1;

    foreach ($links as $key => $link) {
      $class = $key;

      // Add first, last and active classes to the list of links to help out themers.
      if ($i == 1) {
        $class .= ' first';
      }
      if ($i == $num_links) {
        $class .= ' last';
      }
      if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))) {
        $class .= ' active';
      }
      // Added: if the link has child items, add a haschildren class
      if (isset($link['children'])) {
        $class .= ' haschildren';
      }
      $output .= '<li'. drupal_attributes(array('class' => $class)) .'>';

      if (isset($link['href'])) {
        // Pass in $link as $options, they share the same keys.
        $output .= l($link['title'], $link['href'], $link);
      }
      else if (!empty($link['title'])) {
        // Some links are actually not links, but we wrap these in <span> for adding title and class attributes
        if (empty($link['html'])) {
          $link['title'] = check_plain($link['title']);
        }
        $span_attributes = '';
        if (isset($link['attributes'])) {
          $span_attributes = drupal_attributes($link['attributes']);
        }
        $output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
      }

      // Added: if the link has child items, print them out recursively
      if (isset($link['children'])) {
        $output .= "\n" . theme('links', $link['children'], array());
      }

      $i++;
      $output .= "</li>\n";
    }

    $output .= '</ul>';
  }

  return $output;
}
function supertheme_primary_links() {
  return supertheme_navigation_links(variable_get('menu_primary_links_source', 'primary-links'

)); }

【问题讨论】:

    标签: drupal menu


    【解决方案1】:

    3 厘米:

    • 当您说您尝试将 $level 更改为 0 时,不完全确定您的意思,但随后它不会进入循环:while ($level-- &gt; 0 &amp;&amp; $tree_page)

    • 为了能够停止递归,递归函数需要有一个带深度的参数。因此,在递归函数内部,您决定何时停止:if ($depth &lt;1) { return; },每次调用递归函数时,您都使用$depth-1 调用它

    • 如果您想要“开箱即用”此功能,请尝试http://drupal.org/project/menu_block,或从其代码中获得一些灵感:(请参阅menu_tree_depth_trim function

    【讨论】:

    • 对,用一个额外的参数修改了我的例程;奇迹般有效 !谢谢!
    【解决方案2】:

    我相信,如果您在菜单设置下将菜单设置为非扩展,Drupal 会为您处理其余部分。

    另一种选择是使用nice menus,主要用于显示可扩展菜单,但也可用于控制菜单扩展级别。

    【讨论】:

    • 其实是设置为非展开的;不确定您是否正确理解了这个问题。
    • 另外,我已经编写了所有代码,切换到 nicemenus 对我来说不是一个真正的选择。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    相关资源
    最近更新 更多