You are here

function redirect_after_logout_page_attachments in Redirect after logout 8

Implements hook_page_attachments().

File

./redirect_after_logout.module, line 28
Contains redirect_after_logout.module.

Code

function redirect_after_logout_page_attachments(array &$attachments) {

  // Set logout message.
  if (!empty($_GET['logout-message']) && \Drupal::currentUser()
    ->isAnonymous()) {
    $config = \Drupal::config('redirect_after_logout.settings');
    $logout_message = $config
      ->get('message');
    $token_service = \Drupal::token();
    \Drupal::messenger()
      ->addMessage(Xss::filter($token_service
      ->replace($logout_message)));
  }
  elseif (!empty($_GET['logout-message']) && !\Drupal::currentUser()
    ->isAnonymous()) {
    $destination = \Drupal::service('redirect.destination')
      ->getAsArray();
    $current_url = Url::fromRoute('<current>');
    $path = $current_url
      ->getInternalPath();
    $path_args = explode('/', $path);
    if (!empty($path_args)) {
      $destination = implode('/', $path_args);
    }
    $response = new RedirectResponse($destination);
    $response
      ->send();
  }
}