1

What I have

I currently have a menu with subitems printed out in my page.tpl like this:

in template.php

function THEMENAME_preprocess_page(&$vars, $hook) {

       $sitemap_tree = menu_tree_all_data('menu-sitemap');
       $vars['sitemap_expanded'] = menu_tree_output(i18n_menu_localize_tree($sitemap_tree));

}

then in page.tpl.php I have

print render($sitemap_expanded);

What I want

While this prints out the entire menu tree perfectly I need the links to be absolute in stead of relative (what they are now). Any way of doing this in a Drupal fashion without too much extra lines ?

Thanx all.

1

What you probably want to do is theme the menu links differently for this menu. In D7, you would be overriding this function:

/**
 * Returns HTML for a menu link and submenu.
 *
 * @param $variables
 *   An associative array containing:
 *   - element: Structured array data for a menu link.
 *
 * @ingroup themeable
 */
function theme_menu_link(array $variables) {
  $element = $variables['element'];
  $sub_menu = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

In menu_tree_output(), it also sets up per menu theme overrides, so, in your template.php file, you could create your own YOURTHEME_menu_link__MENU_NAME() function. On first flush, it could look something like this:

function YOURTHEME_menu_link__menu_sitemap(array $variables) {
  $element = $variables['element'];
  $sub_menu = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  $output = l($element['#title'], $element['#href'], array_merge($element['#localized_options'], array('absolute' => TRUE)));
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

but this is an intellectual exercise for me, not a real world one, since I don't have a D7 sandbox to test it out in but it looks pretty accurate, or at least a starting point.

  • 1 word: brilliant ! I did not run into any issues whatsoever. Your override worked perfectly. Brilliant ! A huge thank you from Sin City. – belgianwolfie Sep 5 '12 at 18:11
  • glad I could help out! – Jimajamma Sep 5 '12 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.