Is there any way that a link in a drupal menu can be opened in a new window?

Comments

vaishnavig’s picture

hi,
i am not getting exactly which links you want to be opened in a new window,but
you can use 'target' attribute in 'href' like

 <a href="a.php"  target="_blank" >go</a>

if you want the drupal menu links to be opened in a new window then you will have to change the module file of drupal.

gleble’s picture

I am trying to add a new item to a menu. The box doesn't allow html

anonymous07’s picture

subscribe

alan d.’s picture

This theme hook is used to hook all Drupal menu links. So you can parse the $link_item['path'] in D5 or the $link['href'] in D6 to determine what links to create the popup from.

<?php
/**
 * Generate the HTML representing a given menu item ID.
 *
 * @param $item
 *   The menu item to render.
 * @param $link_item
 *   The menu item which should be used to find the correct path.
 *
 * @ingroup themeable
 */

function phptemplate_menu_item_link($item, $link_item) {
  $options = !empty($item['description']) ? array('title' => $item['description']) : array();
  // DO CHECKS ON LINKS TO GENERATE THE TARGET
  if(TRUE) {
    $options['target'] = '_blank';
  }  
  return l($item['title'], $link_item['path'], $options, isset($item['query']) ? $item['query'] : NULL);
}


?>

The hook is slightly different in D6:

<?php
/**
 * Generate the HTML output for a single menu link.
 *
 * @ingroup themeable
 */
function phptemplate_menu_item_link($link) {
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }
  // DO CHECKS ON LINKS TO GENERATE THE TARGET
  if(TRUE) {
    $link['localized_options']['attributes']['target'] = '_blank';
  }
  return l($link['title'], $link['href'], $link['localized_options']);
}

?>

Enjoy :)


Alan Davison
www.caignwebs.com.au

Alan Davison
gleble’s picture

I've tried both of the examples and I get this error
warning: preg_match() expects parameter 2 to be string, array given in /var/www/drupal-5.7/includes/bootstrap.inc on line 670.
I have completed the function as
return l($link['Why Kingsnorth'], $link['http://eskvalleyenergy.org'], $link['localized_options']);
Is that correct?

alan d.’s picture

Arrays in php take this syntax;

$my_array['array_index'] = 'array value';

so instead of:

return l($link['Why Kingsnorth'], $link['http://eskvalleyenergy.org'], $link['localized_options']);

use:

$item['title'] = 'Why Kingsnorth';
$link['path'] = 'http://eskvalleyenergy.org';
etc...

I'd recommend reviewing array overview: http://www.php.net/manual/en/language.types.array.php


Alan Davison
www.caignwebs.com.au

Alan Davison
gleble’s picture

Nearly there, I've go to this

function phptemplate_menu_item_link($link) {
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }
  // DO CHECKS ON LINKS TO GENERATE THE TARGET
  if(TRUE) {
    $link['localized_options']['attributes']['target'] = '_blank';
  }
  return l($link['title'] = 'Why Kingsnorth', 
$link['path'] = 'http://eskvalleyenergy.org',
$link['localized_options']);
}
}

and I get this warning: preg_match() expects parameter 2 to be string, array given in /var/www/drupal-5.7/includes/bootstrap.inc on line 670.
All the links change to Why Kingnorth
The link opens in the same window.
Confused!!!

alan d.’s picture

The code would be something like: (only changes "eskvalleyenergy.org")

<?php
function phptemplate_menu_item_link($link) {
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }
  // DO CHECKS ON LINKS TO GENERATE THE TARGET
  if(strpos($link['path'] , 'eskvalleyenergy.org') !== FALSE) {
    $link['localized_options']['attributes']['target'] = '_blank';
  }
  return l($link['title'], $link['path'], $link['localized_options']);
}
}
?>

Alan Davison
www.caignwebs.com.au

Alan Davison
gleble’s picture

I still get the same error and the link still opens in the same window

alan d.’s picture

The first comment reply had the solution for both D5 and D6, and you are using the D6 function code bits after post 3. If you are using D5, modify the first function.

Eg:

<?php
function phptemplate_menu_item_link($item, $link_item) {
  $options = !empty($item['description']) ? array('title' => $item['description']) : array();
  // check on the domain eskvalleyenergy.org
  if(strpos($link['path'] , 'eskvalleyenergy.org') !== FALSE) {
    // uncomment this line to debug the condition.
    // die('Adding target');
    $options['target'] = '_blank';
  } 
  return l($item['title'], $link_item['path'], $options, isset($item['query']) ? $item['query'] : NULL);
}
?>

I ran the first set of functions on both D5.7 and D6.2, and they both worked on my install.


Alan Davison
www.caignwebs.com.au

Alan Davison
gleble’s picture

No error messages now but it still opens in the same window

alan d.’s picture

Did the die statement get called? (uncomment it)


Alan Davison
www.caignwebs.com.au

Alan Davison
alan d.’s picture

Duh. I forgot to change the test after copying it from the D6 version.

Change "if(strpos($link['path'] , 'eskvalleyenergy.org') !== FALSE) {" to "if(strpos($link_item['path'], 'eskvalleyenergy.org') !== FALSE) {"


