diff --git a/core/modules/field/migrations/d7_field_instance.yml b/core/modules/field/migrations/d7_field_instance.yml index c824c00971e63b7ce38054a12efd82cfa92b6c47..6edf3047d349403b90e9f5d7462d59f8b63b7af1 100644 --- a/core/modules/field/migrations/d7_field_instance.yml +++ b/core/modules/field/migrations/d7_field_instance.yml @@ -29,6 +29,15 @@ process: description: description required: required status: 'constants/status' + allowed_values: + - + plugin: sub_process + source: allowed_vid + process: + - + plugin: migration_lookup + migration: d7_taxonomy_vocabulary + source: vid settings: plugin: d7_field_instance_settings source: diff --git a/core/modules/field/src/Plugin/migrate/process/d7/FieldInstanceSettings.php b/core/modules/field/src/Plugin/migrate/process/d7/FieldInstanceSettings.php index 991ce09c581b613bba559763a89420dd8e4ff5ce..dae922bb63059d6c3338783c7f0bd1fcfe2da2ed 100644 --- a/core/modules/field/src/Plugin/migrate/process/d7/FieldInstanceSettings.php +++ b/core/modules/field/src/Plugin/migrate/process/d7/FieldInstanceSettings.php @@ -23,6 +23,19 @@ public function transform($value, MigrateExecutableInterface $migrate_executable $field_data = unserialize($field_definition['data']); $field_settings = $field_data['settings']; + // Get taxonomy term reference handler settings from allowed values. + if ($row->getSourceProperty('type') == 'taxonomy_term_reference') { + $instance_settings['handler_settings']['sort'] = [ + 'field' => '_none', + ]; + $allowed_values = $row->get('@allowed_values'); + foreach ($allowed_values as $allowed_value) { + foreach ($allowed_value as $vocabulary) { + $instance_settings['handler_settings']['target_bundles'][$vocabulary] = $vocabulary; + } + } + } + // Get entityreference handler settings from source field configuration. if ($row->getSourceProperty('type') == "entityreference") { $instance_settings['handler'] = 'default:' . $field_settings['target_type']; diff --git a/core/modules/field/src/Plugin/migrate/source/d7/FieldInstance.php b/core/modules/field/src/Plugin/migrate/source/d7/FieldInstance.php index 65b31bdfd48e961229c29e8efc651eb101faf9cc..ce905f9d58ce122229d41d1dedf85a5992a8f7db 100644 --- a/core/modules/field/src/Plugin/migrate/source/d7/FieldInstance.php +++ b/core/modules/field/src/Plugin/migrate/source/d7/FieldInstance.php @@ -128,6 +128,24 @@ public function prepareRow(Row $row) { } $row->setSourceProperty('translatable', $translatable); + // Get the vid for each allowed value for taxonomy term reference fields + // which is used in a migration_lookup in the process pipeline. + if ($row->getSourceProperty('type') == 'taxonomy_term_reference') { + $vocabulary = []; + $data = unserialize($field_definition['data']); + foreach ($data['settings']['allowed_values'] as $allowed_value) { + $vocabulary[] = $allowed_value['vocabulary']; + } + $query = $this->select('taxonomy_vocabulary', 'v') + ->fields('v', ['vid']) + ->condition('machine_name', $vocabulary, 'IN'); + $allowed_vid = $query->execute()->fetchAllAssoc('vid'); + $row->setSourceProperty('allowed_vid', $allowed_vid); + } + + $field_data = unserialize($row->getSourceProperty('field_data')); + $row->setSourceProperty('field_settings', $field_data['settings']); + return parent::prepareRow($row); } diff --git a/core/modules/field/tests/src/Kernel/Migrate/d7/MigrateFieldInstanceTest.php b/core/modules/field/tests/src/Kernel/Migrate/d7/MigrateFieldInstanceTest.php index 72d31050e4855f27b014857e4ebe30206829f3f2..ab73909171aeba682551f15b0b139ac639a3092e 100644 --- a/core/modules/field/tests/src/Kernel/Migrate/d7/MigrateFieldInstanceTest.php +++ b/core/modules/field/tests/src/Kernel/Migrate/d7/MigrateFieldInstanceTest.php @@ -79,6 +79,23 @@ protected function assertLinkFields($id, $title_setting) { $this->assertSame($title_setting, $field->getSetting('title')); } + /** + * Asserts the settings of an entity reference field config entity. + * + * @param string $id + * The entity ID in the form ENTITY_TYPE.BUNDLE.FIELD_NAME. + * @param string[] $target_bundles + * An array of expected target bundles. + */ + protected function assertEntityReferenceFields($id, array $target_bundles) { + $field = FieldConfig::load($id); + $handler_settings = $field->getSetting('handler_settings'); + $this->assertArrayHasKey('target_bundles', $handler_settings); + foreach ($handler_settings['target_bundles'] as $target_bundle) { + $this->assertContains($target_bundle, $target_bundles); + } + } + /** * Tests migrating D7 field instances to field_config entities. */ @@ -120,6 +137,10 @@ public function testFieldInstances() { $this->assertLinkFields('node.article.field_link', DRUPAL_DISABLED); $this->assertLinkFields('node.blog.field_link', DRUPAL_REQUIRED); + $this->assertEntityReferenceFields('node.article.field_tags', ['tags']); + $this->assertEntityReferenceFields('node.forum.taxonomy_forums', ['forums']); + $this->assertEntityReferenceFields('node.test_content_type.field_term_reference', ['tags', 'test_vocabulary']); + // Tests that fields created by the Title module are not migrated. $title_field = FieldConfig::load('node.test_content_type.title_field'); $this->assertNull($title_field); diff --git a/core/modules/migrate_drupal/tests/fixtures/drupal7.php b/core/modules/migrate_drupal/tests/fixtures/drupal7.php index a3fd9801b140385922dcce395ec6ac46108ed276..073f62f00aa0fa139e19b136ee483923eb371165 100644 --- a/core/modules/migrate_drupal/tests/fixtures/drupal7.php +++ b/core/modules/migrate_drupal/tests/fixtures/drupal7.php @@ -3631,7 +3631,7 @@ 'storage_module' => 'field_sql_storage', 'storage_active' => '1', 'locked' => '0', - 'data' => 'a:7:{s:8:"settings";a:3:{s:14:"allowed_values";a:1:{i:0;a:2:{s:10:"vocabulary";s:6:"forums";s:6:"parent";i:0;}}s:21:"options_list_callback";s:28:"i18n_taxonomy_allowed_values";s:23:"entity_translation_sync";b:0;}s:12:"entity_types";a:0:{}s:12:"translatable";s:1:"0";s:7:"storage";a:5:{s:4:"type";s:17:"field_sql_storage";s:8:"settings";a:0:{}s:6:"module";s:17:"field_sql_storage";s:6:"active";s:1:"1";s:7:"details";a:1:{s:3:"sql";a:2:{s:18:"FIELD_LOAD_CURRENT";a:1:{s:26:"field_data_taxonomy_forums";a:1:{s:3:"tid";s:19:"taxonomy_forums_tid";}}s:19:"FIELD_LOAD_REVISION";a:1:{s:30:"field_revision_taxonomy_forums";a:1:{s:3:"tid";s:19:"taxonomy_forums_tid";}}}}}s:12:"foreign keys";a:1:{s:3:"tid";a:2:{s:5:"table";s:18:"taxonomy_term_data";s:7:"columns";a:1:{s:3:"tid";s:3:"tid";}}}s:7:"indexes";a:1:{s:3:"tid";a:1:{i:0;s:3:"tid";}}s:2:"id";s:1:"5";}', + 'data' => 'a:7:{s:8:"settings";a:3:{s:14:"allowed_values";a:1:{i:0;a:2:{s:10:"vocabulary";s:19:"sujet_de_discussion";s:6:"parent";i:0;}}s:21:"options_list_callback";s:28:"i18n_taxonomy_allowed_values";s:23:"entity_translation_sync";b:0;}s:12:"entity_types";a:0:{}s:12:"translatable";s:1:"0";s:7:"storage";a:5:{s:4:"type";s:17:"field_sql_storage";s:8:"settings";a:0:{}s:6:"module";s:17:"field_sql_storage";s:6:"active";s:1:"1";s:7:"details";a:1:{s:3:"sql";a:2:{s:18:"FIELD_LOAD_CURRENT";a:1:{s:26:"field_data_taxonomy_forums";a:1:{s:3:"tid";s:19:"taxonomy_forums_tid";}}s:19:"FIELD_LOAD_REVISION";a:1:{s:30:"field_revision_taxonomy_forums";a:1:{s:3:"tid";s:19:"taxonomy_forums_tid";}}}}}s:12:"foreign keys";a:1:{s:3:"tid";a:2:{s:5:"table";s:18:"taxonomy_term_data";s:7:"columns";a:1:{s:3:"tid";s:3:"tid";}}}s:7:"indexes";a:1:{s:3:"tid";a:1:{i:0;s:3:"tid";}}s:2:"id";s:1:"5";}', 'cardinality' => '1', 'translatable' => '0', 'deleted' => '0', @@ -3841,7 +3841,7 @@ 'storage_module' => 'field_sql_storage', 'storage_active' => '1', 'locked' => '0', - 'data' => 'a:7:{s:12:"translatable";s:1:"0";s:12:"entity_types";a:0:{}s:8:"settings";a:3:{s:14:"allowed_values";a:1:{i:0;a:2:{s:10:"vocabulary";s:15:"test_vocabulary";s:6:"parent";s:1:"0";}}s:21:"options_list_callback";s:28:"i18n_taxonomy_allowed_values";s:23:"entity_translation_sync";b:0;}s:7:"storage";a:5:{s:4:"type";s:17:"field_sql_storage";s:8:"settings";a:0:{}s:6:"module";s:17:"field_sql_storage";s:6:"active";s:1:"1";s:7:"details";a:1:{s:3:"sql";a:2:{s:18:"FIELD_LOAD_CURRENT";a:1:{s:31:"field_data_field_term_reference";a:1:{s:3:"tid";s:24:"field_term_reference_tid";}}s:19:"FIELD_LOAD_REVISION";a:1:{s:35:"field_revision_field_term_reference";a:1:{s:3:"tid";s:24:"field_term_reference_tid";}}}}}s:12:"foreign keys";a:1:{s:3:"tid";a:2:{s:5:"table";s:18:"taxonomy_term_data";s:7:"columns";a:1:{s:3:"tid";s:3:"tid";}}}s:7:"indexes";a:1:{s:3:"tid";a:1:{i:0;s:3:"tid";}}s:2:"id";s:2:"20";}', + 'data' => 'a:7:{s:12:"translatable";s:1:"0";s:12:"entity_types";a:0:{}s:8:"settings";a:3:{s:14:"allowed_values";a:2:{i:0;a:2:{s:10:"vocabulary";s:15:"test_vocabulary";s:6:"parent";s:1:"0";}i:1;a:2:{s:10:"vocabulary";s:4:"tags";s:6:"parent";s:1:"0";}}s:21:"options_list_callback";s:28:"i18n_taxonomy_allowed_values";s:23:"entity_translation_sync";b:0;}s:7:"storage";a:5:{s:4:"type";s:17:"field_sql_storage";s:8:"settings";a:0:{}s:6:"module";s:17:"field_sql_storage";s:6:"active";s:1:"1";s:7:"details";a:1:{s:3:"sql";a:2:{s:18:"FIELD_LOAD_CURRENT";a:1:{s:31:"field_data_field_term_reference";a:1:{s:3:"tid";s:24:"field_term_reference_tid";}}s:19:"FIELD_LOAD_REVISION";a:1:{s:35:"field_revision_field_term_reference";a:1:{s:3:"tid";s:24:"field_term_reference_tid";}}}}}s:12:"foreign keys";a:1:{s:3:"tid";a:2:{s:5:"table";s:18:"taxonomy_term_data";s:7:"columns";a:1:{s:3:"tid";s:3:"tid";}}}s:7:"indexes";a:1:{s:3:"tid";a:1:{i:0;s:3:"tid";}}s:2:"id";s:2:"20";}', 'cardinality' => '1', 'translatable' => '0', 'deleted' => '0',