External Links Not Recognized

Created on 28 November 2018, almost 7 years ago
Updated 11 August 2025, about 1 month ago

Recently implemented hook_link_alter to open all external links in a new window. This was done on Drupal 8.5.X and worked without issues. The implementation tested is the URL was external and if so, added the target="_blank" attribute.

Prior to moving code to production, Drupal core was updated to 8.6.3 (modules as well). QA on the site revealed that some external links were no longer opening in a new window/tab. Links such as those going to twitter or facebook had no issues. The links that were not being recognized were part of a third party integration and had the domain name included.

Example:
-- Domain Name: mysite.com
-- Links not being recognized as external: mysite.thirdparty.com/contact or mysite.thirdparty.com/form

It seems that when the site name appeared in the subdomain Drupal was not recognizing it as external. I verified this by printing out the link text for those links that Drupal deemed external ->

function hook_link_alter(&$variables) {
if ($variables['url']->isExternal()) {
debug('link text: ' . $variables['text']);
}
}

Links of the pattern [domain_name].thirdparty.com did not appear in debug statement.

πŸ› Bug report
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

base system

Created by

πŸ‡ΊπŸ‡ΈUnited States guerinteed_mike

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States dcam

    Moving to the base system per #2.

    But furthermore I can't reproduce the issue. If I enter URLs like https://mysite.com or https://mysite.thirdparty.com they are always considered to be external, which is how the Url class works. If I leave off the schema (https://), then I can't even save a link field due to validation errors. Therefore, this problem needs better steps to reproduce starting from a fresh install of Drupal Core.

Production build 0.71.5 2024