You are here

class PrintEngineExceptionSubscriber in Entity Print 8.2

Exception event subscriber.

Hierarchy

Expanded class hierarchy of PrintEngineExceptionSubscriber

1 string reference to 'PrintEngineExceptionSubscriber'
entity_print.services.yml in ./entity_print.services.yml
entity_print.services.yml
1 service uses PrintEngineExceptionSubscriber
entity_print.print_engine_exception_subscriber in ./entity_print.services.yml
Drupal\entity_print\EventSubscriber\PrintEngineExceptionSubscriber

File

src/EventSubscriber/PrintEngineExceptionSubscriber.php, line 18

Namespace

Drupal\entity_print\EventSubscriber
View 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

Namesort descending Modifiers Type Description Overrides
PrintEngineExceptionSubscriber::$entityTypeManager protected property The entity type manager.
PrintEngineExceptionSubscriber::$routeMatch protected property The route match service.
PrintEngineExceptionSubscriber::getEntity protected function Gets a generic entity from the route data if it exists.
PrintEngineExceptionSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
PrintEngineExceptionSubscriber::getView protected function Gets the view from the route data if it exists.
PrintEngineExceptionSubscriber::handleException public function Handles print exceptions.
PrintEngineExceptionSubscriber::__construct public function PrintEngineExceptionSubscriber constructor.