class UnpublishedNodesRedirectSubscriber in Unpublished Nodes Redirect 8
Hierarchy
- class \Drupal\unpublished_nodes_redirect\EventSubscriber\UnpublishedNodesRedirectSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of UnpublishedNodesRedirectSubscriber
1 string reference to 'UnpublishedNodesRedirectSubscriber'
1 service uses UnpublishedNodesRedirectSubscriber
File
- src/
EventSubscriber/ UnpublishedNodesRedirectSubscriber.php, line 14
Namespace
Drupal\unpublished_nodes_redirect\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
UnpublishedNodesRedirectSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
UnpublishedNodesRedirectSubscriber:: |
public | function | Fires redirects whenever the KernelEvents::RESPONSE event is dispatched. |