3 namespace Drupal\node\EventSubscriber;
5 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
6 use Drupal\Core\Language\LanguageManagerInterface;
7 use Drupal\Core\ParamConverter\ParamNotConvertedException;
8 use Drupal\Core\Routing\UrlGeneratorInterface;
9 use Drupal\Core\State\StateInterface;
10 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11 use Symfony\Component\HttpFoundation\RedirectResponse;
12 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
13 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14 use Symfony\Component\HttpKernel\KernelEvents;
17 * Redirect node translations that have been consolidated by migration.
19 * If we migrated node translations from Drupal 6 or 7, these nodes are now
20 * combined with their source language node. Since there still might be
21 * references to the URLs of these now consolidated nodes, this service catches
22 * the 404s and try to redirect them to the right node in the right language.
24 * The mapping of the old nids to the new ones is made by the
25 * NodeTranslationMigrateSubscriber class during the migration and is stored
26 * in the "node_translation_redirect" key/value collection.
28 * @see \Drupal\node\NodeServiceProvider
29 * @see \Drupal\node\EventSubscriber\NodeTranslationMigrateSubscriber
31 class NodeTranslationExceptionSubscriber implements EventSubscriberInterface {
34 * The key value factory.
36 * @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface
41 * The language manager.
43 * @var \Drupal\Core\Language\LanguageManagerInterface
45 protected $languageManager;
50 * @var \Drupal\Core\Routing\UrlGeneratorInterface
52 protected $urlGenerator;
57 * @var \Drupal\Core\State\StateInterface
62 * Constructs the NodeTranslationExceptionSubscriber.
64 * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value
65 * The key value factory.
66 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
67 * The language manager.
68 * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
70 * @param \Drupal\Core\State\StateInterface $state
73 public function __construct(KeyValueFactoryInterface $key_value, LanguageManagerInterface $language_manager, UrlGeneratorInterface $url_generator, StateInterface $state) {
74 $this->keyValue = $key_value;
75 $this->languageManager = $language_manager;
76 $this->urlGenerator = $url_generator;
77 $this->state = $state;
81 * Redirects not found node translations using the key value collection.
83 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
84 * The exception event.
86 public function onException(GetResponseForExceptionEvent $event) {
87 $exception = $event->getException();
89 // If this is not a 404, we don't need to check for a redirection.
90 if (!($exception instanceof NotFoundHttpException)) {
94 $previous_exception = $exception->getPrevious();
95 if ($previous_exception instanceof ParamNotConvertedException) {
96 $route_name = $previous_exception->getRouteName();
97 $parameters = $previous_exception->getRawParameters();
98 if ($route_name === 'entity.node.canonical' && isset($parameters['node'])) {
99 // If the node_translation_redirect state is not set, we don't need to check
100 // for a redirection.
101 if (!$this->state->get('node_translation_redirect')) {
104 $old_nid = $parameters['node'];
105 $collection = $this->keyValue->get('node_translation_redirect');
106 if ($old_nid && $value = $collection->get($old_nid)) {
107 list($nid, $langcode) = $value;
108 $language = $this->languageManager->getLanguage($langcode);
109 $url = $this->urlGenerator->generateFromRoute('entity.node.canonical', ['node' => $nid], ['language' => $language]);
110 $response = new RedirectResponse($url, 301);
111 $event->setResponse($response);
120 public static function getSubscribedEvents() {
123 $events[KernelEvents::EXCEPTION] = ['onException'];