6

I'm using Drupal 8 with Bootstrap theme, and I've created a sub-theme and different menus.

I've tried to edit the menu twig template but i am not really familiar with Twig.

How can I add a menu where links open in new tabs?

5

In my case I utilized hook_link_alter() to accomplish this. Situation: in my menu links, I only need Contact Us to open in a new tab.

<?php
function MYMODULE_link_alter(&$variables) {
  if (!$variables['url']->isRouted()) {
    $uri = $variables['url']->getUri();
    // Only want the Contact Us to open in new tab.
    if ($uri == 'base:contact-us') {
      $variables['options']['attributes'] = ['target' => '_blank'];
    }
  }
}
2

Menu target module in dev version for D8 but you can check.

Allows privileged users to choose wether or not to open menu items in a new window. When enabled, users who have access to add or edit menu items, are provided the possibility to choose if the menu items should be opened in a new window or in the same window.

  • Menu target didn't work for me, as a matter of fact it gave a lot of trouble uninstalling because of deprecated functions. I advise against it. (I tried it as 8.x-1.x-dev last updated 2014-Mar-28) – mikeDOTexe Oct 25 '16 at 21:39
2

This one works fine for ALL external links:

function MYMODULE_link_alter(&$variables) {
  if ($variables['url']->isExternal()) {
    $variables['options']['attributes'] = ['target' => '_blank'];
  }
}
  • Menu link classes are lost when you rewrite the attributes array. $variables['options']['attributes']['target'] = '_blank'; looks like a valid solution but for some unknown reason a 0 is added to the link class. – JurgenR Aug 24 '17 at 12:32
  • Hi Roman, this was exactly what I needed. Now our content editors can create menu links as they want and don't have to bother with setting target _blank for each link when needed. I liked this so much that I just converted this into a drupal module, check out: drupal.org/project/external_links_new_tab – user33560 Jul 5 '18 at 9:13
  • Could putting a theme name in the place of MYMODULE work? What should the MYMODULE be if I would like all the links of my site to open a new tab? If it's just all the menus what should the MYMODULE be? – hiew1 Oct 23 '18 at 19:30
  • This would be awesome - but I created a module with your code, I enable it, but it does absolutely nothing. I know the code is running, because if I have a syntax error for example, it crashes and I see the error. I tried external links in menus, through a 'link' field in a content type, and hardcoded in a basic page. All open in the same tab. I re-saved all of them to check whether it would work after, but nope. Tried clearing caches, just in case - but nothing changes. Any help? Ideally - I would like something to work for all external links, wherever they are. Thanks for your time – thomas Oct 27 '18 at 20:27
1

Menu Link Attributes module solves this out of the box for Drupal 8. Have to be set for each link in the menu, though.

  • Thanks. This module worked perfectly for me. – sfmiller940 Sep 16 '19 at 1:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.