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.

Latest Pretty Good Meditation Episode

Body Scan - 2021-02-21

Upcoming Events

Gentle Yoga (Freely Offered Class) -
Gentle Yoga (Yoga Class) -
Gentle Yoga (Yoga Class) -
Gentle Yoga (Yoga Class) -
Gentle Yoga (Yoga Class) -

Currently Reading

Cory Doctorow, How to Destroy Surveillance Capitalism (2021)

 

If you enjoy this content, sign up to receive email updates or subscribe via RSS.