Making a tab or other menu link a popup in Drupal 5

Contrary to what you might think, making a menu item/link a popup link is no easy matter in Drupal. I spent a good deal of time figuring out how to make a tab into a popup because one has to dig around in core to get results and it seems no one was able to do this right before or has at least not posted about it on Drupal.org, read on for the solution


Note: The below is for Drupal 5. The Drupal 6 writeup is here.

See, a menu link is not like a link in a node. It isn't possible to just easily change the source code for a link. If one were to dig deep enough, eventually the code for generating the HTML for a menu  link will come back to the theme_menu_item_link() in menu.inc. Now, one post on drupal.org got quite close http://drupal.org/node/89507#comment-458052 However, there is an easier way.

As stated in that post, open up your template.php file in your themes folder, or if you don't have one then create a template.php file in your themes folder and make sure to include the opening and closing php tags. In this file you will insert the following code:

 

yourthemename_menu_item_link($item, $link_item) {
  if($item['title'] == 'View') {
    return l($item['title'], $link_item['path'], !empty($item['description']) ? array('title' => $item['description'], 'target' => '_blank') : array('target' => '_blank'), isset($item['query']) ? $item['query'] : NULL);
  }
 else {
 return l($item['title'], $link_item['path'], !empty($item['description']) ? array('title' => $item['description']) : array(), isset($item['query']) ? $item['query'] : NULL);
 }
}

Of course replace 'View' with whatever link title that is wanted to make a popup, so 'edit' or 'revisions' would both work as they are tab links or in my case a custom 'Preview' tab. The if statement checks for the link title so that it only makes this changes on the link title specified. You could of course add some elseif in there if you needed more than one link changed. The else statement contains the original function exactly as it is found in menu.inc, which means all other links will stay exactly as they are now.

Hope this helps anyone who got stuck in the same boat as me. Basically, I have a client who wanted previews of the page to popup in a new window. Please let me know if you have any questions.

Comment using an existing account (Google, Twitter, etc.)