diff --git a/src/Plugin/Derivative/MigrateEntityReferenceRevisions.php b/src/Plugin/Derivative/MigrateEntityReferenceRevisions.php new file mode 100644 index 0000000..90390ec --- /dev/null +++ b/src/Plugin/Derivative/MigrateEntityReferenceRevisions.php @@ -0,0 +1,26 @@ +entityDefinitions as $entityType => $entityInfo) { + if ($entityInfo->getKey('revision')) { + $this->derivatives[$entityType] = array( + 'id' => "entity_reference_revisions:$entityType", + 'class' => EntityReferenceRevisions::class, + 'requirements_met' => 1, + 'provider' => $entityInfo->getProvider(), + ); + } + } + return $this->derivatives; + } + +} diff --git a/src/Plugin/migrate/destination/EntityReferenceRevisions.php b/src/Plugin/migrate/destination/EntityReferenceRevisions.php new file mode 100644 index 0000000..2aaa6d9 --- /dev/null +++ b/src/Plugin/migrate/destination/EntityReferenceRevisions.php @@ -0,0 +1,88 @@ +save(); + + return [ + $this->getKey('id') => $entity->id(), + $this->getKey('revision') => $entity->getRevisionId(), + ]; + } + + /** + * {@inheritdoc} + */ + public function getIds() { + // TODO: return to parent::getIds() after https://www.drupal.org/node/2820886 is solved. + $ids = []; + $ids[$this->getKey('id')]['type'] = 'integer'; + + // TODO: Improve after https://www.drupal.org/node/2783715 is finished. + $ids[$this->getKey('revision')]['type'] = 'string'; + + return $ids; + } + + /** + * {@inheritdoc} + */ + protected function getEntity(Row $row, array $oldDestinationIdValues) { + $entityId = $oldDestinationIdValues ? + reset($oldDestinationIdValues) : + $row->getDestinationProperty($this->getKey('id')); + $revisionId = $oldDestinationIdValues ? + array_pop($oldDestinationIdValues) : + $row->getDestinationProperty($this->getKey('revision')); + if (!empty($entityId) && !empty($revisionId) && ($entity = $this->storage->loadByProperties([$this->getKey('id') => $entityId, $this->getKey('revision') => $revisionId]))) { + $entity->setNewRevision(FALSE); + } + else { + // Attempt to ensure we always have a bundle. + if ($bundle = $this->getBundle($row)) { + $row->setDestinationProperty($this->getKey('bundle'), $bundle); + } + + // Stubs might need some required fields filled in. + if ($row->isStub()) { + $this->processStubRow($row); + } + $entity = $this->storage->create($row->getDestination()); + $entity->enforceIsNew(); + } + return $entity; + } + +} diff --git a/tests/modules/err_migration_test/err_migration_test.info.yml b/tests/modules/err_migration_test/err_migration_test.info.yml new file mode 100644 index 0000000..d61822b --- /dev/null +++ b/tests/modules/err_migration_test/err_migration_test.info.yml @@ -0,0 +1,9 @@ +name: 'ERR migration test' +type: module +description: 'Entity migration test' +package: Testing +core: 8.x + +dependencies: + - entity_reference_revisions + - migrate diff --git a/tests/modules/err_migration_test/migration_templates/err_migration_test.yml b/tests/modules/err_migration_test/migration_templates/err_migration_test.yml new file mode 100644 index 0000000..21a5a6b --- /dev/null +++ b/tests/modules/err_migration_test/migration_templates/err_migration_test.yml @@ -0,0 +1,10 @@ +id: err_migration_test +migration_tags: {} +label: 'ERR Migration Test' +source: + plugin: dummy +process: + id +destination: + plugin: 'entity_reference_revisions:entity_test_composite' +migration_dependencies: { } diff --git a/tests/modules/err_migration_test/src/Plugin/migrate/source/DummySource.php b/tests/modules/err_migration_test/src/Plugin/migrate/source/DummySource.php new file mode 100644 index 0000000..9a7e5e2 --- /dev/null +++ b/tests/modules/err_migration_test/src/Plugin/migrate/source/DummySource.php @@ -0,0 +1,45 @@ + 1, 'revision_id' => 1, 'name' => 'content item 1'], + ]; + } +} diff --git a/tests/src/Kernel/Plugin/Derivative/EntityReferenceRevisionsDeriverTest.php b/tests/src/Kernel/Plugin/Derivative/EntityReferenceRevisionsDeriverTest.php new file mode 100644 index 0000000..d61b720 --- /dev/null +++ b/tests/src/Kernel/Plugin/Derivative/EntityReferenceRevisionsDeriverTest.php @@ -0,0 +1,49 @@ +installConfig($this->modules); + } + + /** + * Tests deriver. + * + * @covers ::getDerivativeDefinitions + */ + public function testDestinationDeriver() { + /** @var MigrationPluginManager $migrationManager */ + $migrationManager = \Drupal::service('plugin.manager.migration'); + /** @var MigrateDestinationPluginManager $migrationDestinationManager */ + $migrationDestinationManager = \Drupal::service('plugin.manager.migrate.destination'); + + $definition = $migrationManager->getDefinition('err_migration_test'); + $destination = $migrationDestinationManager->getDefinition($definition['destination']['plugin']); + $this->assertEquals(EntityReferenceRevisions::class, $destination['class']); + } + + + +} diff --git a/tests/src/Kernel/Plugin/migrate/destination/EntityReferenceRevisionsDestinationTest.php b/tests/src/Kernel/Plugin/migrate/destination/EntityReferenceRevisionsDestinationTest.php new file mode 100644 index 0000000..9f37da6 --- /dev/null +++ b/tests/src/Kernel/Plugin/migrate/destination/EntityReferenceRevisionsDestinationTest.php @@ -0,0 +1,45 @@ +getDefinition('err_migration_test'); + /** @var Migration $migration */ + $migration = $migrationManager->createStubMigration($definition); + /** @var EntityReferenceRevisions $destination */ + $destination = $migration->getDestinationPlugin(); + + /** @var EntityStorageBase $storage */ + $storage = $this->readAttribute($destination, 'storage'); + $actual = $this->readAttribute($storage, 'entityTypeId'); + + $expected = 'entity_test_composite'; + $this->assertEquals($expected, $actual); + } + +}