You are here

class CustomPageExceptionHtmlSubscriber in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/EventSubscriber/CustomPageExceptionHtmlSubscriber.php \Drupal\Core\EventSubscriber\CustomPageExceptionHtmlSubscriber

Exception subscriber for handling core custom HTML error pages.

Hierarchy

Expanded class hierarchy of CustomPageExceptionHtmlSubscriber

1 file declares its use of CustomPageExceptionHtmlSubscriber
CustomPageExceptionHtmlSubscriberTest.php in core/tests/Drupal/Tests/Core/EventSubscriber/CustomPageExceptionHtmlSubscriberTest.php
1 string reference to 'CustomPageExceptionHtmlSubscriber'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses CustomPageExceptionHtmlSubscriber
exception.custom_page_html in core/core.services.yml
Drupal\Core\EventSubscriber\CustomPageExceptionHtmlSubscriber

File

core/lib/Drupal/Core/EventSubscriber/CustomPageExceptionHtmlSubscriber.php, line 20

Namespace

Drupal\Core\EventSubscriber
View source
class CustomPageExceptionHtmlSubscriber extends DefaultExceptionHtmlSubscriber {

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The access manager.
   *
   * @var \Drupal\Core\Access\AccessManagerInterface
   */
  protected $accessManager;

  /**
   * Constructs a new CustomPageExceptionHtmlSubscriber.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
   *   The HTTP Kernel service.
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger service.
   * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
   *   The redirect destination service.
   * @param \Symfony\Component\Routing\Matcher\UrlMatcherInterface $access_unaware_router
   *   A router implementation which does not check access.
   * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
   *   The access manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, HttpKernelInterface $http_kernel, LoggerInterface $logger, RedirectDestinationInterface $redirect_destination, UrlMatcherInterface $access_unaware_router, AccessManagerInterface $access_manager) {
    parent::__construct($http_kernel, $logger, $redirect_destination, $access_unaware_router);
    $this->configFactory = $config_factory;
    $this->accessManager = $access_manager;
  }

  /**
   * {@inheritdoc}
   */
  protected static function getPriority() {
    return -50;
  }

  /**
   * {@inheritdoc}
   */
  public function on403(ExceptionEvent $event) {
    $custom_403_path = $this->configFactory
      ->get('system.site')
      ->get('page.403');
    if (!empty($custom_403_path)) {
      $this
        ->makeSubrequestToCustomPath($event, $custom_403_path, Response::HTTP_FORBIDDEN);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function on404(ExceptionEvent $event) {
    $custom_404_path = $this->configFactory
      ->get('system.site')
      ->get('page.404');
    if (!empty($custom_404_path)) {
      $this
        ->makeSubrequestToCustomPath($event, $custom_404_path, Response::HTTP_NOT_FOUND);
    }
  }

  /**
   * Makes a subrequest to retrieve the custom error page.
   *
   * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
   *   The event to process.
   * @param string $custom_path
   *   The custom path to which to make a subrequest for this error message.
   * @param int $status_code
   *   The status code for the error being handled.
   */
  protected function makeSubrequestToCustomPath(ExceptionEvent $event, $custom_path, $status_code) {
    $url = Url::fromUserInput($custom_path);
    if ($url
      ->isRouted()) {
      $access_result = $this->accessManager
        ->checkNamedRoute($url
        ->getRouteName(), $url
        ->getRouteParameters(), NULL, TRUE);
      $request = $event
        ->getRequest();

      // Merge the custom path's route's access result's cacheability metadata
      // with the existing one (from the master request), otherwise create it.
      if (!$request->attributes
        ->has(AccessAwareRouterInterface::ACCESS_RESULT)) {
        $request->attributes
          ->set(AccessAwareRouterInterface::ACCESS_RESULT, $access_result);
      }
      else {
        $existing_access_result = $request->attributes
          ->get(AccessAwareRouterInterface::ACCESS_RESULT);
        if ($existing_access_result instanceof RefinableCacheableDependencyInterface) {
          $existing_access_result
            ->addCacheableDependency($access_result);
        }
      }

      // Only perform the subrequest if the custom path is actually accessible.
      if (!$access_result
        ->isAllowed()) {
        return;
      }
    }
    $this
      ->makeSubrequest($event, $custom_path, $status_code);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CustomPageExceptionHtmlSubscriber::$accessManager protected property The access manager.
CustomPageExceptionHtmlSubscriber::$configFactory protected property The configuration factory.
CustomPageExceptionHtmlSubscriber::getPriority protected static function Specifies the priority of all listeners in this class. Overrides DefaultExceptionHtmlSubscriber::getPriority
CustomPageExceptionHtmlSubscriber::makeSubrequestToCustomPath protected function Makes a subrequest to retrieve the custom error page.
CustomPageExceptionHtmlSubscriber::on403 public function Handles a 403 error for HTML. Overrides DefaultExceptionHtmlSubscriber::on403
CustomPageExceptionHtmlSubscriber::on404 public function Handles a 404 error for HTML. Overrides DefaultExceptionHtmlSubscriber::on404
CustomPageExceptionHtmlSubscriber::__construct public function Constructs a new CustomPageExceptionHtmlSubscriber. Overrides DefaultExceptionHtmlSubscriber::__construct
DefaultExceptionHtmlSubscriber::$accessUnawareRouter protected property A router implementation which does not check access.
DefaultExceptionHtmlSubscriber::$httpKernel protected property The HTTP kernel.
DefaultExceptionHtmlSubscriber::$logger protected property The logger instance.
DefaultExceptionHtmlSubscriber::$redirectDestination protected property The redirect destination service.
DefaultExceptionHtmlSubscriber::getHandledFormats protected function Specifies the request formats this subscriber will respond to. Overrides HttpExceptionSubscriberBase::getHandledFormats
DefaultExceptionHtmlSubscriber::makeSubrequest protected function Makes a subrequest to retrieve the default error page.
DefaultExceptionHtmlSubscriber::on401 public function Handles a 401 error for HTML.
DefaultExceptionHtmlSubscriber::on4xx public function Handles a 4xx error for HTML.
HttpExceptionSubscriberBase::getSubscribedEvents public static function Registers the methods in this class that should be listeners.
HttpExceptionSubscriberBase::onException public function Handles errors for this subscriber. 1