class RedirectSubscriber in Url Redirect 8.2
Same name and namespace in other branches
- 8 src/EventSubscriber/RedirectSubscriber.php \Drupal\url_redirect\EventSubscriber\RedirectSubscriber
Hierarchy
- class \Drupal\url_redirect\EventSubscriber\RedirectSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses StringTranslationTrait
Expanded class hierarchy of RedirectSubscriber
1 string reference to 'RedirectSubscriber'
1 service uses RedirectSubscriber
File
- src/EventSubscriber/ RedirectSubscriber.php, line 20 
Namespace
Drupal\url_redirect\EventSubscriberView source
class RedirectSubscriber implements EventSubscriberInterface {
  use StringTranslationTrait;
  /**
   * Current path stack service.
   *
   * @var \Drupal\Core\Path\CurrentPathStack
   */
  protected $currentPathStack;
  /**
   * Path matcher service.
   *
   * @var \Drupal\Core\Path\PathMatcher
   */
  protected $pathMatcher;
  /**
   * Entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManagerInterface;
  /**
   * Current user service.
   *
   * @var \Drupal\Core\Session\AccountProxy
   */
  protected $currentUser;
  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;
  /**
   * RedirectSubscriber constructor.
   *
   * @param \Drupal\Core\Path\CurrentPathStack $currentPathStack
   *  Current path stack service.
   * @param \Drupal\Core\Path\PathMatcher $pathMatcher
   *  Path matcher service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManagerInterface
   *  Entity type manager service.
   * @param \Drupal\Core\Session\AccountProxy $currentUser
   *  Current user service.
   * @param \Drupal\Core\StringTranslation\TranslationManager $stringTranslation
   *  Translation manager service.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *  The messenger.
   */
  public function __construct(CurrentPathStack $currentPathStack, PathMatcher $pathMatcher, EntityTypeManagerInterface $entityTypeManagerInterface, AccountProxy $currentUser, TranslationManager $stringTranslation, MessengerInterface $messenger) {
    $this->currentPathStack = $currentPathStack;
    $this->pathMatcher = $pathMatcher;
    $this->entityTypeManagerInterface = $entityTypeManagerInterface;
    $this->currentUser = $currentUser;
    $this->stringTranslation = $stringTranslation;
    $this->messenger = $messenger;
  }
  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    // This needs to be executed before RouterListener::onKernelRequest() which has 32
    // priority Otherwise, that aborts the request if no matching route is found.
    $events[KernelEvents::REQUEST][] = [
      'requestRedirect',
      33,
    ];
    $events[KernelEvents::EXCEPTION][] = [
      'exceptionRedirect',
      1,
    ];
    return $events;
  }
  /**
   * 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 KernelEvents::REQUEST event.
   * It results in infinite redirection and an error.
   *
   * @param GetResponseForExceptionEvent $event
   */
  public function exceptionRedirect(GetResponseForExceptionEvent $event) {
    $exception = $event
      ->getException();
    if ($exception instanceof HttpExceptionInterface && $event
      ->getException()
      ->getStatusCode() === 403) {
      $this
        ->doRedirect($event);
    }
  }
  /**
   * Perform redirect for http request.
   *
   * @param GetResponseEvent $event
   */
  public function requestRedirect(GetResponseEvent $event) {
    $this
      ->doRedirect($event);
  }
  /**
   * Set response to redirection.
   *
   * @param GetResponseEvent $event
   */
  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(),
          )));
        }
      }
    }
  }
  /**
   * Get redirection.
   *
   * @param string $path
   *  Source path.
   * @return array
   */
  protected function getRedirect($path) {
    $storage = $this->entityTypeManagerInterface
      ->getStorage('url_redirect');
    $queryResult = $storage
      ->getQuery()
      ->condition('path', $path)
      ->condition('status', 1)
      ->execute();
    return $queryResult;
  }
  /**
   * Get redirection.
   *
   * @param string $path
   *  Source path.
   * @return array
   */
  protected function getPatterns() {
    $storage = $this->entityTypeManagerInterface
      ->getStorage('url_redirect');
    $queryResult = $storage
      ->getQuery()
      ->condition("path", "*", "CONTAINS")
      ->condition('status', 1)
      ->execute();
    return $queryResult;
  }
  /**
   * Check for external URL.
   *
   * @param type $path
   * @return type
   */
  public function url_redirect_is_external($path) {
    // check for http:// or https://
    if (preg_match("`https?://`", $path)) {
      return TRUE;
    }
    else {
      return FALSE;
    }
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| RedirectSubscriber:: | protected | property | Current path stack service. | |
| RedirectSubscriber:: | protected | property | Current user service. | |
| RedirectSubscriber:: | protected | property | Entity type manager service. | |
| RedirectSubscriber:: | protected | property | The messenger. | |
| RedirectSubscriber:: | protected | property | Path matcher service. | |
| RedirectSubscriber:: | protected | function | Set response to redirection. | |
| RedirectSubscriber:: | public | function | 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:: | protected | function | Get redirection. | |
| RedirectSubscriber:: | protected | function | Get redirection. | |
| RedirectSubscriber:: | public static | function | Returns an array of event names this subscriber wants to listen to. | |
| RedirectSubscriber:: | public | function | Perform redirect for http request. | |
| RedirectSubscriber:: | public | function | Check for external URL. | |
| RedirectSubscriber:: | public | function | RedirectSubscriber constructor. | |
| StringTranslationTrait:: | protected | property | The string translation service. | 1 | 
| StringTranslationTrait:: | protected | function | Formats a string containing a count of items. | |
| StringTranslationTrait:: | protected | function | Returns the number of plurals supported by a given language. | |
| StringTranslationTrait:: | protected | function | Gets the string translation service. | |
| StringTranslationTrait:: | public | function | Sets the string translation service to use. | 2 | 
| StringTranslationTrait:: | protected | function | Translates a string to the current language or to a given language. | 
