Loading src/Plugin/migrate/source/d8/ContentEntity.php +28 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace Drupal\migrate_drupal_d8\Plugin\migrate\source\d8; use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\State\StateInterface; use Drupal\migrate\MigrateException; use Drupal\migrate\Plugin\migrate\source\SqlBase; Loading Loading @@ -109,6 +110,15 @@ class ContentEntity extends SqlBase { } } // Base fields that have a cardinality greater then 1 are their own // dedicated tables. We need to gather those names too. But they are in code // so this is the only way to get those names. They aren't stored in the DB. foreach ($this->entityFieldManager->getBaseFieldDefinitions($this->configuration['entity_type']) as $fieldName => $definition) { if ($this->shouldMigrateBaseFieldDefinition($fieldName, $definition)) { $fields[$fieldName] = $fieldName; } } return $fields; } Loading Loading @@ -310,4 +320,22 @@ class ContentEntity extends SqlBase { ] + $fieldDefinition->getSettings(); } /** * Determine if a base field definition should be migrated. * * @param string $fieldName * The name of the field to evaluate. * @param \Drupal\Core\Field\FieldDefinitionInterface $definition * The base field definition to evaluate. * * @return bool * Return TRUE if the given base field definition should be migrated; FALSE * otherwise. */ protected function shouldMigrateBaseFieldDefinition($fieldName, $definition) { return $definition instanceof FieldStorageDefinitionInterface && $definition->isMultiple() && ($definition->isComputed() === FALSE); } } src/Plugin/migrate/source/d8/TaxonomyTerm.php +3 −0 Original line number Diff line number Diff line Loading @@ -11,6 +11,9 @@ use Drupal\migrate\Row; /** * Drupal 8 taxonomy_term source from database. * * @deprecated Instead use * \Drupal\migrate_drupal_d8\Plugin\migrate\source\d8\ContentEntity. * * @MigrateSource( * id = "d8_taxonomy_term", * source_provider = "migrate_drupal_d8" Loading tests/src/Kernel/ContentEntityTest.php +12 −39 Original line number Diff line number Diff line Loading @@ -4,14 +4,14 @@ namespace Drupal\Tests\migrate_drupal_d8\Kernel; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait; use Drupal\file\Entity\File; use Drupal\media\Entity\Media; use Drupal\media\Entity\MediaType; use Drupal\node\Entity\Node; use Drupal\node\Entity\NodeType; use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Vocabulary; use Drupal\Tests\field\Traits\EntityReferenceTestTrait; use Drupal\Tests\media\Traits\MediaTypeCreationTrait; use Drupal\Tests\migrate\Kernel\MigrateTestBase; use Drupal\user\Entity\User; Loading @@ -24,6 +24,7 @@ use Drupal\user\Entity\User; class ContentEntityTest extends MigrateTestBase { use EntityReferenceTestTrait; use MediaTypeCreationTrait; /** * {@inheritdoc} Loading Loading @@ -99,7 +100,7 @@ class ContentEntityTest extends MigrateTestBase { $this->installSchema('user', 'users_data'); $this->installSchema('file', 'file_usage'); $this->installConfig($this->modules); $this->installConfig(self::$modules); // Create a vocabulary. $vocabulary = Vocabulary::create([ Loading Loading @@ -134,7 +135,7 @@ class ContentEntityTest extends MigrateTestBase { ); // Create a media type. $mediaType = $this->createMediaType('test'); $mediaType = $this->createMediaType('file', ['id' => 'image']); // Create some data. $this->user = User::create([ Loading Loading @@ -176,8 +177,12 @@ class ContentEntityTest extends MigrateTestBase { 'name' => 'Foo media', 'uid' => $this->user->id(), 'bundle' => $mediaType->id(), 'field_media_file' => [ 'target_id' => $file->id(), ], ]); $media->save(); $this->container->get('cron')->run(); } /** Loading Loading @@ -263,12 +268,11 @@ class ContentEntityTest extends MigrateTestBase { $values = $source->current()->getSource(); $this->assertEquals(1, $values['mid']); $this->assertEquals('Foo media', $values['name']); $this->assertEquals('Foo media', $values['thumbnail__title']); $this->assertEquals(1, $values['uid']); $this->assertEquals('image', $values['bundle']); // Term testing. $definition['source']['plugin'] = 'd8_taxonomy_term'; $definition['source']['entity_type'] = 'taxonomy_term'; $definition['source']['bundle'] = $this->vocabulary; $source = $migrationPluginManager->createStubMigration($definition)->getSourcePlugin(); $ids = $source->getIds(); Loading @@ -282,13 +286,13 @@ class ContentEntityTest extends MigrateTestBase { $values = $source->current()->getSource(); $this->assertEquals($this->vocabulary, $values['vid']); $this->assertEquals(1, $values['tid']); $this->assertEquals(0, $values['parent']); $this->assertEquals(0, $values['parent'][0]['target_id'], print_r($values, TRUE)); $this->assertEquals('Apples', $values['name']); $source->next(); $values = $source->current()->getSource(); $this->assertEquals($this->vocabulary, $values['vid']); $this->assertEquals(2, $values['tid']); $this->assertEquals(1, $values['parent']); $this->assertEquals(1, $values['parent'][0]['target_id'], print_r($values, TRUE)); $this->assertEquals('Granny Smith', $values['name']); } Loading @@ -311,35 +315,4 @@ class ContentEntityTest extends MigrateTestBase { ]; } /** * Create a media type for a source plugin. * * @param string $media_source_name * The name of the media source. * * @return \Drupal\media\MediaTypeInterface * A media type. */ protected function createMediaType($media_source_name) { $id = strtolower($this->randomMachineName()); $media_type = MediaType::create([ 'id' => 'image', 'label' => 'Image', 'source' => $media_source_name, 'new_revision' => FALSE, ]); $media_type->save(); $source_field = $media_type->getSource()->createSourceField($media_type); // The media type form creates a source field if it does not exist yet. The // same must be done in a kernel test, since it does not use that form. // @see \Drupal\media\MediaTypeForm::save() $source_field->getFieldStorageDefinition()->save(); // The source field storage has been created, now the field can be saved. $source_field->save(); $media_type->set('source_configuration', [ 'source_field' => $source_field->getName(), ])->save(); return $media_type; } } Loading
src/Plugin/migrate/source/d8/ContentEntity.php +28 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace Drupal\migrate_drupal_d8\Plugin\migrate\source\d8; use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\State\StateInterface; use Drupal\migrate\MigrateException; use Drupal\migrate\Plugin\migrate\source\SqlBase; Loading Loading @@ -109,6 +110,15 @@ class ContentEntity extends SqlBase { } } // Base fields that have a cardinality greater then 1 are their own // dedicated tables. We need to gather those names too. But they are in code // so this is the only way to get those names. They aren't stored in the DB. foreach ($this->entityFieldManager->getBaseFieldDefinitions($this->configuration['entity_type']) as $fieldName => $definition) { if ($this->shouldMigrateBaseFieldDefinition($fieldName, $definition)) { $fields[$fieldName] = $fieldName; } } return $fields; } Loading Loading @@ -310,4 +320,22 @@ class ContentEntity extends SqlBase { ] + $fieldDefinition->getSettings(); } /** * Determine if a base field definition should be migrated. * * @param string $fieldName * The name of the field to evaluate. * @param \Drupal\Core\Field\FieldDefinitionInterface $definition * The base field definition to evaluate. * * @return bool * Return TRUE if the given base field definition should be migrated; FALSE * otherwise. */ protected function shouldMigrateBaseFieldDefinition($fieldName, $definition) { return $definition instanceof FieldStorageDefinitionInterface && $definition->isMultiple() && ($definition->isComputed() === FALSE); } }
src/Plugin/migrate/source/d8/TaxonomyTerm.php +3 −0 Original line number Diff line number Diff line Loading @@ -11,6 +11,9 @@ use Drupal\migrate\Row; /** * Drupal 8 taxonomy_term source from database. * * @deprecated Instead use * \Drupal\migrate_drupal_d8\Plugin\migrate\source\d8\ContentEntity. * * @MigrateSource( * id = "d8_taxonomy_term", * source_provider = "migrate_drupal_d8" Loading
tests/src/Kernel/ContentEntityTest.php +12 −39 Original line number Diff line number Diff line Loading @@ -4,14 +4,14 @@ namespace Drupal\Tests\migrate_drupal_d8\Kernel; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait; use Drupal\file\Entity\File; use Drupal\media\Entity\Media; use Drupal\media\Entity\MediaType; use Drupal\node\Entity\Node; use Drupal\node\Entity\NodeType; use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Vocabulary; use Drupal\Tests\field\Traits\EntityReferenceTestTrait; use Drupal\Tests\media\Traits\MediaTypeCreationTrait; use Drupal\Tests\migrate\Kernel\MigrateTestBase; use Drupal\user\Entity\User; Loading @@ -24,6 +24,7 @@ use Drupal\user\Entity\User; class ContentEntityTest extends MigrateTestBase { use EntityReferenceTestTrait; use MediaTypeCreationTrait; /** * {@inheritdoc} Loading Loading @@ -99,7 +100,7 @@ class ContentEntityTest extends MigrateTestBase { $this->installSchema('user', 'users_data'); $this->installSchema('file', 'file_usage'); $this->installConfig($this->modules); $this->installConfig(self::$modules); // Create a vocabulary. $vocabulary = Vocabulary::create([ Loading Loading @@ -134,7 +135,7 @@ class ContentEntityTest extends MigrateTestBase { ); // Create a media type. $mediaType = $this->createMediaType('test'); $mediaType = $this->createMediaType('file', ['id' => 'image']); // Create some data. $this->user = User::create([ Loading Loading @@ -176,8 +177,12 @@ class ContentEntityTest extends MigrateTestBase { 'name' => 'Foo media', 'uid' => $this->user->id(), 'bundle' => $mediaType->id(), 'field_media_file' => [ 'target_id' => $file->id(), ], ]); $media->save(); $this->container->get('cron')->run(); } /** Loading Loading @@ -263,12 +268,11 @@ class ContentEntityTest extends MigrateTestBase { $values = $source->current()->getSource(); $this->assertEquals(1, $values['mid']); $this->assertEquals('Foo media', $values['name']); $this->assertEquals('Foo media', $values['thumbnail__title']); $this->assertEquals(1, $values['uid']); $this->assertEquals('image', $values['bundle']); // Term testing. $definition['source']['plugin'] = 'd8_taxonomy_term'; $definition['source']['entity_type'] = 'taxonomy_term'; $definition['source']['bundle'] = $this->vocabulary; $source = $migrationPluginManager->createStubMigration($definition)->getSourcePlugin(); $ids = $source->getIds(); Loading @@ -282,13 +286,13 @@ class ContentEntityTest extends MigrateTestBase { $values = $source->current()->getSource(); $this->assertEquals($this->vocabulary, $values['vid']); $this->assertEquals(1, $values['tid']); $this->assertEquals(0, $values['parent']); $this->assertEquals(0, $values['parent'][0]['target_id'], print_r($values, TRUE)); $this->assertEquals('Apples', $values['name']); $source->next(); $values = $source->current()->getSource(); $this->assertEquals($this->vocabulary, $values['vid']); $this->assertEquals(2, $values['tid']); $this->assertEquals(1, $values['parent']); $this->assertEquals(1, $values['parent'][0]['target_id'], print_r($values, TRUE)); $this->assertEquals('Granny Smith', $values['name']); } Loading @@ -311,35 +315,4 @@ class ContentEntityTest extends MigrateTestBase { ]; } /** * Create a media type for a source plugin. * * @param string $media_source_name * The name of the media source. * * @return \Drupal\media\MediaTypeInterface * A media type. */ protected function createMediaType($media_source_name) { $id = strtolower($this->randomMachineName()); $media_type = MediaType::create([ 'id' => 'image', 'label' => 'Image', 'source' => $media_source_name, 'new_revision' => FALSE, ]); $media_type->save(); $source_field = $media_type->getSource()->createSourceField($media_type); // The media type form creates a source field if it does not exist yet. The // same must be done in a kernel test, since it does not use that form. // @see \Drupal\media\MediaTypeForm::save() $source_field->getFieldStorageDefinition()->save(); // The source field storage has been created, now the field can be saved. $source_field->save(); $media_type->set('source_configuration', [ 'source_field' => $source_field->getName(), ])->save(); return $media_type; } }