keyValue = $key_value; $this->state = $state; } /** * Helper method to check if we are migrating translated nodes. * * @param \Drupal\migrate\Event\EventBase $event * The migrate event. * * @return bool * True if we are migrating translated nodes, false otherwise. */ protected function isNodeTranslationsMigration(EventBase $event) { $migration = $event->getMigration(); $source_configuration = $migration->getSourceConfiguration(); $destination_configuration = $migration->getDestinationConfiguration(); return !empty($source_configuration['translations']) && $destination_configuration['plugin'] === 'entity:node'; } /** * Maps the old nid to the new one in the key value collection. * * @param \Drupal\migrate\Event\MigratePostRowSaveEvent $event * The migrate post row save event. */ public function onPostRowSave(MigratePostRowSaveEvent $event) { if ($this->isNodeTranslationsMigration($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']]); } } /** * Set the node_translation_redirect state to enable the redirections. * * @param \Drupal\migrate\Event\MigrateImportEvent $event * The migrate import event. */ public function onPostImport(MigrateImportEvent $event) { if ($this->isNodeTranslationsMigration($event)) { $this->state->set('node_translation_redirect', TRUE); } } /** * {@inheritdoc} */ public static function getSubscribedEvents() { $events = []; $events[MigrateEvents::POST_ROW_SAVE] = ['onPostRowSave']; $events[MigrateEvents::POST_IMPORT] = ['onPostImport']; return $events; } }