diff --git a/core/modules/block_content/migration_templates/d6_custom_block.yml b/core/modules/block_content/migration_templates/d6_custom_block.yml index be99072..f1f58a1 100644 --- a/core/modules/block_content/migration_templates/d6_custom_block.yml +++ b/core/modules/block_content/migration_templates/d6_custom_block.yml @@ -4,11 +4,8 @@ migration_tags: - Drupal 6 source: plugin: d6_box - constants: - type: basic process: id: bid - type: 'constants/type' info: info 'body/format': plugin: migration @@ -17,6 +14,7 @@ process: 'body/value': body destination: plugin: entity:block_content + bundle: basic no_stub: true migration_dependencies: required: diff --git a/core/modules/block_content/migration_templates/d7_custom_block.yml b/core/modules/block_content/migration_templates/d7_custom_block.yml index f63ca93..f5c9a97 100644 --- a/core/modules/block_content/migration_templates/d7_custom_block.yml +++ b/core/modules/block_content/migration_templates/d7_custom_block.yml @@ -4,11 +4,8 @@ migration_tags: - Drupal 7 source: plugin: d7_block_custom - constants: - type: basic process: id: bid - type: 'constants/type' info: info 'body/format': plugin: migration @@ -17,6 +14,7 @@ process: 'body/value': body destination: plugin: entity:block_content + bundle: basic no_stub: true migration_dependencies: required: diff --git a/core/modules/menu_link_content/migration_templates/menu_links.yml b/core/modules/menu_link_content/migration_templates/menu_links.yml index 7b818c6..1bf95f4 100644 --- a/core/modules/menu_link_content/migration_templates/menu_links.yml +++ b/core/modules/menu_link_content/migration_templates/menu_links.yml @@ -5,11 +5,8 @@ migration_tags: - Drupal 7 source: plugin: menu_link - constants: - bundle: menu_link_content process: id: mlid - bundle: 'constants/bundle' title: link_title description: description menu_name: @@ -49,6 +46,7 @@ process: changed: updated destination: plugin: entity:menu_link_content + bundle: menu_link_content no_stub: true migration_dependencies: required: diff --git a/core/modules/migrate/src/Plugin/migrate/destination/Entity.php b/core/modules/migrate/src/Plugin/migrate/destination/Entity.php index 55c59fc..e802be5 100644 --- a/core/modules/migrate/src/Plugin/migrate/destination/Entity.php +++ b/core/modules/migrate/src/Plugin/migrate/destination/Entity.php @@ -89,6 +89,16 @@ protected static function getEntityTypeId($plugin_id) { } /** + * Finds the entity bundle from configuration. + * + * @return string + * The entity bundle - empty string if the bundle was omitted. + */ + protected function getEntityBundleId() { + return isset($this->configuration['bundle']) ? $this->configuration['bundle'] : ''; + } + + /** * {@inheritdoc} */ public function fields(MigrationInterface $migration = NULL) { @@ -112,6 +122,12 @@ protected function getEntity(Row $row, array $old_destination_id_values) { $this->updateEntity($entity, $row); } else { + $bundle_key = $this->getKey('bundle'); + // Apply any configured bundle if the bundle has not already been set. + if (($bundle = $this->getEntityBundleId()) && empty($row->getDestinationProperty($bundle_key))) { + $row->setDestinationProperty($bundle_key, $bundle); + } + // Stubs might need some required fields filled in. if ($row->isStub()) { $this->processStubRow($row); diff --git a/core/modules/migrate/tests/src/Kernel/MigrateBundleTest.php b/core/modules/migrate/tests/src/Kernel/MigrateBundleTest.php new file mode 100644 index 0000000..88df9dd --- /dev/null +++ b/core/modules/migrate/tests/src/Kernel/MigrateBundleTest.php @@ -0,0 +1,155 @@ +installEntitySchema('taxonomy_vocabulary'); + $this->installEntitySchema('taxonomy_term'); + $this->installConfig(['taxonomy']); + // Set up two vocabularies (taxonomy bundles). + Vocabulary::create(['vid' => 'tags', 'name' => 'Tags']); + Vocabulary::create(['vid' => 'categories', 'name' => 'Categories']); + } + + /** + * Tests setting the bundle in the destination. + */ + public function testDestinationBundle() { + $term_data_rows = [ + ['id' => 1, 'name' => 'Category 1'], + ]; + $ids = ['id' => ['type' => 'integer']]; + $definition = [ + 'id' => 'terms', + 'migration_tags' => ['Bundle test'], + 'source' => [ + 'plugin' => 'embedded_data', + 'data_rows' => $term_data_rows, + 'ids' => $ids, + ], + 'process' => [ + 'tid' => 'id', + 'name' => 'name', + ], + 'destination' => [ + 'plugin' => 'entity:taxonomy_term', + 'bundle' => 'categories', + ], + 'migration_dependencies' => [], + ]; + + $term_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition); + + // Import and validate the term entity was created with the correct bundle. + $term_executable = new MigrateExecutable($term_migration, $this); + $term_executable->import(); + /** @var Term $term */ + $term = Term::load(1); + $this->assertEquals($term->bundle(), 'categories'); + } + + /** + * Tests setting the bundle in the process pipeline. + */ + public function testProcessBundle() { + $term_data_rows = [ + ['id' => 1, 'vocab' => 'categories', 'name' => 'Category 1'], + ['id' => 2, 'vocab' => 'tags', 'name' => 'Tag 1'], + ]; + $ids = ['id' => ['type' => 'integer']]; + $definition = [ + 'id' => 'terms', + 'migration_tags' => ['Bundle test'], + 'source' => [ + 'plugin' => 'embedded_data', + 'data_rows' => $term_data_rows, + 'ids' => $ids, + ], + 'process' => [ + 'tid' => 'id', + 'vid' => 'vocab', + 'name' => 'name', + ], + 'destination' => [ + 'plugin' => 'entity:taxonomy_term', + ], + 'migration_dependencies' => [], + ]; + + $term_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition); + + // Import and validate the term entities were created with the correct bundle. + $term_executable = new MigrateExecutable($term_migration, $this); + $term_executable->import(); + /** @var Term $term */ + $term = Term::load(1); + $this->assertEquals($term->bundle(), 'categories'); + $term = Term::load(2); + $this->assertEquals($term->bundle(), 'tags'); + } + + /** + * Tests setting bundles both in process and destination. + */ + public function testMixedBundles() { + $term_data_rows = [ + ['id' => 1, 'vocab' => 'categories', 'name' => 'Category 1'], + ['id' => 2, 'name' => 'Tag 1'], + ]; + $ids = ['id' => ['type' => 'integer']]; + $definition = [ + 'id' => 'terms', + 'migration_tags' => ['Bundle test'], + 'source' => [ + 'plugin' => 'embedded_data', + 'data_rows' => $term_data_rows, + 'ids' => $ids, + ], + 'process' => [ + 'tid' => 'id', + 'vid' => 'vocab', + 'name' => 'name', + ], + 'destination' => [ + 'plugin' => 'entity:taxonomy_term', + // When no vocab is provided, the destination bundle is applied. + 'bundle' => 'tags', + ], + 'migration_dependencies' => [], + ]; + + $term_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition); + + // Import and validate the term entities were created with the correct bundle. + $term_executable = new MigrateExecutable($term_migration, $this); + $term_executable->import(); + /** @var Term $term */ + $term = Term::load(1); + $this->assertEquals($term->bundle(), 'categories'); + $term = Term::load(2); + $this->assertEquals($term->bundle(), 'tags'); + } + +} diff --git a/core/modules/node/migration_templates/d6_node.yml b/core/modules/node/migration_templates/d6_node.yml index 82571b8..ec1474a 100644 --- a/core/modules/node/migration_templates/d6_node.yml +++ b/core/modules/node/migration_templates/d6_node.yml @@ -8,7 +8,6 @@ source: process: nid: nid vid: vid - type: type langcode: plugin: default_value source: language diff --git a/core/modules/node/migration_templates/d6_node_revision.yml b/core/modules/node/migration_templates/d6_node_revision.yml index cd32ea0..b7826a1 100644 --- a/core/modules/node/migration_templates/d6_node_revision.yml +++ b/core/modules/node/migration_templates/d6_node_revision.yml @@ -8,7 +8,6 @@ source: process: nid: nid vid: vid - type: type langcode: plugin: default_value source: language diff --git a/core/modules/node/migration_templates/d7_node.yml b/core/modules/node/migration_templates/d7_node.yml index 18e01d1..b763534 100644 --- a/core/modules/node/migration_templates/d7_node.yml +++ b/core/modules/node/migration_templates/d7_node.yml @@ -8,7 +8,6 @@ source: process: nid: nid vid: vid - type: type langcode: plugin: default_value source: language diff --git a/core/modules/node/migration_templates/d7_node_revision.yml b/core/modules/node/migration_templates/d7_node_revision.yml index 11f9d0a..0ee8bca 100644 --- a/core/modules/node/migration_templates/d7_node_revision.yml +++ b/core/modules/node/migration_templates/d7_node_revision.yml @@ -8,7 +8,6 @@ source: process: nid: nid vid: vid - type: type langcode: plugin: default_value source: language diff --git a/core/modules/node/src/Plugin/migrate/D6NodeDeriver.php b/core/modules/node/src/Plugin/migrate/D6NodeDeriver.php index d586efd..34c7b14 100644 --- a/core/modules/node/src/Plugin/migrate/D6NodeDeriver.php +++ b/core/modules/node/src/Plugin/migrate/D6NodeDeriver.php @@ -98,6 +98,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { '@type' => $node_type, ]); $values['source']['node_type'] = $node_type; + $values['destination']['bundle'] = $node_type; // If this migration is based on the d6_node_revision migration, it // should explicitly depend on the corresponding d6_node variant. diff --git a/core/modules/node/src/Plugin/migrate/D7NodeDeriver.php b/core/modules/node/src/Plugin/migrate/D7NodeDeriver.php index 3809c03..f18b455 100644 --- a/core/modules/node/src/Plugin/migrate/D7NodeDeriver.php +++ b/core/modules/node/src/Plugin/migrate/D7NodeDeriver.php @@ -92,6 +92,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { '@type' => $row->getSourceProperty('name'), ]); $values['source']['node_type'] = $node_type; + $values['destination']['bundle'] = $node_type; $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($values); if (isset($fields[$node_type])) {