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); } }