我正在使用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>';
}
上述功能就像我希望它在网站的主导航上工作一样。但是,我不希望这个应用于我在侧边栏中使用Menu Block的main_menu实例。有没有办法有条件地使用them_menu_link基于区域或块菜单是bing渲染吗?
答案 0 :(得分:0)
所以我找到了解决这个问题的方法,但我不确定它是不是最好的方法。
在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>';
}
}