You are here

public function ShurlySubscriber::shurlyOnRespond in ShURLy 8

File

src/ShurlySubscriber.php, line 21

Class

ShurlySubscriber

Namespace

Drupal\shurly

Code

public function shurlyOnRespond(GetResponseEvent $event) {
  $path = \Drupal::service('path.current')
    ->getPath();
  $current_path = str_replace("/", "", $path);
  if (shurly_validate_custom($current_path)) {
    $row = \Drupal::database()
      ->query("SELECT rid, destination, active FROM {shurly} WHERE source = :q ", [
      ':q' => $current_path,
    ])
      ->fetchObject();
    if ($row) {
      if ($row->active == 1) {
        $this
          ->shurlyRedirectTo($row);
      }
      else {

        // Check if the redirect page is defined. Otherwise the request will
        // be handled normally by drupal.
        $shurly_redirect_url = trim(\Drupal::config('shurly.settings')
          ->get('shurly_redirect_page'));
        if (!empty($shurly_redirect_url)) {
          $url = Url::fromUserInput($shurly_redirect_url);
          $response = new RedirectResponse($url
            ->toString());
          $response
            ->send();
          return;
        }
      }
    }
  }
}