class PreviewLinkRouteEventSubscriber in Preview Link 2.x
Same name and namespace in other branches
- 2.0.x src/EventSubscriber/PreviewLinkRouteEventSubscriber.php \Drupal\preview_link\EventSubscriber\PreviewLinkRouteEventSubscriber
Modifies canonical entity routing to redirect to preview link.
Hierarchy
- class \Drupal\preview_link\EventSubscriber\PreviewLinkRouteEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses StringTranslationTrait
Expanded class hierarchy of PreviewLinkRouteEventSubscriber
1 string reference to 'PreviewLinkRouteEventSubscriber'
1 service uses PreviewLinkRouteEventSubscriber
File
- src/
EventSubscriber/ PreviewLinkRouteEventSubscriber.php, line 23
Namespace
Drupal\preview_link\EventSubscriberView source
class PreviewLinkRouteEventSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Provides helpers for redirect destinations.
*
* @var \Drupal\Core\Routing\RedirectDestinationInterface
*/
protected $redirectDestination;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* A logger instance.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* PreviewLinkRouteEventSubscriber constructor.
*
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
* @param \Drupal\Core\Routing\RedirectDestinationInterface $redirectDestination
* Provides helpers for redirect destinations.
* @param \Drupal\Core\StringTranslation\TranslationInterface $stringTranslation
* The string translation service.
* @param \Drupal\Core\Session\AccountInterface $currentUser
* The current user.
* @param \Psr\Log\LoggerInterface $logger
* The logger channel.
*/
public function __construct(MessengerInterface $messenger, RedirectDestinationInterface $redirectDestination, TranslationInterface $stringTranslation, AccountInterface $currentUser, LoggerInterface $logger) {
$this->messenger = $messenger;
$this->redirectDestination = $redirectDestination;
$this->stringTranslation = $stringTranslation;
$this->currentUser = $currentUser;
$this->logger = $logger;
}
/**
* Redirects from canonical routes to preview link route.
*
* Need to use GetResponseForExceptionEvent and getException method instead of
* ExceptionEvent::getThrowable() since these are in Symfony 4.4, and
* Drupal 8.9 supports Symfony 3.4.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* The exception event.
*/
public function onException(GetResponseForExceptionEvent $event) : void {
$exception = $event
->getException();
if ($exception instanceof PreviewLinkRerouteException) {
$entity = $exception
->getEntity();
$token = $exception
->getPreviewLink()
->getToken();
$previewLinkUrl = Url::fromRoute('entity.' . $entity
->getEntityTypeId() . '.preview_link', [
$entity
->getEntityTypeId() => $entity
->id(),
'preview_token' => $token,
]);
// Push the user back, but only if they have permission to view the
// canonical route.
// Redirect destination actually has the canonical route since thats
// where we are right now.
$removeUrl = Url::fromRoute('preview_link.session_tokens.remove');
$destination = $this->redirectDestination
->get();
try {
$canonicalUrl = Url::fromUserInput($destination);
if ($canonicalUrl
->access($this->currentUser)) {
$removeUrl
->setOption('query', $this->redirectDestination
->getAsArray());
}
} catch (\InvalidArgumentException $e) {
}
// Message is designed to only be visible on canonical -> preview link
// redirects, not on preview link routes accessed directly.
$this->messenger
->addMessage($this
->t('You are viewing this page because a preview link granted you access. Click <a href="@remove_session_url">here</a> to remove token.', [
'@remove_session_url' => $removeUrl
->toString(),
]));
$this->logger
->debug('Redirecting to preview link of @entity', [
'@entity' => $entity
->label(),
]);
// 307: temporary.
$response = (new TrustedRedirectResponse($previewLinkUrl
->toString(), TrustedRedirectResponse::HTTP_TEMPORARY_REDIRECT))
->addCacheableDependency($exception);
$event
->setResponse($response);
$event
->stopPropagation();
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() : array {
// Needs to be higher than ExceptionLoggingSubscriber::onError (priority 50)
// so exception is not logged. Larger numbers are earlier:
$events[KernelEvents::EXCEPTION][] = [
'onException',
51,
];
return $events;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PreviewLinkRouteEventSubscriber:: |
protected | property | The current user. | |
PreviewLinkRouteEventSubscriber:: |
protected | property | A logger instance. | |
PreviewLinkRouteEventSubscriber:: |
protected | property | The messenger service. | |
PreviewLinkRouteEventSubscriber:: |
protected | property | Provides helpers for redirect destinations. | |
PreviewLinkRouteEventSubscriber:: |
public static | function | ||
PreviewLinkRouteEventSubscriber:: |
public | function | Redirects from canonical routes to preview link route. | |
PreviewLinkRouteEventSubscriber:: |
public | function | PreviewLinkRouteEventSubscriber constructor. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |