You are here

protected function RedirectSubscriber::doRedirect in Url Redirect 8.2

Same name and namespace in other branches
  1. 8 src/EventSubscriber/RedirectSubscriber.php \Drupal\url_redirect\EventSubscriber\RedirectSubscriber::doRedirect()

Set response to redirection.

Parameters

GetResponseEvent $event:

2 calls to RedirectSubscriber::doRedirect()
RedirectSubscriber::exceptionRedirect in src/EventSubscriber/RedirectSubscriber.php
Perform redirect for access denied exceptions. Without this callback, if a user has a custom page to display on 403 (access denied) on admin/config/system/site-information, another redirection will take place before the redirection for the…
RedirectSubscriber::requestRedirect in src/EventSubscriber/RedirectSubscriber.php
Perform redirect for http request.

File

src/EventSubscriber/RedirectSubscriber.php, line 126

Class

RedirectSubscriber

Namespace

Drupal\url_redirect\EventSubscriber

Code

protected function doRedirect(GetResponseEvent $event) {
  global $base_url;
  $path_matches = FALSE;

  // Check URL path in url_redirect entity.
  $path = HTML::escape($event
    ->getRequest()
    ->getRequestUri());
  if ($path == '/') {
    $path = '<front>';
  }
  $wildcards = $this
    ->getPatterns();
  foreach ($wildcards as $wildcard_path) {
    $wildcard_path_load = $this->entityTypeManagerInterface
      ->getStorage('url_redirect')
      ->load($wildcard_path);
    $path_matches = \Drupal::service('path.matcher')
      ->matchPath($path, $wildcard_path_load
      ->get_path());
    if ($path_matches) {
      $wildcard_path_key = $wildcard_path;
      break;
    }
  }
  $url_redirect = $this
    ->getRedirect($path);
  if (!$url_redirect) {
    $url_redirect = $this
      ->getRedirect(substr($path, 1));
  }
  if ($url_redirect || $path_matches) {
    $id = array_keys($url_redirect);
    if (!$id) {
      $id[0] = $wildcard_path_key;
    }
    $successful_redirect = FALSE;

    /** @var \Drupal\url_redirect\Entity\UrlRedirect $url_redirect_load */
    $url_redirect_load = $this->entityTypeManagerInterface
      ->getStorage('url_redirect')
      ->load($id[0]);
    $check_for = $url_redirect_load
      ->get_checked_for();

    // Check for Role.
    if ($check_for == 'Role') {
      $role_check_array = $url_redirect_load
        ->get_roles();
      $user_role_check_array = $this->currentUser
        ->getRoles();
      $checked_result = array_intersect($role_check_array, $user_role_check_array);
      $checked_result = $url_redirect_load
        ->get('negate') ? $url_redirect_load
        ->get('negate') : $checked_result;
      if ($checked_result) {
        $successful_redirect = TRUE;
        if ($this
          ->url_redirect_is_external($url_redirect_load
          ->get_redirect_path())) {
          $event
            ->setResponse(new TrustedRedirectResponse($url_redirect_load
            ->get_redirect_path(), 301));
        }
        else {
          if (empty($url_redirect_load
            ->get_redirect_path()) || $url_redirect_load
            ->get_redirect_path() == '<front>') {
            $event
              ->setResponse(new TrustedRedirectResponse('<front>', 301));
          }
          else {
            $event
              ->setResponse(new TrustedRedirectResponse($base_url . '/' . $url_redirect_load
              ->get_redirect_path(), 301));
          }
        }
      }
    }
    elseif ($check_for == 'User') {
      $redirect_users = $url_redirect_load
        ->get_users();
      if ($redirect_users) {
        $uids = array_column($redirect_users, 'target_id', 'target_id');
        $uid_in_list = isset($uids[$this->currentUser
          ->id()]);
        $redirect_user = $url_redirect_load
          ->get('negate') ? $url_redirect_load
          ->get('negate') : $uid_in_list;
        if ($redirect_user) {
          $successful_redirect = TRUE;
          if ($this
            ->url_redirect_is_external($url_redirect_load
            ->get_redirect_path())) {
            $event
              ->setResponse(new TrustedRedirectResponse($url_redirect_load
              ->get_redirect_path(), 301));
          }
          else {
            if (empty($url_redirect_load
              ->get_redirect_path()) || $url_redirect_load
              ->get_redirect_path() == '<front>') {
              $event
                ->setResponse(new TrustedRedirectResponse('<front>', 301));
            }
            else {
              $event
                ->setResponse(new TrustedRedirectResponse($base_url . '/' . $url_redirect_load
                ->get_redirect_path(), 301));
            }
          }
        }
      }
    }
    if ($successful_redirect) {
      $message = $url_redirect_load
        ->get_message();
      if ($message == $this
        ->t('Yes')) {
        $this->messenger
          ->addMessage($this
          ->t("You have been redirected to '@link_path'.", array(
          '@link_path' => $url_redirect_load
            ->get_redirect_path(),
        )));
      }
    }
  }
}