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.
Comments
Comment #2
cilefen commentedAs this is Drupal\Core\Url::isExternal the issue could be mis-categorized as the link module and may be "base system" instead.
Comment #10
dcam commentedMoving to the base system per #2.
But furthermore I can't reproduce the issue. If I enter URLs like
https://mysite.comorhttps://mysite.thirdparty.comthey are always considered to be external, which is how theUrlclass 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.Comment #12
quietone commentedThere has been no discussion here for 7 years. Including no reply for more information asked for 8 months ago.
Therefor, closing this issue. If you are experiencing this problem provide steps to reproduce the issue starting from "Install Drupal core" and set the status to 'Active'.