You are here

extlink.module in External Links 8

Same filename and directory in other branches
  1. 5 extlink.module
  2. 6 extlink.module
  3. 7 extlink.module

This is the External Links module.

File

extlink.module
View source
<?php

/**
 * @file
 * This is the External Links module.
 */
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\Config;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;

/**
 * Implements hook_modules_installed().
 */
function extlink_modules_installed($modules) {
  if (in_array('extlink', $modules)) {

    // Be friendly to your users: what to do after install?
    $url = Url::fromRoute('extlink_admin.settings');
    if (PHP_SAPI != 'cli') {
      \Drupal::messenger()
        ->addMessage(t('You can now <a href="@extlink_admin">configure the External Links module</a> for your site.', [
        '@extlink_admin' => $url
          ->toString(),
      ]), 'status');
    }
  }
}

/**
 * Implements hook_help().
 */
function extlink_help($route_name, RouteMatchInterface $arg) {
  switch ($route_name) {
    case 'help.page.extlink':
      $output = '';
      $output .= '<p>' . t('External Links is used to differentiate between internal and external links. Using jQuery, it will find all external links on a page and add an external icon indicating it will take you offsite or a mail icon for mailto links.') . '</p>';
      return [
        '#markup' => $output,
      ];
  }
}

/**
 * Implements hook_page_attachments().
 */
function extlink_page_attachments(array &$attachments) {
  $config = \Drupal::config('extlink.settings');

  // Checks to see if external link is enabled on admin routes.
  if ($config
    ->get('extlink_exclude_admin_routes') && \Drupal::service('router.admin_context')
    ->isAdminRoute()) {
    return;
  }

  // Add the extlink.settings config as a cacheable dependency.
  $cacheable_metadata = CacheableMetadata::createFromRenderArray($attachments);
  $cacheable_metadata
    ->addCacheableDependency($config);
  $cacheable_metadata
    ->applyTo($attachments);
  $attachments['#attached']['library'][] = 'extlink/drupal.extlink';
  if (!empty($config
    ->get('extlink_use_external_js_file'))) {

    // If using an external JS file, stop here.
    return;
  }
  $attachments['#attached']['drupalSettings']['data']['extlink'] = _extlink_get_settings_from_config($config);
}

/**
 * Helper function to prepare extlink JS drupalSettings from configuration.
 *
 * @param \Drupal\Core\Config\Config $config
 *   The extlink.settings config object.
 *
 * @return array
 *   Associative array of extlink settings to use as JS drupalSettings.
 */
function _extlink_get_settings_from_config(Config $config) {
  $settings = $config
    ->get();

  // Allow other modules to manipulate the settings.
  \Drupal::moduleHandler()
    ->alter('extlink_settings', $settings);

  // Allow other modules to alter the excluded CSS selector settings.
  \Drupal::moduleHandler()
    ->alter('extlink_css_exclude', $settings['extlink_css_exclude']);
  $callback = [
    Html::class,
    'cleanCssIdentifier',
  ];
  $fa_link_classes = $config
    ->get('extlink_font_awesome_classes.links') ?: 'fa fa-external-link';
  $fa_link_classes = array_map($callback, explode(' ', $fa_link_classes));
  $fa_mailto_classes = $config
    ->get('extlink_font_awesome_classes.mailto') ?: 'fa fa-envelope-o';
  $fa_mailto_classes = array_map($callback, explode(' ', $fa_mailto_classes));
  $defaultText = t('This link will take you to an external web site. We are not responsible for their content.');
  $extAlertText = $defaultText;
  if (!empty($settings['extlink_alert_text'])) {
    if (!is_array($settings['extlink_alert_text'])) {
      $extAlertText = isset($settings['extlink_alert_text']) ? Xss::filterAdmin($settings['extlink_alert_text']) : $defaultText;
    }
    else {
      $extAlertText = isset($settings['extlink_alert_text']['value']) ? Html::escape($settings['extlink_alert_text']['value']) : $defaultText;
    }
  }
  return [
    'extTarget' => isset($settings['extlink_target']) ? $settings['extlink_target'] : FALSE,
    'extTargetNoOverride' => isset($settings['extlink_target_no_override']) ? $settings['extlink_target_no_override'] : FALSE,
    'extNofollow' => isset($settings['extlink_nofollow']) ? $settings['extlink_nofollow'] : FALSE,
    'extNoreferrer' => isset($settings['extlink_noreferrer']) ? $settings['extlink_noreferrer'] : FALSE,
    'extFollowNoOverride' => isset($settings['extlink_follow_no_override']) ? $settings['extlink_follow_no_override'] : FALSE,
    'extClass' => isset($settings['extlink_class']) ? $settings['extlink_class'] : 'ext',
    'extLabel' => isset($settings['extlink_label']) ? Html::escape($settings['extlink_label']) : t('(link is external)'),
    'extImgClass' => isset($settings['extlink_img_class']) ? $settings['extlink_img_class'] : FALSE,
    'extSubdomains' => isset($settings['extlink_subdomains']) ? $settings['extlink_subdomains'] : TRUE,
    'extExclude' => isset($settings['extlink_exclude']) ? $settings['extlink_exclude'] : '',
    'extInclude' => isset($settings['extlink_include']) ? $settings['extlink_include'] : '',
    'extCssExclude' => isset($settings['extlink_css_exclude']) ? $settings['extlink_css_exclude'] : '',
    'extCssExplicit' => isset($settings['extlink_css_explicit']) ? $settings['extlink_css_explicit'] : '',
    'extAlert' => isset($settings['extlink_alert']) ? $settings['extlink_alert'] : FALSE,
    'extAlertText' => $extAlertText,
    'mailtoClass' => isset($settings['extlink_mailto_class']) ? $settings['extlink_mailto_class'] : 'mailto',
    'mailtoLabel' => isset($settings['extlink_mailto_label']) ? Html::escape($settings['extlink_mailto_label']) : t('(link sends email)'),
    'extUseFontAwesome' => isset($settings['extlink_use_font_awesome']) ? $settings['extlink_use_font_awesome'] : FALSE,
    'extIconPlacement' => !empty($settings['extlink_icon_placement']) ? $settings['extlink_icon_placement'] : 'append',
    'extFaLinkClasses' => implode(' ', $fa_link_classes),
    'extFaMailtoClasses' => implode(' ', $fa_mailto_classes),
    'whitelistedDomains' => $config
      ->get('whitelisted_domains'),
  ];
}

/**
 * Implements hook_library_info_alter().
 */
function extlink_library_info_alter(&$libraries, $extension) {
  if ($extension === 'extlink' && isset($libraries['drupal.extlink']) && \Drupal::config('extlink.settings')
    ->get('extlink_use_external_js_file')) {

    // Add the external settings JS file as a dependency to the drupal.extlink
    // library so that it will be loaded when configuration is set to use the
    // external file.
    $libraries['drupal.extlink']['dependencies'][] = 'extlink/extlink.settings';
  }
}

Functions

Namesort descending Description
extlink_help Implements hook_help().
extlink_library_info_alter Implements hook_library_info_alter().
extlink_modules_installed Implements hook_modules_installed().
extlink_page_attachments Implements hook_page_attachments().
_extlink_get_settings_from_config Helper function to prepare extlink JS drupalSettings from configuration.