Alan Davison
www.caignwebs.com.au

Alan Davison
gleble’s picture

Thanks very much. Got there at last.

sara2309’s picture

hai Alan Davison,

Thanks For doing this job Bcoz of you plz we are learn lot ...Good job ....

i am confused with this code .. actually i need to give popup for only one drop down link (Example link will be :http://google.com Size of 400 px 300px)but now it working for all link .. i dont know to solve this issue ,, i am new to drupal cms .. Wait for your reply

here is the code i had add in my theme tempalte.php

function mythemename_menu_item_link($item, $link_item) {
  $options = !empty($item['Experiences']) ? array('title' => $item['Experiences']) : array();
  // DO CHECKS ON LINKS TO GENERATE THE TARGET
  if(TRUE) {
    $options['target'] = '_blank';
  }  
  return l($item['title'], $link_item['path'], $options, isset($item['query']) ? $item['query'] : NULL);
}

Thanks & Regards
Muthu
India

alan d.’s picture

You need to change the link check

<?php
  // This is always added
  if(TRUE) {
    $options['target'] = '_blank';
  }
  // This is only added if the path is x/y/z
  if($link_item['path'] == 'x/y/z') {
    $options['target'] = '_blank';
  }

?>

Alan Davison
arnoldc’s picture

In drupal 6, $link['path'] doesn't exist, one should use $link['link_path'] or $link['href']

  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }
  // DO CHECKS ON LINKS TO GENERATE THE TARGET
  if(strpos($link['href'],'eskvalleyenergy.org') !== FALSE) {
    $link['localized_options']['attributes']['target'] = '_blank';
  }
  return l($link['title'], $link['href'], $link['localized_options']);
scarer’s picture

I inserted this code into my template.php file in my theme.


function phptemplate_menu_item_link($link) {
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }
  // DO CHECKS ON LINKS TO GENERATE THE TARGET
  if(strpos($link['link_path'] , 'http://writer.zoho.com/index.do') !== FALSE) {
    $link['localized_options']['attributes']['target'] = '_blank';
  }
  return l($link['title'], $link['link_path'], $link['localized_options']);
}

however, my local tabs don't seem to be working properly now. they seem to just return to the front page of the site. please help.

alan d.’s picture

Give $link['href'] a try, instead of $link['link_path']


Alan Davison
scarer’s picture

working fine now :)

dvkd’s picture

Hi Alan,

I have primary drop down menu in my site, i Need to open the link of primary items only in new window, and not the navigation munus.

Is it possible?

Thank u

alan d.’s picture

This method works well if u can distinguish between each. Otherwise I would create a new menu theming function, cloning the original functionality and then calling a custom theme_new_window_menu_links and add the target attribute.

You could also use JQuery too if the target is not critical for your site. For example, something like $('#primary-links a').attr('target', '_blank');


Alan Davison
dvkd’s picture

Hi Alan,

Can u please give the code ,as i am not full- fledged in drupal,

Thanks

alan d.’s picture

If I had the time..., the jquery way would be (cut into template, and replace #primary-links with the DIV ID of the menu you want to add the target to):

<script type="text/javascript">
<!--//--><![CDATA[//><!--
if (Drupal.jsEnabled) {
  $(document).ready(function() {
    $('#primary-links a').attr('target', '_blank');
  });
}
//--><!]]>
</script>

Alan Davison
emackn’s picture

You can use this for Drupal 6,
http://drupal.org/project/menu_attributes

notebene’s picture

subscribe

awolfey’s picture

if ($link['external']) {
  $link['localized_options']['attributes']['target'] = '_blank';
}
wickedskaman’s picture

Just in case people were still looking for this... it was never mentioned in this discussion that there is a great module for this... External Links: http://drupal.org/project/extlink

Also, there's a very sensible and simple JS solution here: http://mydrupal.com/external_links_in_new_window_with_message_box

Steve Ramos
www.zapcrunch.com

doublejosh’s picture

However Extlinks DOES NOT work on menu links.

noonway’s picture

While extlink is great for other links in the site, menu_attributes is what you want to use for menus. Great helper module! Thanks! Hope to see it go compatible with 7.x

wickedskaman’s picture

Thank you very much! That will be helpful in lots of situations.

Steve Ramos
www.zapcrunch.com

justinlevi’s picture

The following code added to the template.php file worked for me:

function phptemplate_menu_item_link($link) {
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }

  // CHECK IF THE LINK HAS 'HTTP' AND ADD _BLANK IF TRUE
  if( substr($link['href'], 0, 4) == 'http' ) {
    $link['localized_options']['attributes']['target'] = '_blank';
  }

  return l($link['title'], $link['href'], $link['localized_options']);
}
vinodh kumar1985’s picture

@above
Thanks that solved my problem after adding this code to my template.php

ccshannon’s picture

this isn't working. I've tried putting the theme override in my custom module. I tried putting it into my template.php.

I put debugging messages into it. Nothing triggers this thing.

When I did get it to show up, it only triggered on my local tabs, not my primary links.

Very frustrating. Not a good Drupal day, here.

deaconzero’s picture