You are here

function timezone_detect_page_attachments in Timezone Detect 8

Implements hook_page_attachments().

File

./timezone_detect.module, line 26
Module provides automatic timezone detection via javascript.

Code

function timezone_detect_page_attachments(array &$attachments) {

  // Include the javascript only when appropriate.
  if (isset($_SESSION['timezone_detect']['update_timezone']) || \Drupal::currentUser()
    ->isAuthenticated() && \Drupal::config('timezone_detect.settings')
    ->get('mode') === TimezoneDetectInterface::MODE_ALWAYS) {
    $account = \Drupal::currentUser()
      ->getAccount();

    // Store the current timezone for comparison.
    $timezone = $account
      ->getTimeZone();
    if (!empty($_SESSION['timezone_detect']['current_timezone'])) {
      $timezone = $_SESSION['timezone_detect']['current_timezone'];
    }
    $attachments['#attached']['library'][] = 'timezone_detect/init';
    $attachments['#attached']['drupalSettings']['timezone_detect'] = [
      'current_timezone' => $timezone,
      'token' => \Drupal::csrfToken()
        ->get(),
    ];
  }
}