Chris@0: keyValue = $key_value; Chris@0: $this->languageManager = $language_manager; Chris@0: $this->urlGenerator = $url_generator; Chris@0: $this->state = $state; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Redirects not found node translations using the key value collection. Chris@0: * Chris@0: * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event Chris@0: * The exception event. Chris@0: */ Chris@0: public function onException(GetResponseForExceptionEvent $event) { Chris@0: $exception = $event->getException(); Chris@0: Chris@0: // If this is not a 404, we don't need to check for a redirection. Chris@0: if (!($exception instanceof NotFoundHttpException)) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $previous_exception = $exception->getPrevious(); Chris@0: if ($previous_exception instanceof ParamNotConvertedException) { Chris@0: $route_name = $previous_exception->getRouteName(); Chris@0: $parameters = $previous_exception->getRawParameters(); Chris@0: if ($route_name === 'entity.node.canonical' && isset($parameters['node'])) { Chris@0: // If the node_translation_redirect state is not set, we don't need to check Chris@0: // for a redirection. Chris@0: if (!$this->state->get('node_translation_redirect')) { Chris@0: return; Chris@0: } Chris@0: $old_nid = $parameters['node']; Chris@0: $collection = $this->keyValue->get('node_translation_redirect'); Chris@0: if ($old_nid && $value = $collection->get($old_nid)) { Chris@0: list($nid, $langcode) = $value; Chris@0: $language = $this->languageManager->getLanguage($langcode); Chris@0: $url = $this->urlGenerator->generateFromRoute('entity.node.canonical', ['node' => $nid], ['language' => $language]); Chris@0: $response = new RedirectResponse($url, 301); Chris@0: $event->setResponse($response); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function getSubscribedEvents() { Chris@0: $events = []; Chris@0: Chris@0: $events[KernelEvents::EXCEPTION] = ['onException']; Chris@0: Chris@0: return $events; Chris@0: } Chris@0: Chris@0: }