class PrintEngineExceptionSubscriber in Entity Print 8.2
Exception event subscriber.
Hierarchy
- class \Drupal\entity_print\EventSubscriber\PrintEngineExceptionSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of PrintEngineExceptionSubscriber
1 string reference to 'PrintEngineExceptionSubscriber'
1 service uses PrintEngineExceptionSubscriber
File
- src/
EventSubscriber/ PrintEngineExceptionSubscriber.php, line 18
Namespace
Drupal\entity_print\EventSubscriberView source
class PrintEngineExceptionSubscriber implements EventSubscriberInterface {
/**
* The route match service.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* PrintEngineExceptionSubscriber constructor.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* Route match service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type manager.
*/
public function __construct(RouteMatchInterface $routeMatch, EntityTypeManagerInterface $entityTypeManager) {
$this->routeMatch = $routeMatch;
$this->entityTypeManager = $entityTypeManager;
}
/**
* Handles print exceptions.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* The exception event.
*/
public function handleException(GetResponseForExceptionEvent $event) {
$exception = $event
->getException();
if ($exception instanceof PrintEngineException) {
\Drupal::messenger()
->addError(new FormattableMarkup($exception
->getPrettyMessage(), []));
if ($entity = $this
->getEntity()) {
$event
->setResponse(new RedirectResponse($entity
->toUrl()
->toString()));
}
elseif ($view = $this
->getView()) {
$display_id = $this->routeMatch
->getParameter('display_id');
/** @var \Drupal\views\ViewExecutable $executable */
$executable = $view
->getExecutable();
$executable
->setDisplay($display_id);
$url = $executable
->hasUrl() ? $executable
->getUrl()
->toString() : Url::fromRoute('<front>');
$event
->setResponse(new RedirectResponse($url));
}
}
}
/**
* Gets a generic entity from the route data if it exists.
*
* @return bool|\Drupal\Core\Entity\EntityInterface
* The entity or FALSE if it does not exist.
*/
protected function getEntity() {
$entity_type = $this->routeMatch
->getParameter('entity_type');
$entity_id = $this->routeMatch
->getParameter('entity_id');
return $entity_type && $entity_id ? $this->entityTypeManager
->getStorage($entity_type)
->load($entity_id) : FALSE;
}
/**
* Gets the view from the route data if it exists.
*
* @return bool|\Drupal\views\ViewEntityInterface
* The View or FALSE if it not a view route.
*/
protected function getView() {
$view_name = $this->routeMatch
->getParameter('view_name');
return $view_name ? $this->entityTypeManager
->getStorage('view')
->load($view_name) : FALSE;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
KernelEvents::EXCEPTION => 'handleException',
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PrintEngineExceptionSubscriber:: |
protected | property | The entity type manager. | |
PrintEngineExceptionSubscriber:: |
protected | property | The route match service. | |
PrintEngineExceptionSubscriber:: |
protected | function | Gets a generic entity from the route data if it exists. | |
PrintEngineExceptionSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
PrintEngineExceptionSubscriber:: |
protected | function | Gets the view from the route data if it exists. | |
PrintEngineExceptionSubscriber:: |
public | function | Handles print exceptions. | |
PrintEngineExceptionSubscriber:: |
public | function | PrintEngineExceptionSubscriber constructor. |