diff --git a/core/modules/migrate/src/Plugin/migrate/destination/EntityRevision.php b/core/modules/migrate/src/Plugin/migrate/destination/EntityRevision.php index 24602b6..76164e6 100644 --- a/core/modules/migrate/src/Plugin/migrate/destination/EntityRevision.php +++ b/core/modules/migrate/src/Plugin/migrate/destination/EntityRevision.php @@ -30,6 +30,24 @@ protected static function getEntityTypeId($plugin_id) { } /** + * Get the previous revision of an entity. + */ + protected function getPreviousRevision($entity_id, $revision_id) { + $query = $this->storage->getQuery(); + $query->condition($this->getKey('id'), $entity_id); + $query->condition($this->getKey('revision'), $revision_id, '<'); + $query->sort('vid', 'DESC'); + $query->range(0, 1); + $query->allRevisions(); + $ids = $query->execute(); + + if (empty($ids)) { + return NULL; + } + return $this->storage->loadRevision(key($ids)); + } + + /** * Gets the entity. * * @param \Drupal\migrate\Row $row @@ -46,22 +64,31 @@ protected function getEntity(Row $row, array $old_destination_id_values) { $row->getDestinationProperty($this->getKey('revision')); if (!empty($revision_id) && ($entity = $this->storage->loadRevision($revision_id))) { $entity->setNewRevision(FALSE); + $existing = TRUE; } else { $entity_id = $row->getDestinationProperty($this->getKey('id')); - $entity = $this->storage->load($entity_id); + $entity = $this->getPreviousRevision($entity_id, $revision_id); + } - // If we fail to load the original entity something is wrong and we need - // to return immediately. - if (!$entity) { - return FALSE; + if ($entity) { + $this->updateEntity($entity, $row); + } + else { + if ($row->isStub()) { + $this->processStubRow($row); } + $entity = $this->storage->create($row->getDestination()); + } + if (!isset($existing)) { $entity->enforceIsNew(FALSE); $entity->setNewRevision(TRUE); + // Reset the revision ID, since setNewRevision clears it. + $entity->set($this->getKey('revision'), $revision_id); + $entity->isDefaultRevision(FALSE); } - $this->updateEntity($entity, $row); - $entity->isDefaultRevision(FALSE); + return $entity; } diff --git a/core/modules/node/src/Plugin/migrate/source/d6/Node.php b/core/modules/node/src/Plugin/migrate/source/d6/Node.php index cd7bddc..e3ca482 100644 --- a/core/modules/node/src/Plugin/migrate/source/d6/Node.php +++ b/core/modules/node/src/Plugin/migrate/source/d6/Node.php @@ -61,7 +61,6 @@ public function query() { 'translate', )) ->fields('nr', array( - 'vid', 'title', 'body', 'teaser', @@ -96,6 +95,7 @@ protected function initializeIterator() { public function fields() { $fields = array( 'nid' => $this->t('Node ID'), + 'vid' => $this->t('Revision ID'), 'type' => $this->t('Type'), 'title' => $this->t('Title'), 'body' => $this->t('Body'), @@ -266,11 +266,15 @@ public function getIds() { * @param $query */ protected function addTranslationConditions(SelectInterface $query) { - // Do not include translations. - $condition = (new Condition('OR')) - ->where('n.tnid = n.nid') - ->condition('n.tnid', 0); - $query->condition($condition); + // Use the maximum vid for each translation set. + $subquery = $this->select('node', 'n'); + $subquery->addExpression("CASE n.tnid WHEN 0 THEN n.nid ELSE n.tnid END", + 'translation_set'); + $subquery->groupBy('translation_set'); + $subquery->addExpression('MAX(vid)', 'vid'); + + $query->innerJoin($subquery, 'max_vid', 'n.nid = max_vid.translation_set'); + $query->fields('max_vid', ['vid']); } } diff --git a/core/modules/node/src/Plugin/migrate/source/d6/NodeRevision.php b/core/modules/node/src/Plugin/migrate/source/d6/NodeRevision.php index 4a2bc01..faf8e35 100644 --- a/core/modules/node/src/Plugin/migrate/source/d6/NodeRevision.php +++ b/core/modules/node/src/Plugin/migrate/source/d6/NodeRevision.php @@ -40,7 +40,6 @@ public function prepareRow(Row $row) { public function fields() { // Use all the node fields plus the vid that identifies the version. return parent::fields() + array( - 'vid' => t('The primary identifier for this version.'), 'log' => $this->t('Revision Log message'), 'timestamp' => $this->t('Revision timestamp'), ); @@ -59,7 +58,10 @@ public function getIds() { * {@inheritdoc} */ protected function addTranslationConditions(SelectInterface $query) { - // Translations are included in the selection by default. + // Get revisions in order, so when we import a revision we can use previous + // ones. + $query->fields('nr', ['vid']); + $query->orderBy('vid', 'ASC'); } }