X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Fmodules%2Fnode%2Fsrc%2FEventSubscriber%2FNodeTranslationMigrateSubscriber.php;fp=web%2Fcore%2Fmodules%2Fnode%2Fsrc%2FEventSubscriber%2FNodeTranslationMigrateSubscriber.php;h=5911f0e05f796035ca4a7f3ed3bf61c0747d1272;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hp=0000000000000000000000000000000000000000;hpb=aea91e65e895364e460983b890e295aa5d5540a5;p=yaffs-website diff --git a/web/core/modules/node/src/EventSubscriber/NodeTranslationMigrateSubscriber.php b/web/core/modules/node/src/EventSubscriber/NodeTranslationMigrateSubscriber.php new file mode 100644 index 000000000..5911f0e05 --- /dev/null +++ b/web/core/modules/node/src/EventSubscriber/NodeTranslationMigrateSubscriber.php @@ -0,0 +1,113 @@ +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; + } + +}