You are here

public function LoginDestinationManager::prepareDestination in Login Destination 8

Same name and namespace in other branches
  1. 8.2 src/LoginDestinationManager.php \Drupal\login_destination\LoginDestinationManager::prepareDestination()

Prepare request destination provided by login destination rule.

Parameters

\Drupal\login_destination\Entity\LoginDestination $destination: Login destination rule.

Overrides LoginDestinationManagerInterface::prepareDestination

File

src/LoginDestinationManager.php, line 158

Class

LoginDestinationManager
Defines a login destination manager service.

Namespace

Drupal\login_destination

Code

public function prepareDestination(LoginDestination $destination) {

  // Get config with settings.
  $config = $this->configFactory
    ->get('login_destination.settings');
  if ($config
    ->get('preserve_destination')) {

    // Get current destination value.
    $drupal_destination = $this->requestStack
      ->getCurrentRequest()->query
      ->get('destination');
    if (UrlHelper::isExternal($drupal_destination)) {
      $drupal_destination = NULL;
    }

    // Determine if a destination exist in the URL.
    if (!empty($drupal_destination)) {
      return;
    }
  }

  // Prepare destination path.
  $token_service = \Drupal::token();
  $path = $token_service
    ->replace($destination
    ->getDestination());

  // Check if rules refers to the current page.
  if ($destination
    ->isDestinationCurrent()) {
    $request = $this->requestStack
      ->getCurrentRequest();
    $query = $request
      ->get('current');
    $path_destination = !empty($query) ? $query : '';
  }
  else {
    $path_destination = Url::fromUri($path)
      ->toString();
  }

  // Set destination to current request.
  $this->requestStack
    ->getCurrentRequest()->query
    ->set('destination', $path_destination);
}