You are here

public function UnpublishedNodesRedirectSubscriber::redirectUnpublishedNodes in Unpublished Nodes Redirect 8

Fires redirects whenever the KernelEvents::RESPONSE event is dispatched.

Parameters

GetResponseEvent $event:

File

src/EventSubscriber/UnpublishedNodesRedirectSubscriber.php, line 29

Class

UnpublishedNodesRedirectSubscriber

Namespace

Drupal\unpublished_nodes_redirect\EventSubscriber

Code

public function redirectUnpublishedNodes(FilterResponseEvent $event) {

  // Only act on nodes.
  if ($event
    ->getRequest()->attributes
    ->get('node') != NULL) {
    $node_type = $event
      ->getRequest()->attributes
      ->get('node')
      ->get('type')
      ->getString();
    $node_status = $event
      ->getRequest()->attributes
      ->get('node')
      ->get('status')
      ->getString();
    $config = \Drupal::config('unpublished_nodes_redirect.settings');
    $is_anonymous = \Drupal::currentUser()
      ->isAnonymous();

    // Get the redirect path for this node type.
    $redirect_path = $config
      ->get(Utils::getNodeTypeKey($node_type));

    // Get the response code for this node type.
    $response_code = $config
      ->get(Utils::getResponseCodeKey($node_type));
    if (Utils::checksBeforeRedirect($node_status, $is_anonymous, $redirect_path, $response_code)) {
      $event
        ->setResponse(new RedirectResponse($redirect_path, $response_code));
    }
  }
}