You are here

function bu_page_attachments in Browser update 8

Implements hook_page_attachments().

File

./bu.module, line 13
Drupal Module: Browser Update.

Code

function bu_page_attachments(array &$attachments) {

  // Load the configuration settings.
  $configurationSettings = \Drupal::config('bu.settings');

  // Add the javascript location.
  $drupalSettings = $configurationSettings
    ->get();

  // Set the default scripts if needed.
  if (empty($drupalSettings['source'])) {
    $drupalSettings['source'] = '//browser-update.org/update.min.js';
  }

  // Determine which pages this block appears on.
  $visibility_pages = $configurationSettings
    ->get('visibility_pages');

  // Verify visibility if it is set.
  if (!$configurationSettings
    ->get('test_mode') && !empty($visibility_pages)) {

    // Check to make sure the current page matches the allowed paths.
    $current_path = \Drupal::service('path.current')
      ->getPath();
    $match = \Drupal::service('path.matcher')
      ->matchPath($current_path, $visibility_pages);

    // If this page should not be showing the popup, exit.
    if ($configurationSettings
      ->get('visibility_type') == 'hide' && $match || $configurationSettings
      ->get('visibility_type') == 'show' && !$match) {
      return;
    }
  }

  // Attach the library.
  $attachments['#attached']['library'][] = 'bu/bu.checker';
  $attachments['#attached']['drupalSettings']['bu'] = $drupalSettings;
}