I'd like to add a simple target="_blank" attribute to a few menu items.
What module do I need now (sigh) ?

Comments

JohnForsythe’s picture

I don't know of a module that does this, but you might want to take a look at these:

http://drupal.org/node/75192
http://drupal.org/node/64892

--
John Forsythe
Blamcast - Custom theme design and more...

doublejosh’s picture

Can't figure out a way to to it at the module level, but I went for a theme level solution for now...

function phptemplate_menu_item_link($link) {

  if($link['link_path'] == 'http://www.tableausoftware.com/community/forums') {
    	$link['options']['attributes']['target']='_blank';
  }
  return l($link['title'],$link['link_path'],array('attributes'=>$link['options']['attributes']) );

}

...however, be aware that this does not rebuild using all options that might be on the link... like fragments and queries, etc.

doublejosh’s picture

Here is the proper way to drop in from link overrides for special circumstances. (In my case target="_blank")

function MY-THEME_menu_item_link($link) {

  // Just an initializer
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }

  // If an item is a LOCAL TASK, render it as a tab
  if ($link['type'] & MENU_IS_LOCAL_TASK) {
    $link['title'] = '<span class="tab">' . check_plain($link['title']) . '</span>';
    $link['localized_options']['html'] = TRUE;
  }
  
  // ALTERING YOUR SPECIAL LINKS. I WAS LOOKING FOR TARGET=_BLANK
  // You can also do this by checking $link['path'] with a substring, but I opted just for integer comparison for now.

  if($link['mlid'] == 1210) { //http://www.tableausoftware.com/community/forums
    	$link['localized_options']['attributes']['target']='_blank';
  }
  
  // Proper way to return the link preserving all the extra options.
  return l($link['title'], $link['href'], $link['localized_options']);

}
Hedkandi’s picture

If you want to open ALL external links in a new window you could use $link['external'] to decide if you want to $link['localized_options']['attributes']['target']='_blank';

I did that in menu.inc and it works like a charm with just 3 extra lines of code.

oriol_e9g’s picture

My solution is a bit different. You can do something like this:

<?php
function phptemplate_menu_item_link($link) {

  if(preg_match("/http:/", $link['link_path'])) {
        $link['options']['attributes']['target']='_blank';
  }
  return l($link['title'],$link['link_path'],array('attributes'=>$link['options']['attributes']) );

}
?>

All external links have and absolute path with the "http:" sting in the url and are opened in a new page.

The internal links have a relative path without the "http:" sting in the url and are opened in the same page.

laevensv@gmail.com’s picture

message only to thank you for this code :)

gambry’s picture

For some reason the code above didn't work for me. I don't have time to check why so I've just recreated the function mixing oriol_e9g's code with theme_menu_item_link() (D6), of course inside template.php:

<?php
function YOURTHEMENAME_menu_item_link($link) {
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }
  if(preg_match("/http:/", $link['link_path'])) {
    $link['options']['attributes']['target']='_blank';
  }

  return l($link['title'], $link['href'], array_merge($link['localized_options'],array('attributes'=>$link['options']['attributes'])));
}
?>
dhendriks’s picture

It sounds like the Menu attributes module (http://drupal.org/project/menu_attributes) is exactly what you are looking for, although it is only available for Drupal 6+...

Swarnendu-Dutta’s picture

function themename_menu_link__menu_name(array $variables) {
  if($variables['element']['#href'] == 'http://www.test.com/') {
    $variables['element']['#localized_options']['attributes']['target'] = '_blank';
  }
return theme_menu_link($variables);
}

This worked for me.. !! :-)

~Swarnendu Dutta