class UnpublishedNodesRedirectOn403Subscriber in Unpublished Nodes Redirect 2.x
Unpublished Nodes Redirect On 403 Subscriber class.
Hierarchy
- class \Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
- class \Drupal\unpublished_nodes_redirect\EventSubscriber\UnpublishedNodesRedirectOn403Subscriber
Expanded class hierarchy of UnpublishedNodesRedirectOn403Subscriber
1 string reference to 'UnpublishedNodesRedirectOn403Subscriber'
1 service uses UnpublishedNodesRedirectOn403Subscriber
File
- src/
EventSubscriber/ UnpublishedNodesRedirectOn403Subscriber.php, line 15
Namespace
Drupal\unpublished_nodes_redirect\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
HttpExceptionSubscriberBase:: |
protected static | function | Specifies the priority of all listeners in this class. | 5 |
HttpExceptionSubscriberBase:: |
public static | function | Registers the methods in this class that should be listeners. | |
HttpExceptionSubscriberBase:: |
public | function | Handles errors for this subscriber. | 1 |
UnpublishedNodesRedirectOn403Subscriber:: |
protected | function |
Specifies the request formats this subscriber will respond to. Overrides HttpExceptionSubscriberBase:: |
|
UnpublishedNodesRedirectOn403Subscriber:: |
public | function | Fires redirects whenever a 403 meets the criteria for unpublished nodes. |