1

I'm trying to add attributes for external links such as the rel="noopener noreferrer" for all the external links where target="_blank". I've tried using modules such as the noreferrer and sel, but neither affect the links in my views.

How can I add rel="noopener noreferrer" to all links globally where target="_blank"?

3
  • 1
    As for affecting links in Views, it depends on how the links are configured in Views. Are these links from a URL field being displayed? Or links from a page display being shown embedded in Views? Or links in custom text in the header/footer? – Patrick Kenny Feb 10 at 2:28
  • 1
    Also, the most popular module for this is Extlink. – Patrick Kenny Feb 10 at 2:28
  • Globally like also all links inside body text fields? – leymannx Feb 10 at 12:53
1

By implementing hook_link_alter in either a custom module or your theme.

The code (untested) would be similar to:

/**
 * Implements hook_link_alter().
 */
function MODULE/THEME_link_alter(&$variables) {
  if (isset($variables['options']['attributes']['target']) && $variables['options']['attributes']['target'] == '_blank') {
    $variables['options']['attributes']['rel'] = 'noopener noreferrer';
  }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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