You are here

function noreferrer_link_alter in No Referrer 8

Implements hook_link_alter().

File

./noreferrer.module, line 15
No Referrer module.

Code

function noreferrer_link_alter(&$variables) {
  $config = \Drupal::config('noreferrer.settings');
  if ($config
    ->get('noopener') && isset($variables['options']['attributes']['target']) && $variables['options']['attributes']['target'] !== '') {
    if (!isset($variables['options']['attributes']['rel']) || is_array($variables['options']['attributes']['rel'])) {
      $variables['options']['attributes']['rel'][] = 'noopener';
    }
    else {
      $variables['options']['attributes']['rel'] = [
        $variables['options']['attributes']['rel'],
        'noopener',
      ];
    }
  }
  if (!$config
    ->get('noreferrer') || !$variables['url']
    ->isExternal() || noreferrer_is_whitelisted($variables['url']
    ->toString())) {
    return;
  }
  if (!isset($variables['options']['attributes']['rel']) || is_array($variables['options']['attributes']['rel'])) {
    $variables['options']['attributes']['rel'][] = 'noreferrer';
  }
  else {
    $variables['options']['attributes']['rel'] = [
      $variables['options']['attributes']['rel'],
      'noreferrer',
    ];
  }
}