Chris@0: configFactory = $config_factory; Chris@0: $this->accessManager = $access_manager; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected static function getPriority() { Chris@0: return -50; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function on403(GetResponseForExceptionEvent $event) { Chris@0: $custom_403_path = $this->configFactory->get('system.site')->get('page.403'); Chris@0: if (!empty($custom_403_path)) { Chris@0: $this->makeSubrequestToCustomPath($event, $custom_403_path, Response::HTTP_FORBIDDEN); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function on404(GetResponseForExceptionEvent $event) { Chris@0: $custom_404_path = $this->configFactory->get('system.site')->get('page.404'); Chris@0: if (!empty($custom_404_path)) { Chris@0: $this->makeSubrequestToCustomPath($event, $custom_404_path, Response::HTTP_NOT_FOUND); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Makes a subrequest to retrieve the custom error page. Chris@0: * Chris@0: * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event Chris@0: * The event to process. Chris@0: * @param string $custom_path Chris@0: * The custom path to which to make a subrequest for this error message. Chris@0: * @param int $status_code Chris@0: * The status code for the error being handled. Chris@0: */ Chris@0: protected function makeSubrequestToCustomPath(GetResponseForExceptionEvent $event, $custom_path, $status_code) { Chris@0: $url = Url::fromUserInput($custom_path); Chris@0: if ($url->isRouted()) { Chris@0: $access_result = $this->accessManager->checkNamedRoute($url->getRouteName(), $url->getRouteParameters(), NULL, TRUE); Chris@0: $request = $event->getRequest(); Chris@0: Chris@0: // Merge the custom path's route's access result's cacheability metadata Chris@0: // with the existing one (from the master request), otherwise create it. Chris@0: if (!$request->attributes->has(AccessAwareRouterInterface::ACCESS_RESULT)) { Chris@0: $request->attributes->set(AccessAwareRouterInterface::ACCESS_RESULT, $access_result); Chris@0: } Chris@0: else { Chris@0: $existing_access_result = $request->attributes->get(AccessAwareRouterInterface::ACCESS_RESULT); Chris@0: if ($existing_access_result instanceof RefinableCacheableDependencyInterface) { Chris@0: $existing_access_result->addCacheableDependency($access_result); Chris@0: } Chris@0: } Chris@0: Chris@0: // Only perform the subrequest if the custom path is actually accessible. Chris@0: if (!$access_result->isAllowed()) { Chris@0: return; Chris@0: } Chris@0: } Chris@0: Chris@0: $this->makeSubrequest($event, $custom_path, $status_code); Chris@0: } Chris@0: Chris@0: }