You are here

function external_link_popup_page_attachments in External Link Pop-up 8

Implements hook_page_attachments().

File

./external_link_popup.module, line 32
The main module file.

Code

function external_link_popup_page_attachments(array &$page) {
  $config = \Drupal::config('external_link_popup.settings');

  // Don't show on admin pages.
  if (!$config
    ->get('show_admin') && \Drupal::service('router.admin_context')
    ->isAdminRoute()) {
    return;
  }
  $storage = \Drupal::entityTypeManager()
    ->getStorage('external_link_popup');
  $entities = $storage
    ->loadByProperties([
    'status' => 1,
  ]);
  if (!$entities) {
    return;
  }
  uasort($entities, [
    ExternalLinkPopup::class,
    'sort',
  ]);
  $settings = [
    'whitelist' => $config
      ->get('whitelist'),
    'width' => $config
      ->get('width') ? implode('', $config
      ->get('width')) : '85%',
    'popups' => array_values($entities),
  ];
  $page['#attached']['drupalSettings']['external_link_popup'] = $settings;
  $page['#attached']['library'][] = 'external_link_popup/dialog';
  foreach ($storage
    ->getEntityType()
    ->getListCacheTags() as $cache_tag) {
    $page['#cache']['tags'][] = $cache_tag;
  }
}