Opening a menu link in a new window

November 20, 2011
 

In Drupal, it seems like it should be easy to configure external menu links to open in a new window, but I could not find clear instructions for doing so in Drupal 7. If this needs to be done in numerous places, the easiest way to get this done might be to install the External Links module. However, for a single link (menu item) I found the simplest and quickest way to get this done was to implement hook_menu_link_alter():


  function mymodule_menu_link_alter(&$item) {
    if ($item['link_path'] == 'http://example.com') {
      $item['options']['attributes']['target'] = '_blank';
    }
  }

That's it. I hope someone finds this helpful.

Comments

Newsletter

Did you enjoy this article? Sign up for my newsletter

Currently Reading

Johann Hari, Stolen Focus: Why You Can’t Pay Attention — and How to Think Deeply Again (2021)