You are here

function autologout_page_attachments in Automated Logout 8

Implements hook_page_attachments().

Add a form element to every page which is used to detect if the page was loaded from browser cache. This happens when the browser's back button is pressed for example. The JS will set the value of the hidden input element to 1 after initial load. If this is 1 on subsequent loads, the page was loaded from cache and an autologout timeout refresh needs to be triggered.

File

./autologout.module, line 186
Automated Logout - Module.

Code

function autologout_page_attachments(array &$page) {
  $autologout_manager = \Drupal::service('autologout.manager');

  // Check if JS should be included on this request.
  if ($autologout_manager
    ->preventJs()) {
    return;
  }

  // Check if anything wants to be refresh only. This URL would include the
  // javascript but will keep the login alive whilst that page is opened.
  $refresh_only = $autologout_manager
    ->refreshOnly();
  $settings = \Drupal::config('autologout.settings');

  // Get all settings JS will need for dialog.
  $timeout = $autologout_manager
    ->getUserTimeout();
  $timeout_padding = $settings
    ->get('padding');
  $redirect_url = $autologout_manager
    ->getUserRedirectUrl();
  $redirect_query = \Drupal::service('redirect.destination')
    ->getAsArray() + array(
    'autologout_timeout' => 1,
  );
  $no_dialog = $settings
    ->get('no_dialog');
  $disable_buttons = $settings
    ->get('disable_buttons');
  $yes_button = $settings
    ->get('yes_button');
  if (empty($yes_button)) {
    $yes_button = t('Yes');
  }
  $no_button = $settings
    ->get('no_button');
  if (empty($no_button)) {
    $no_button = t('No');
  }
  $use_alt_logout_method = $settings
    ->get('use_alt_logout_method');
  $title = $settings
    ->get('dialog_title');
  if (!$title) {
    $title = \Drupal::config('system.site')
      ->get('name') . ' Alert';
  }
  $msg = t(Xss::filter($settings
    ->get('message')));
  $logout_regardless_of_activity = $settings
    ->get('logout_regardless_of_activity');
  $settings = [
    'timeout' => $refresh_only ? $timeout * 500 : $timeout * 1000,
    'timeout_padding' => $timeout_padding * 1000,
    'message' => $msg,
    'redirect_url' => Url::fromUserInput($redirect_url, [
      'query' => $redirect_query,
    ])
      ->toString(),
    'title' => t($title),
    'refresh_only' => $refresh_only,
    'no_dialog' => $no_dialog,
    'disable_buttons' => $disable_buttons,
    'yes_button' => $yes_button,
    'no_button' => $no_button,
    'use_alt_logout_method' => $use_alt_logout_method,
    'logout_regardless_of_activity' => $logout_regardless_of_activity,
  ];

  // If this is an AJAX request, then the logout redirect url should still be
  // referring to the page that generated this request.
  $current_request = \Drupal::request();
  if ($current_request
    ->isXmlHttpRequest()) {
    $base_url = Url::fromRoute('<front>', [], [
      'absolute' => TRUE,
    ])
      ->toString();
    $referer = $current_request->headers
      ->get('referer');
    if ($referer) {
      $destination = str_replace($base_url . '/', '', $referer);
    }
    else {
      $destination = $base_url;
    }
    $settings['redirect_url'] = Url::fromUserInput($redirect_url, [
      'query' => [
        'destination' => urlencode($destination),
      ],
      'autologout_timeout' => 1,
    ]);
  }
  autologout_attach_js($page, $settings);
}