You are here

class UnpublishedNodesRedirectSubscriber in Unpublished Nodes Redirect 8

Hierarchy

Expanded class hierarchy of UnpublishedNodesRedirectSubscriber

1 string reference to 'UnpublishedNodesRedirectSubscriber'
unpublished_nodes_redirect.services.yml in ./unpublished_nodes_redirect.services.yml
unpublished_nodes_redirect.services.yml
1 service uses UnpublishedNodesRedirectSubscriber
unpublished_node_redirect_event_subscriber in ./unpublished_nodes_redirect.services.yml
Drupal\unpublished_nodes_redirect\EventSubscriber\UnpublishedNodesRedirectSubscriber

File

src/EventSubscriber/UnpublishedNodesRedirectSubscriber.php, line 14

Namespace

Drupal\unpublished_nodes_redirect\EventSubscriber
View source
class UnpublishedNodesRedirectSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = array(
      'redirectUnpublishedNodes',
    );
    return $events;
  }

  /**
   * Fires redirects whenever the KernelEvents::RESPONSE event is dispatched.
   *
   * @param GetResponseEvent $event
   */
  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));
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UnpublishedNodesRedirectSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
UnpublishedNodesRedirectSubscriber::redirectUnpublishedNodes public function Fires redirects whenever the KernelEvents::RESPONSE event is dispatched.