【问题标题】:Drupal - how to modify only one instance of a menu with them_menu_linkDrupal - 如何使用 them_menu_link 仅修改菜单的一个实例
【发布时间】:2014-10-28 19:47:20
【问题描述】:

我正在使用 theme_menu_link 来修改菜单,在本例中为 main_menu。 Evertything 按预期工作,但是我正在使用菜单块在内部页面的侧边栏中呈现主菜单的另一个实例。

问题是,我只希望我的修改显示在我用于网站主导航的 main_menu 实例中,而不是我在侧边栏中使用的实例中。

详情:

这是我的 theme_menu_link 函数

/**
 * Returns custom HTML for the main navigation menu.
 * Adds an HTML element to top level parent <li> elements
 * to serve as a drop-down menu toggle.
 *
 * this also sets the HTML wrapper for sub menus, to override the above
 * them_menu_tree function, which is intended for top level menus only.
 *
 * @param $variables
 *   An associative array containing:
 *   - element: Structured array data for a menu link.
 *
 * @ingroup themeable
 */
function mytheme_menu_link__main_menu($variables) {
  $element = $variables['element'];
  $child_menu = $element['#below'];
  $sub_menu = '';

  // if the current top level item has child menus (sub menus)
  if ($child_menu) {
    // unset the sub menu wrapper set above in mytheme_menu_tree__new_main_menu()
    unset($child_menu['#theme_wrappers']);

    // add opening ul tag for the sub menu wrapper to $sub_menu
    $sub_menu = '<ul class="menu sub-menu">';

    // iterate over each sub menu item
    foreach ($child_menu as $child_menu_item){

      // add sub menu item link HTML to a variable
      $child_menu_output = l($child_menu_item['#title'], $child_menu_item['#href']);

      // check to see if item has a title, sincle $element['#below'] returns things besides menu items
      if($child_menu_item['#title']) {
          // output each sub menu item's link and description, wrapped in a <li> element
          $sub_menu .= '<li>' . $child_menu_output .'</li>';
      } // end if shild menu item has a title

    } // end foreach child menu item

    // add closing ul tag for the sub menu wrapper to $sub_menu
    $sub_menu .= '</ul>';
  }

  // output a dummy link for top level items
  $output = '<a href="#" class="nolink">' . $element['#title'] . '</a>';
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . '</li>';

}

上面的功能就像我希望它在网站的主导航上工作一样。但是,我不希望这适用于我在带有菜单块的侧边栏中使用的 main_menu 实例。有没有办法根据区域有条件地使用 them_menu_link 或者阻止菜单被 bing 渲染呢?

【问题讨论】:

  • 菜单属性不适合您吗? drupal.org/project/menu_attributes
  • 我见过那个模块,但它似乎并没有解决我想要做的事情:自定义菜单标记(不仅仅是菜单项属性),但将这些自定义设置为单个实例的特定菜单。不过,我在下面的回答是有效的。

标签: drupal drupal-7 drupal-theming


【解决方案1】:

所以我想出了一个解决这个问题的方法,但我不确定这是否是最好的解决方法。

theme_menu_link 函数中使用 krumo($variables) 我可以看到每个菜单在$variables['element']['#theme'] 处都有一个数组,其中第一项是菜单所在的块。所以对于主导航: $variables['element']['#theme'][0] === 'menu_link__menu_block__1',对于侧边栏导航:$variables['element']['#theme'][0] === 'menu_link__menu_block__2'

一旦我看到在 mytheme_menu_link__main_menu() 函数中创建 if/else 并不难,我们在其中对主菜单进行自定义(输出 a href="#" 用于顶级链接等),但不是侧边栏菜单。

所以新的和改进的theme_menu_link 函数看起来像这样:

function mytheme_menu_link__main_menu($variables) {
  $element = $variables['element'];
  $sub_menu = '';
  // Only customize the menu instance if its the main navigation, not sidebar navigation, etc.
  // We need to check which block the menu is in, otherwise all instances of main_menu will get this treatment.
  if($element['#theme'][0] === 'menu_link__menu_block__1'){
    $child_menu = $element['#below'];
    // if the current top level item has child menus (sub menus)
    if ($child_menu) {
      // unset the sub menu wrapper set above in mytheme_menu_tree__new_main_menu()
      unset($child_menu['#theme_wrappers']);

      // add opening ul tag for the sub menu wrapper to $sub_menu
      $sub_menu = '<ul class="menu sub-menu">';

      // iterate over each sub menu item
      foreach ($child_menu as $child_menu_item){

        // add sub menu item link HTML to a variable
        $child_menu_output = l($child_menu_item['#title'], $child_menu_item['#href']);

        // check to see if item has a title, sincle $element['#below'] returns things besides menu items
        if($child_menu_item['#title']) {
            // output each sub menu item's link and description, wrapped in a <li> element
            $sub_menu .= '<li>' . $child_menu_output .'</li>';
        } // end if shild menu item has a title

      } // end foreach child menu item

      // add closing ul tag for the sub menu wrapper to $sub_menu
      $sub_menu .= '</ul>';
    }

    // output a dummy link for top level items
    $output = '<a href="#" class="nolink">' . $element['#title'] . '</a>';
    return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . '</li>';
    }

    // Otherwise if we're not in the main navigation block, output a normal menu
    else {
       if ($element['#below']) {
        // unset the sub-menu wrapper set above in mytheme_menu_tree__main_menu()
        unset($element['#below']['#theme_wrappers']);

        // Add a custom wrapper for sub-menus for CSS sanity and profit
        $sub_menu = '<ul class="menu sub-menu">' . drupal_render($element['#below']) . '</ul>';
      }

      $output = l($element['#title'], $element['#href'], $element['#localized_options']);
      return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . '</li>';
    }
}

【讨论】:

  • 你也可以试试:global $_current_region; if (isset($_current_region) &amp;&amp; $_current_region == 'REGION_NAME') { // YOUR CODE }
  • 谢谢!我更喜欢这个解决方案,因为它不是特定于特定的块名称,而是更符合逻辑和灵活的区域。
猜你喜欢
  • 2011-07-08
  • 1970-01-01
  • 2013-06-12
  • 2019-12-16
  • 1970-01-01
  • 1970-01-01
  • 2013-03-05
  • 2020-08-04
  • 1970-01-01
相关资源
最近更新 更多