You are here

external_link_popup.module in External Link Pop-up 8

The main module file.

File

external_link_popup.module
View source
<?php

/**
 * @file
 * The main module file.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\external_link_popup\Entity\ExternalLinkPopup;

/**
 * Implements hook_help().
 */
function external_link_popup_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'external_link_popup.settings':
      $output = '<p>' . t('Settings on the page applies to all Pop-ups.') . '</p>';
      return $output;
    case 'entity.external_link_popup.collection':
      $output = '<p>' . t('Pop-ups list, ordered by weight.') . '</p>';
      $output .= '<p>' . t('The Pop-ups are checked from top to bottom, and when domain condition is passed the matching Pop-up is shown, further lookup is stopped.');
      $output .= t('In this way any Pop-up below a Pop-up with "*" domain condition will never be shown.') . '</p>';
      $output .= '<p><em>' . t('Be careful, huge count of the Pop-ups can decrease your site frontend performance. Disable unused Pop-ups.') . '</em></p>';
      return $output;
  }
}

/**
 * Implements hook_page_attachments().
 */
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;
  }
}