diff --git a/src/Plugin/migrate/destination/EntityReferenceRevisions.php b/src/Plugin/migrate/destination/EntityReferenceRevisions.php index 647a1d8..ce891b6 100644 --- a/src/Plugin/migrate/destination/EntityReferenceRevisions.php +++ b/src/Plugin/migrate/destination/EntityReferenceRevisions.php @@ -77,8 +77,7 @@ protected function getEntity(Row $row, array $oldDestinationIdValues) { $revisionId = $oldDestinationIdValues ? array_pop($oldDestinationIdValues) : $row->getDestinationProperty($this->getKey('revision')); - if (!empty($entityId) && !empty($revisionId) && ($entities = $this->storage->loadByProperties([$this->getKey('id') => $entityId, $this->getKey('revision') => $revisionId]))) { - $entity = reset($entities); + if (!empty($entityId) && !empty($revisionId) && ($entity = $this->loadEntityReferenceRevision($entityId, $revisionId))) { $entity->setNewRevision(FALSE); } else { @@ -97,4 +96,68 @@ protected function getEntity(Row $row, array $oldDestinationIdValues) { return $entity; } + /** + * {@inheritdoc} + */ + public function rollback(array $destination_identifiers) { + if ($this->isTranslationDestination()) { + $this->rollbackTranslation($destination_identifiers); + } + else { + $this->rollbackNonTranslation($destination_identifiers); + } + } + + /** + * Rollback translation destinations. + * + * @param array $destination_identifiers + * The IDs of the destination object to delete. + */ + protected function rollbackTranslation(array $destination_identifiers) { + $entity = $this->loadEntityReferenceRevision(reset($destination_identifiers), array_pop($destination_identifiers)); + if ($entity && $entity instanceof TranslatableInterface) { + if ($key = $this->getKey('langcode')) { + if (isset($destination_identifier[$key])) { + $langcode = $destination_identifier[$key]; + if ($entity->hasTranslation($langcode)) { + // Make sure we don't remove the default translation. + $translation = $entity->getTranslation($langcode); + if (!$translation->isDefaultTranslation()) { + $entity->removeTranslation($langcode); + $entity->save(); + } + } + } + } + } + } + + /** + * Rollback non-translation destinations. + * + * @param array $destination_identifiers + * The IDs of the destination object to delete. + */ + protected function rollbackNonTranslation(array $destination_identifiers) { + $entity = $this->loadEntityReferenceRevision(reset($destination_identifiers), array_pop($destination_identifiers)); + if ($entity) { + $entity->delete(); + } + } + + /** + * Loads one entity reference revision entity. + * + * @param mixed $id + * The ID of the entity to load. + * + * @return \Drupal\Core\Entity\EntityInterface|null + * An entity object. NULL if no matching entity is found. + */ + public function loadEntityReferenceRevision($entityId, $revisionId) { + $entities = $this->storage->loadByProperties([$this->getKey('id') => $entityId, $this->getKey('revision') => $revisionId]); + return reset($entities) ?: NULL; + } + } diff --git a/tests/src/Kernel/Plugin/migrate/destination/EntityReferenceRevisionsDestinationTest.php b/tests/src/Kernel/Plugin/migrate/destination/EntityReferenceRevisionsDestinationTest.php index 837ced8..480ab48 100644 --- a/tests/src/Kernel/Plugin/migrate/destination/EntityReferenceRevisionsDestinationTest.php +++ b/tests/src/Kernel/Plugin/migrate/destination/EntityReferenceRevisionsDestinationTest.php @@ -67,6 +67,9 @@ public function testGetEntityTypeId() { * Tests get entity. * * @covers ::getEntity + * @covers ::rollback + * @covers ::rollbackNonTranslation + * @covers ::loadEntityReferenceRevision */ public function testGetEntity() { /** @var MigrationPluginManager $migrationManager */ @@ -78,13 +81,16 @@ public function testGetEntity() { for ($i = 0; $i < 2; $i++) { $migrationExecutable->import(); $migration->getIdMap()->prepareUpdate(); - $entities = \Drupal::entityManager()->getStorage('entity_test_composite')->loadByProperties(['id' => 1, 'revision_id' => 1]); - $entity = reset($entities); + $entity = $migration->getDestinationPlugin()->loadEntityReferenceRevision(1, 1); $this->assertEquals('content item 1', $entity->label()); - $entities = \Drupal::entityManager()->getStorage('entity_test_composite')->loadByProperties(['id' => 2, 'revision_id' => 2]); - $entity = reset($entities); + $entity = $migration->getDestinationPlugin()->loadEntityReferenceRevision(2, 2); $this->assertEquals('content item 2', $entity->label()); } + $migrationExecutable->rollback(); + $entity = $migration->getDestinationPlugin()->loadEntityReferenceRevision(1, 1); + $this->assertEmpty($entity); + $entity = $migration->getDestinationPlugin()->loadEntityReferenceRevision(2, 2); + $this->assertEmpty($entity); } /**