2014-10-28 26 views
0

我使用theme_menu_link来修改菜单,在这种情况下,main_menu。 Evertything按预期工作,但是我使用Menu Block在内部页面上的侧边栏中呈现主菜单的另一个实例。Drupal - 如何修改菜单中的一个实例与他们_menu_link

问题是,我只希望我的修改显示在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呈现它?

+0

没有菜单供您属性工作? https://www.drupal.org/project/menu_attributes – hyunkeln 2014-10-28 19:53:29

+0

我见过这个模块,但它似乎没有解决我想要做的:定制菜单标记(不只是菜单项属性),但范围那些自定义为特定菜单的单个实例。不过,我的答案在下文中有效。 – 2014-10-28 23:45:50

回答

0

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

theme_menu_link功能我能看到每个菜单有一个数组在$variables['element']['#theme'],其中第一个项目是,菜单是块内使用krumo($变量)因此,对于主导航:$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>'; 
    } 
} 
+1

您也可以尝试:'global $ _current_region; if(isset($ _ current_region)&& $ _current_region =='REGION_NAME'){ //您的代码 }' – Djouuuuh 2014-10-29 12:45:19

+0

谢谢!我更喜欢这个解决方案,因为它不是特定于特定的块名称,而是更具逻辑性和灵活性的区域。 – 2014-10-30 16:11:50