You are here

class UnpublishedNodesRedirectOn403Subscriber in Unpublished Nodes Redirect 2.x

Unpublished Nodes Redirect On 403 Subscriber class.

Hierarchy

Expanded class hierarchy of UnpublishedNodesRedirectOn403Subscriber

1 string reference to 'UnpublishedNodesRedirectOn403Subscriber'
unpublished_nodes_redirect.services.yml in ./unpublished_nodes_redirect.services.yml
unpublished_nodes_redirect.services.yml
1 service uses UnpublishedNodesRedirectOn403Subscriber
unpublished_node_redirect.exception403.subscriber in ./unpublished_nodes_redirect.services.yml
Drupal\unpublished_nodes_redirect\EventSubscriber\UnpublishedNodesRedirectOn403Subscriber

File

src/EventSubscriber/UnpublishedNodesRedirectOn403Subscriber.php, line 15

Namespace

Drupal\unpublished_nodes_redirect\EventSubscriber
View source
class UnpublishedNodesRedirectOn403Subscriber extends HttpExceptionSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function getHandledFormats() {
    return [
      'html',
    ];
  }

  /**
   * Fires redirects whenever a 403 meets the criteria for unpublished nodes.
   *
   * @param GetResponseForExceptionEvent $event
   *
   * @see Utils::checksBeforeRedirect for criteria relating to if a node
   * unpublished node should be redirected.
   *
   */
  public function on403(GetResponseForExceptionEvent $event) {
    if ($event
      ->getRequest()->attributes
      ->get('node') != NULL) {
      $nid = \Drupal::routeMatch()
        ->getRawParameter('node');
      $node = Node::load($nid);
      $node_type = $node
        ->getType();
      $is_published = $node
        ->isPublished();
      $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($is_published, $is_anonymous, $redirect_path, $response_code)) {
        $metadata = CacheableMetadata::createFromObject($node)
          ->addCacheableDependency($config)
          ->addCacheTags([
          'rendered',
        ]);
        $response = new TrustedRedirectResponse($redirect_path, $response_code);
        $response
          ->addCacheableDependency($metadata);

        // Set response as not cacheable, otherwise browser will cache it.
        $response
          ->setCache([
          'max_age' => 0,
        ]);
        $event
          ->setResponse($response);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HttpExceptionSubscriberBase::getPriority protected static function Specifies the priority of all listeners in this class. 5
HttpExceptionSubscriberBase::getSubscribedEvents public static function Registers the methods in this class that should be listeners.
HttpExceptionSubscriberBase::onException public function Handles errors for this subscriber. 1
UnpublishedNodesRedirectOn403Subscriber::getHandledFormats protected function Specifies the request formats this subscriber will respond to. Overrides HttpExceptionSubscriberBase::getHandledFormats
UnpublishedNodesRedirectOn403Subscriber::on403 public function Fires redirects whenever a 403 meets the criteria for unpublished nodes.