diff --git a/core/modules/node/node.services.yml b/core/modules/node/node.services.yml index a402537..631f1c7 100644 --- a/core/modules/node/node.services.yml +++ b/core/modules/node/node.services.yml @@ -52,6 +52,6 @@ services: - { name: 'context_provider' } node.node_translation_redirect_subscriber: class: Drupal\node\EventSubscriber\NodeTranslationRedirectSubscriber - arguments: ["@keyvalue", "@module_handler", "@module_installer"] + arguments: ["@database", "@module_handler", "@module_installer"] tags: - { name: event_subscriber } diff --git a/core/modules/node/src/EventSubscriber/NodeTranslationRedirectSubscriber.php b/core/modules/node/src/EventSubscriber/NodeTranslationRedirectSubscriber.php index a9613d6..f0965ba 100644 --- a/core/modules/node/src/EventSubscriber/NodeTranslationRedirectSubscriber.php +++ b/core/modules/node/src/EventSubscriber/NodeTranslationRedirectSubscriber.php @@ -2,9 +2,9 @@ namespace Drupal\node\EventSubscriber; +use Drupal\Core\Database\Connection; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleInstallerInterface; -use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; use Drupal\migrate\Event\EventBase; use Drupal\migrate\Event\MigrateEvents; use Drupal\migrate\Event\MigrateImportEvent; @@ -12,16 +12,16 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** - * Creates a key value collection for migrated node translation redirections. + * Populates the mapping table for migrated node translation redirections. */ class NodeTranslationRedirectSubscriber implements EventSubscriberInterface { /** - * The key value factory. + * The database service. * - * @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface + * @var \Drupal\Core\Database\Connection */ - protected $keyValue; + protected $database; /** * The module handler. @@ -40,15 +40,15 @@ class NodeTranslationRedirectSubscriber implements EventSubscriberInterface { /** * Construct a NodeTranslationSubscriber. * - * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value - * The key value factory. + * @param \Drupal\Core\Database\Connection $database + * The database connection. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer * The module installer. */ - public function __construct(KeyValueFactoryInterface $key_value, ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer) { - $this->keyValue = $key_value; + public function __construct(Connection $database, ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer) { + $this->database = $database; $this->moduleHandler = $module_handler; $this->moduleInstaller = $module_installer; } @@ -70,7 +70,7 @@ protected function isNodeTranslationsMigration(EventBase $event) { } /** - * Maps the old nid to the new one in the key value collection. + * Maps the old nid to the new one in the mapping table. * * @param \Drupal\migrate\Event\MigratePostRowSaveEvent $event * The migrate post row save event. @@ -80,8 +80,10 @@ public function onPostRowSave(MigratePostRowSaveEvent $event) { $row = $event->getRow(); $source = $row->getSource(); $destination = $row->getDestination(); - $collection = $this->keyValue->get('node_translation_redirect'); - $collection->set($source['nid'], [$destination['nid'], $destination['langcode']]); + $this->database->insert('node_translation_redirect') + ->fields(['source_nid', 'destination_nid', 'destination_langcode']) + ->values([$source['nid'], $destination['nid'], $destination['langcode']]) + ->execute(); } } @@ -91,7 +93,7 @@ public function onPostRowSave(MigratePostRowSaveEvent $event) { * @param \Drupal\migrate\Event\MigrateImportEvent $event * The migrate import event. */ - public function onPostImport(MigrateImportEvent $event) { + public function onPreImport(MigrateImportEvent $event) { if ($this->isNodeTranslationsMigration($event)) { if (!$this->moduleHandler->moduleExists('node_translation_redirect')) { $this->moduleInstaller->install(['node_translation_redirect']); @@ -107,7 +109,7 @@ public static function getSubscribedEvents() { if (class_exists('\Drupal\migrate\Event\MigrateEvents')) { $events[MigrateEvents::POST_ROW_SAVE] = ['onPostRowSave']; - $events[MigrateEvents::POST_IMPORT] = ['onPostImport']; + $events[MigrateEvents::PRE_IMPORT] = ['onPreImport']; } return $events; diff --git a/core/modules/node_translation_redirect/node_translation_redirect.install b/core/modules/node_translation_redirect/node_translation_redirect.install new file mode 100644 index 0000000..b8c7a87 --- /dev/null +++ b/core/modules/node_translation_redirect/node_translation_redirect.install @@ -0,0 +1,41 @@ + 'Maps migrated node translation nids for redirection purposes.', + 'fields' => [ + 'source_nid' => [ + 'description' => 'The source nid', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + ], + 'destination_nid' => [ + 'description' => 'The destination nid', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + ], + 'destination_langcode' => [ + 'description' => 'The destination langcode', + 'type' => 'varchar_ascii', + 'length' => 12, + 'not null' => TRUE, + 'default' => '', + ], + ], + 'primary key' => ['source_nid'], + ]; + + return $schema; +} diff --git a/core/modules/node_translation_redirect/node_translation_redirect.services.yml b/core/modules/node_translation_redirect/node_translation_redirect.services.yml index 985ea23..0b5a93d 100644 --- a/core/modules/node_translation_redirect/node_translation_redirect.services.yml +++ b/core/modules/node_translation_redirect/node_translation_redirect.services.yml @@ -1,7 +1,7 @@ services: node_translation_redirect.exception_subscriber: class: Drupal\node_translation_redirect\EventSubscriber\ExceptionSubscriber - arguments: ["@keyvalue", "@language_manager", "@url_generator"] + arguments: ["@database", "@language_manager", "@url_generator"] tags: - { name: event_subscriber } diff --git a/core/modules/node_translation_redirect/src/EventSubscriber/ExceptionSubscriber.php b/core/modules/node_translation_redirect/src/EventSubscriber/ExceptionSubscriber.php index db1d7d9..2fa64cf 100644 --- a/core/modules/node_translation_redirect/src/EventSubscriber/ExceptionSubscriber.php +++ b/core/modules/node_translation_redirect/src/EventSubscriber/ExceptionSubscriber.php @@ -2,7 +2,7 @@ namespace Drupal\node_translation_redirect\EventSubscriber; -use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; +use Drupal\Core\Database\Connection; use Drupal\Core\Routing\UrlGeneratorInterface; use Drupal\language\ConfigurableLanguageManagerInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -16,11 +16,11 @@ class ExceptionSubscriber implements EventSubscriberInterface { /** - * The key value factory. + * The database service. * - * @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface + * @var \Drupal\Core\Database\Connection */ - protected $keyValue; + protected $database; /** * The language manager. @@ -39,21 +39,21 @@ class ExceptionSubscriber implements EventSubscriberInterface { /** * Construct an ExceptionSubscriber. * - * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value - * The key value factory. + * @param \Drupal\Core\Database\Connection $database + * The database connection. * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager * The language manager. * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator * The URL generator. */ - public function __construct(KeyValueFactoryInterface $key_value, ConfigurableLanguageManagerInterface $language_manager, UrlGeneratorInterface $url_generator) { - $this->keyValue = $key_value; + public function __construct(Connection $database, ConfigurableLanguageManagerInterface $language_manager, UrlGeneratorInterface $url_generator) { + $this->database = $database; $this->languageManager = $language_manager; $this->urlGenerator = $url_generator; } /** - * Redirects not found node translations using the key value collection. + * Redirects not found node translations using the mapping table. * * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event; * The exception event. @@ -65,14 +65,18 @@ public function onException(GetResponseForExceptionEvent $event) { // Path aliases have already been migrated, so the path we're getting from // getPathInfo() is the internal path. if ($status_code == 404 && preg_match('/^\/node\/(\d+)$/', $path, $matches)) { - $collection = $this->keyValue->get('node_translation_redirect'); - $old_nid = isset($matches[1]) ? $matches[1] : NULL; - if ($old_nid && $collection->has($old_nid)) { - list($nid, $langcode) = $collection->get($old_nid); - $language = $this->languageManager->getLanguage($langcode); - $url = $this->urlGenerator->generateFromRoute('entity.node.canonical', ['node' => $nid], ['language' => $language]); - $response = new RedirectResponse($url); - $response->send(); + if (isset($matches[1])) { + $result = $this->database->select('node_translation_redirect', 'ntr') + ->fields('ntr', ['destination_nid', 'destination_langcode']) + ->condition('source_nid', $matches[1]) + ->execute() + ->fetchAssoc(); + if ($result) { + $language = $this->languageManager->getLanguage($result['destination_langcode']); + $url = $this->urlGenerator->generateFromRoute('entity.node.canonical', ['node' => $result['destination_nid']], ['language' => $language]); + $response = new RedirectResponse($url); + $response->send(); + } } } }