You are here

function _extlink_get_settings_from_config in External Links 8

Helper function to prepare extlink JS drupalSettings from configuration.

Parameters

\Drupal\Core\Config\Config $config: The extlink.settings config object.

Return value

array Associative array of extlink settings to use as JS drupalSettings.

2 calls to _extlink_get_settings_from_config()
ExternalSettingsJsController::extlinkJsFile in src/Controller/ExternalSettingsJsController.php
Creates response for external settings JS file.
extlink_page_attachments in ./extlink.module
Implements hook_page_attachments().

File

./extlink.module, line 76
This is the External Links module.

Code

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'),
  ];
}