You are here

function extlink_page_attachments in External Links 8

Implements hook_page_attachments().

File

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

Code

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);
}