How to open all external links in a new tab I want to create a small module to open all external links in the menu in a new tab, how can I do that?

I think I should use hook_link_alter to do it, but i don't know how to do it so i can select all the external links only?
Links can be generated in a number of ways, which makes it hard to change consistently. For a single implementation that you want to reach most parts, you should swap the link_generator service with your own subclass. This should catch all menu items and links in general, but wont touch custom markup from wysiwyg. Here you would need to create a custom filter. 

The ::generate($text, Url $url) method is what is used to actually create the create the HTML. What you want to do, it to add the _blank target attribute if the url is external.

The code for the link generator subclass, could look like this:

class MyLinkGenerator extends LinkGenerator {
  public function generate($text, Url $url) {
    if ($url-&gt;isExternal()) {
      $attributes = $url-&gt;getOption('attributes');
      $attributes['target'] = '_blank';
      $url-&gt;setOption('attributes', $attributes);
    }
    return parent::generate($text, $url);
  }
}


I wont go into how to change the class of a service, there is plenty of examples for this here and elsewhere.