From 66449600c6daa3e709462fbb178492f845e389cb Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Sun, 2 Jan 2022 10:33:17 +0000
Subject: [PATCH] Issue #3042533 by quietone, danflanagan8: D6 taxonomy term
 fields are not migrated with allowed vocabularies

---
 .../d6_vocabulary_field_instance.yml          | 11 +++++++-
 .../Plugin/migrate/process/TargetBundle.php   | 28 +++++++++++++++++++
 .../d6/MigrateVocabularyFieldInstanceTest.php | 25 ++++++++++++++++-
 3 files changed, 62 insertions(+), 2 deletions(-)
 create mode 100644 core/modules/taxonomy/src/Plugin/migrate/process/TargetBundle.php

diff --git a/core/modules/taxonomy/migrations/d6_vocabulary_field_instance.yml b/core/modules/taxonomy/migrations/d6_vocabulary_field_instance.yml
index c701a7d46bac..321b5713abeb 100644
--- a/core/modules/taxonomy/migrations/d6_vocabulary_field_instance.yml
+++ b/core/modules/taxonomy/migrations/d6_vocabulary_field_instance.yml
@@ -47,8 +47,17 @@ process:
       plugin: forum_vocabulary
       machine_name: taxonomy_forums
   label: name
+  _vid:
+    -
+      plugin: migration_lookup
+      migration:  d6_taxonomy_vocabulary
+      source: vid
+    -
+      plugin: skip_on_empty
+      method: row
   'settings/handler': 'constants/selection_handler'
-  'settings/handler_settings/target_bundles/0': '@field_name'
+  'settings/handler_settings/target_bundles':
+    plugin: target_bundle
   'settings/handler_settings/auto_create': 'constants/auto_create'
   required: required
    # Get the i18n taxonomy translation setting for this vocabulary.
diff --git a/core/modules/taxonomy/src/Plugin/migrate/process/TargetBundle.php b/core/modules/taxonomy/src/Plugin/migrate/process/TargetBundle.php
new file mode 100644
index 000000000000..5741908c876a
--- /dev/null
+++ b/core/modules/taxonomy/src/Plugin/migrate/process/TargetBundle.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace Drupal\taxonomy\Plugin\migrate\process;
+
+use Drupal\migrate\MigrateExecutableInterface;
+use Drupal\migrate\ProcessPluginBase;
+use Drupal\migrate\Row;
+
+/**
+ * Converts a Drupal 6 vocabulary ID to a target bundle array.
+ *
+ * @MigrateProcessPlugin(
+ *   id = "target_bundle"
+ * )
+ */
+class TargetBundle extends ProcessPluginBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
+    $target_bundle = [];
+    $vid = $row->get('@_vid');
+    $target_bundle[$vid] = $vid;
+    return $target_bundle;
+  }
+
+}
diff --git a/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateVocabularyFieldInstanceTest.php b/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateVocabularyFieldInstanceTest.php
index 5869e71a69f4..584ae9d4a9c8 100644
--- a/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateVocabularyFieldInstanceTest.php
+++ b/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateVocabularyFieldInstanceTest.php
@@ -46,6 +46,7 @@ public function testVocabularyFieldInstance() {
     $this->assertSame('Tags', $field->label());
     $this->assertTrue($field->isRequired(), 'Field is required');
     $this->assertFalse($field->isTranslatable());
+    $this->assertTargetBundles($field_id, ['tags' => 'tags']);
 
     // Test the page bundle as well. Tags has a multilingual option of 'None'.
     $field_id = 'node.page.field_tags';
@@ -57,7 +58,7 @@ public function testVocabularyFieldInstance() {
 
     $settings = $field->getSettings();
     $this->assertSame('default:taxonomy_term', $settings['handler'], 'The handler plugin ID is correct.');
-    $this->assertSame(['field_tags'], $settings['handler_settings']['target_bundles'], 'The target_bundles handler setting is correct.');
+    $this->assertTargetBundles($field_id, ['tags' => 'tags']);
     $this->assertTrue($settings['handler_settings']['auto_create'], 'The "auto_create" setting is correct.');
 
     $this->assertSame([['node', 'article', 'field_tags']], $this->getMigration('d6_vocabulary_field_instance')->getIdMap()->lookupDestinationIds([4, 'article']));
@@ -68,6 +69,7 @@ public function testVocabularyFieldInstance() {
     $field = FieldConfig::load($field_id);
     $this->assertFalse($field->isRequired(), 'Field is not required');
     $this->assertTrue($field->isTranslatable());
+    $this->assertTargetBundles($field_id, ['vocabulary_1_i_0_' => 'vocabulary_1_i_0_']);
 
     // Test the field vocabulary_2_i_0_ with multilingual option,
     // 'Set language to vocabulary'.
@@ -75,6 +77,7 @@ public function testVocabularyFieldInstance() {
     $field = FieldConfig::load($field_id);
     $this->assertFalse($field->isRequired(), 'Field is not required');
     $this->assertFalse($field->isTranslatable());
+    $this->assertTargetBundles($field_id, ['vocabulary_2_i_1_' => 'vocabulary_2_i_1_']);
 
     // Test the field vocabulary_3_i_0_ with multilingual option,
     // 'Localize terms'.
@@ -82,12 +85,32 @@ public function testVocabularyFieldInstance() {
     $field = FieldConfig::load($field_id);
     $this->assertFalse($field->isRequired(), 'Field is not required');
     $this->assertTrue($field->isTranslatable());
+    $this->assertTargetBundles($field_id, ['vocabulary_3_i_2_' => 'vocabulary_3_i_2_']);
 
     // Tests that a vocabulary named like a D8 base field will be migrated and
     // prefixed with 'field_' to avoid conflicts.
     $field_type = FieldConfig::load('node.sponsor.field_type');
     $this->assertInstanceOf(FieldConfig::class, $field_type);
     $this->assertTrue($field->isTranslatable());
+    $this->assertTargetBundles($field_id, ['vocabulary_3_i_2_' => 'vocabulary_3_i_2_']);
+
+    $this->assertTargetBundles('node.employee.field_vocabulary_3_i_2_', ['vocabulary_3_i_2_' => 'vocabulary_3_i_2_']);
+
+  }
+
+  /**
+   * 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 assertTargetBundles($id, array $target_bundles) {
+    $field = FieldConfig::load($id);
+    $handler_settings = $field->getSetting('handler_settings');
+    $this->assertArrayHasKey('target_bundles', $handler_settings);
+    $this->assertSame($handler_settings['target_bundles'], $target_bundles);
   }
 
   /**
-- 
GitLab