Mercurial > hg > isophonics-drupal-site
comparison core/modules/node/src/EventSubscriber/NodeTranslationExceptionSubscriber.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\node\EventSubscriber; | |
4 | |
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; | |
15 | |
16 /** | |
17 * Redirect node translations that have been consolidated by migration. | |
18 * | |
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. | |
23 * | |
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. | |
27 * | |
28 * @see \Drupal\node\NodeServiceProvider | |
29 * @see \Drupal\node\EventSubscriber\NodeTranslationMigrateSubscriber | |
30 */ | |
31 class NodeTranslationExceptionSubscriber implements EventSubscriberInterface { | |
32 | |
33 /** | |
34 * The key value factory. | |
35 * | |
36 * @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface | |
37 */ | |
38 protected $keyValue; | |
39 | |
40 /** | |
41 * The language manager. | |
42 * | |
43 * @var \Drupal\Core\Language\LanguageManagerInterface | |
44 */ | |
45 protected $languageManager; | |
46 | |
47 /** | |
48 * The URL generator. | |
49 * | |
50 * @var \Drupal\Core\Routing\UrlGeneratorInterface | |
51 */ | |
52 protected $urlGenerator; | |
53 | |
54 /** | |
55 * The state service. | |
56 * | |
57 * @var \Drupal\Core\State\StateInterface | |
58 */ | |
59 protected $state; | |
60 | |
61 /** | |
62 * Constructs the NodeTranslationExceptionSubscriber. | |
63 * | |
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 | |
69 * The URL generator. | |
70 * @param \Drupal\Core\State\StateInterface $state | |
71 * The state service. | |
72 */ | |
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; | |
78 } | |
79 | |
80 /** | |
81 * Redirects not found node translations using the key value collection. | |
82 * | |
83 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event | |
84 * The exception event. | |
85 */ | |
86 public function onException(GetResponseForExceptionEvent $event) { | |
87 $exception = $event->getException(); | |
88 | |
89 // If this is not a 404, we don't need to check for a redirection. | |
90 if (!($exception instanceof NotFoundHttpException)) { | |
91 return; | |
92 } | |
93 | |
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')) { | |
102 return; | |
103 } | |
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); | |
112 } | |
113 } | |
114 } | |
115 } | |
116 | |
117 /** | |
118 * {@inheritdoc} | |
119 */ | |
120 public static function getSubscribedEvents() { | |
121 $events = []; | |
122 | |
123 $events[KernelEvents::EXCEPTION] = ['onException']; | |
124 | |
125 return $events; | |
126 } | |
127 | |
128 } |