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']);
}
?>
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?
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!!!
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.
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);
}
<?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';
}
?>
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');
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):
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
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
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']);
}
Comments
can use "target=_blank"
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
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.
menu
I am trying to add a new item to a menu. The box doesn't allow html
subscribe
subscribe
Use a theming function
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.The hook is slightly different in D6:
Enjoy :)
Alan Davison
www.caignwebs.com.au
Alan Davison
still stuck
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?
Array keys wrong
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
still stuck
Nearly there, I've go to this
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!!!
This would change all your links!
The code would be something like: (only changes "eskvalleyenergy.org")
Alan Davison
www.caignwebs.com.au
Alan Davison
Same error
I still get the same error and the link still opens in the same window
D5 or D6
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:
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
no new window
No error messages now but it still opens in the same window
Did the die statement get
Did the die statement get called? (uncomment it)
Alan Davison
www.caignwebs.com.au
Alan Davison
Duh. I forgot to change the
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
Bingo
Thanks very much. Got there at last.
primary link pop up But only one link in drop down
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
Thanks & Regards
Muthu
India
You need to change the link
You need to change the link check
Alan Davison
In drupal 6, $link['path']
In drupal 6, $link['path'] doesn't exist, one should use $link['link_path'] or $link['href']
my tabs don't seem to be working correctly now
I inserted this code into my template.php file in my theme.
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.
Give $link['href'] a try,
Give
$link['href']a try, instead of$link['link_path']Alan Davison
thank you!
working fine now :)
Hi Alan, I have primary drop
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
Same links or different links?
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
Hi Alan, Can u please give
Hi Alan,
Can u please give the code ,as i am not full- fledged in drupal,
Thanks
If I had the time..., the
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):
Alan Davison
Menu Attribute module for D6
You can use this for Drupal 6,
http://drupal.org/project/menu_attributes
subscribe
subscribe
All D6 external links
Just in case people were
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
However Extlinks DOES NOT
However Extlinks DOES NOT work on menu links.
Menu Attributes is the answer for 6.x
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
Thank you very much! That
Thank you very much! That will be helpful in lots of situations.
Steve Ramos
www.zapcrunch.com
Menu Link Add target _blank - menu_item_link hook solution
The following code added to the template.php file worked for me:
Thanks
@above
Thanks that solved my problem after adding this code to my template.php
Can't get it working
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.
Simple fix for D6
http://drupal.org/project/jquery_popupwindow works great.