3 namespace Drupal\node\EventSubscriber;
5 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
6 use Drupal\Core\State\StateInterface;
7 use Drupal\migrate\Event\EventBase;
8 use Drupal\migrate\Event\MigrateEvents;
9 use Drupal\migrate\Event\MigrateImportEvent;
10 use Drupal\migrate\Event\MigratePostRowSaveEvent;
11 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14 * Creates a key value collection for migrated node translation redirections.
16 * If we are migrating node translations from Drupal 6 or 7, these nodes will be
17 * combined with their source node. Since there still might be references to the
18 * URLs of these now consolidated nodes, this service saves the mapping between
19 * the old nids to the new ones to be able to redirect them to the right node in
22 * The mapping is stored in the "node_translation_redirect" key/value collection
23 * and the redirection is made by the NodeTranslationExceptionSubscriber class.
25 * @see \Drupal\node\NodeServiceProvider
26 * @see \Drupal\node\EventSubscriber\NodeTranslationExceptionSubscriber
28 class NodeTranslationMigrateSubscriber implements EventSubscriberInterface {
31 * The key value factory.
33 * @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface
40 * @var \Drupal\Core\State\StateInterface
45 * Constructs the NodeTranslationMigrateSubscriber.
47 * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value
48 * The key value factory.
49 * @param \Drupal\Core\State\StateInterface $state
52 public function __construct(KeyValueFactoryInterface $key_value, StateInterface $state) {
53 $this->keyValue = $key_value;
54 $this->state = $state;
58 * Helper method to check if we are migrating translated nodes.
60 * @param \Drupal\migrate\Event\EventBase $event
64 * True if we are migrating translated nodes, false otherwise.
66 protected function isNodeTranslationsMigration(EventBase $event) {
67 $migration = $event->getMigration();
68 $source_configuration = $migration->getSourceConfiguration();
69 $destination_configuration = $migration->getDestinationConfiguration();
70 return !empty($source_configuration['translations']) && $destination_configuration['plugin'] === 'entity:node';
74 * Maps the old nid to the new one in the key value collection.
76 * @param \Drupal\migrate\Event\MigratePostRowSaveEvent $event
77 * The migrate post row save event.
79 public function onPostRowSave(MigratePostRowSaveEvent $event) {
80 if ($this->isNodeTranslationsMigration($event)) {
81 $row = $event->getRow();
82 $source = $row->getSource();
83 $destination = $row->getDestination();
84 $collection = $this->keyValue->get('node_translation_redirect');
85 $collection->set($source['nid'], [$destination['nid'], $destination['langcode']]);
90 * Set the node_translation_redirect state to enable the redirections.
92 * @param \Drupal\migrate\Event\MigrateImportEvent $event
93 * The migrate import event.
95 public function onPostImport(MigrateImportEvent $event) {
96 if ($this->isNodeTranslationsMigration($event)) {
97 $this->state->set('node_translation_redirect', TRUE);
104 public static function getSubscribedEvents() {
107 $events[MigrateEvents::POST_ROW_SAVE] = ['onPostRowSave'];
108 $events[MigrateEvents::POST_IMPORT] = ['onPostImport'];