I made a small change to theme_menu_item_link (includes/menu.inc) which will allow you to enter a static link to be displayed as a menu item.

Original:

function theme_menu_item_link($item, $link_item) {
  return l($item['title'], $link_item['path'], array_key_exists('description', $item) ? array('title' => $item['description']) : array());
}

New:

function theme_menu_item_link($item, $link_item) {
  $link = '';

  if(substr($link_item['path'], 0, 5) == "link:") {
    $link_item['path'] = substr($link_item['path'], 5);

    // This needs to be a new function in common.inc - such as "static_link" or such
    // static_link($item['title'], $link_item['path'], array_key_exists('description', $item) ? array('title' => $item['description']) : array());
    // -Start-
    $text = $item['title'];
    $url = substr($link_item['path'], 5);
    $attributes = array_key_exists('description', $item) ? array('title' => $item['description']) : array();
    $link = '<a href="/'. check_url($url) .'"'. drupal_attributes($attributes) .'>'. ($html ? $text : check_plain($text)) .'</a>';
    // -End-
  }
  else {
    $link = l($item['title'], $link_item['path'], array_key_exists('description', $item) ? array('title' => $item['description']) : array());
  }
  return $link;
}

Themed menu links will need to be updated as well (eg. mytheme_menu_link_item) as well as the Theme Engines.

So now, a menu item path can be: "link:hxxp://some.external.link/" and it will be rendered as an external link in the menu. Also, there is no need for the class="active" attribute since the link will never be active (i.e. if you click on it, you're going offsite).

Some improvement needs to be done if you want to make the link open in a new window.

I'm certain this is not the best solution. But I hope this may jumpstart the idea of having the ability to have static links in the menu (without creating a cumbersome module to handle another 2 tables, and 30 queries just to render a link in a nice fashion).

Comments

sepeck’s picture

You could wrap this as a patch and open a feature request against Drupal.

-sp
---------
Test site, always start with a test site.
Drupal Best Practices Guide -|- Black Mountain

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide

AlexanderV’s picture

Hello,

by chance I find that you only must activate "search engine friendly URLs" to use external links in menus.

Ciao

Alex

bhagman’s picture

Unfortunately, that enabling "search engine friendly URLs" is just a quick fix. The URL just happens to slip through the url() function because it just generates "relative" links. So essentially, you're generating a "relative" URL with a FQ URL as the path:

hxxp://my.website.com/hxxp://an.offsitelink.com/

that's how it would appear if it were absolute (which is a parameter for the url() function).

I'm still not convinced that my solution is the best, but it currently is the only way to put a static link directly into the menus (there are modules which generate nodes which point to links, but I think that's a bit of overkill for a simple external link in a menu).

AlexanderV’s picture

Hello,

a few days before I have implemented the same functionality like you, but on an other way. I don't know which way ist better.

Here is my solution for the problem:

function l($text, $path, $attributes = array(), $query = NULL, $fragment = NULL, $absolute = FALSE, $html = FALSE) {
  if (preg_match('/^http:\/\//',$path)) {
    return '<a href="/'. $path .'"'. drupal_attributes($attributes) .' target="_blank">'. ($html ? $text : check_plain($text)) .'</a>';
  } else {
    if (drupal_get_normal_path($path) == $_GET['q']) {
      if (isset($attributes['class'])) {
        $attributes['class'] .= ' active';
      }
      else {
        $attributes['class'] = 'active';
      }
    }
    return '<a href="/'. check_url(url($path, $query, $fragment, $absolute)) .'"'. drupal_attributes($attributes) .'>'. ($html ? $text : check_plain($text)) .'</a>';
  }
}

You must edit the function "l" in the common.inc. Ather the change you can use external links (everywhere?) and drupal identifies the link on the "http://" ahead.

I don't have any time to test it a long time. But after a quick test it works fine. If I test it longer, I can repost the results here if anyone want.

Sincerly,

Alex