You are here

redirect_after_logout.module in Redirect after logout 8

Same filename and directory in other branches
  1. 7 redirect_after_logout.module

File

redirect_after_logout.module
View source
<?php

/**
 * @file
 * Contains redirect_after_logout.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Component\Utility\Xss;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
 * Implements hook_help().
 */
function redirect_after_logout_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the redirect_after_logout module.
    case 'help.page.redirect_after_logout':
      return check_markup(file_get_contents(\Drupal::service('extension.list.module')
        ->getPath('redirect_after_logout') . "/README.txt"));
  }
}

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

/**
 * Implements hook_user_logout().
 */
function redirect_after_logout_user_logout($account) {
  if ($account
    ->hasPermission('redirect user after logout')) {
    $config = \Drupal::config('redirect_after_logout.settings');
    $logout_destination = $config
      ->get('destination');
    $token_service = \Drupal::token();
    $destination =& drupal_static(__FUNCTION__);
    $destination = $token_service
      ->replace($logout_destination);
  }
}