Wednesday, 15 June 2011

Drupal - how to modify only one instance of a menu with them_menu_link -



Drupal - how to modify only one instance of a menu with them_menu_link -

i'm using theme_menu_link modify menu, in case main_menu. evertything works expected, i'm using menu block render instance of main menu in sidebar on internal pages.

the problem is, want modifications show in instance of main_menu i'm using site's main navigation, not in instance i'm using in sidebar.

details:

here's theme_menu_link function

/** * returns custom html main navigation menu. * adds html element top level parent <li> elements * serve drop-down menu toggle. * * sets html wrapper sub menus, override above * them_menu_tree function, intended top level menus only. * * @param $variables * associative array containing: * - element: structured array info menu link. * * @ingroup themeable */ function mytheme_menu_link__main_menu($variables) { $element = $variables['element']; $child_menu = $element['#below']; $sub_menu = ''; // if current top level item has kid menus (sub menus) if ($child_menu) { // unset sub menu wrapper set above in mytheme_menu_tree__new_main_menu() unset($child_menu['#theme_wrappers']); // add together opening ul tag sub menu wrapper $sub_menu $sub_menu = '<ul class="menu sub-menu">'; // iterate on each sub menu item foreach ($child_menu $child_menu_item){ // add together sub menu item link html variable $child_menu_output = l($child_menu_item['#title'], $child_menu_item['#href']); // check see if item has title, sincle $element['#below'] returns things besides menu items if($child_menu_item['#title']) { // output each sub menu item's link , description, wrapped in <li> element $sub_menu .= '<li>' . $child_menu_output .'</li>'; } // end if shild menu item has title } // end foreach kid menu item // add together closing ul tag sub menu wrapper $sub_menu $sub_menu .= '</ul>'; } // output dummy link top level items $output = '<a href="#" class="nolink">' . $element['#title'] . '</a>'; homecoming '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . '</li>'; }

the above function works want work on site's main navigation. however, not want apply instance of main_menu i'm using in sidebar menu block. there way conditionally utilize them_menu_link based on part or block menu bing rendered it?

so figured out prepare one, i'm not sure if it's best way go it.

using krumo($variables) within theme_menu_link function able see each menu has array @ $variables['element']['#theme'], first item block menu in. main navigation: $variables['element']['#theme'][0] === 'menu_link__menu_block__1', , sidebar navigation: $variables['element']['#theme'][0] === 'menu_link__menu_block__2'.

once saw wasn't hard create if/else in mytheme_menu_link__main_menu() function, customizations main menu (outputting a href="#" top level links, etc), not sidebar menu.

so new , improved theme_menu_link function looks this:

function mytheme_menu_link__main_menu($variables) { $element = $variables['element']; $sub_menu = ''; // customize menu instance if main navigation, not sidebar navigation, etc. // need check block menu in, otherwise instances of main_menu treatment. if($element['#theme'][0] === 'menu_link__menu_block__1'){ $child_menu = $element['#below']; // if current top level item has kid menus (sub menus) if ($child_menu) { // unset sub menu wrapper set above in mytheme_menu_tree__new_main_menu() unset($child_menu['#theme_wrappers']); // add together opening ul tag sub menu wrapper $sub_menu $sub_menu = '<ul class="menu sub-menu">'; // iterate on each sub menu item foreach ($child_menu $child_menu_item){ // add together sub menu item link html variable $child_menu_output = l($child_menu_item['#title'], $child_menu_item['#href']); // check see if item has title, sincle $element['#below'] returns things besides menu items if($child_menu_item['#title']) { // output each sub menu item's link , description, wrapped in <li> element $sub_menu .= '<li>' . $child_menu_output .'</li>'; } // end if shild menu item has title } // end foreach kid menu item // add together closing ul tag sub menu wrapper $sub_menu $sub_menu .= '</ul>'; } // output dummy link top level items $output = '<a href="#" class="nolink">' . $element['#title'] . '</a>'; homecoming '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . '</li>'; } // otherwise if we're not in main navigation block, output normal menu else { if ($element['#below']) { // unset sub-menu wrapper set above in mytheme_menu_tree__main_menu() unset($element['#below']['#theme_wrappers']); // add together custom wrapper sub-menus css sanity , turn a profit $sub_menu = '<ul class="menu sub-menu">' . drupal_render($element['#below']) . '</ul>'; } $output = l($element['#title'], $element['#href'], $element['#localized_options']); homecoming '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . '</li>'; } }

drupal drupal-7 drupal-theming

No comments:

Post a Comment