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