From 2f33339ff7b19a9eb75c3d3f67880c340a624bd5 Mon Sep 17 00:00:00 2001
From: stborchert <stborchert@36942.no-reply.drupal.org>
Date: Sat, 5 Dec 2015 14:50:15 -0600
Subject: [PATCH] Issue #2613336 by stBorchert, webflo: Convert migration_group
 to a third party setting

---
 config/schema/migrate_plus.schema.yml         |  7 +++
 .../migrate.migration.beer_comment.yml        |  4 +-
 .../install/migrate.migration.beer_node.yml   |  4 +-
 .../install/migrate.migration.beer_term.yml   | 14 ++++-
 .../install/migrate.migration.beer_user.yml   |  4 +-
 .../install/migrate.migration.wine_terms.yml  |  4 +-
 ...play.node.migrate_example_wine.default.yml |  4 +-
 ...play.node.migrate_example_wine.default.yml |  4 +-
 ...splay.node.migrate_example_wine.teaser.yml |  4 +-
 ..._wine.field_migrate_example_adv_image.yml} |  6 +-
 ....node.field_migrate_example_adv_image.yml} |  4 +-
 migrate_plus.module                           | 63 ++++++++-----------
 src/Tests/MigrationGroupTest.php              | 18 +++++-
 13 files changed, 83 insertions(+), 57 deletions(-)
 rename migrate_example_advanced/migrate_example_advanced_setup/config/install/{field.field.node.migrate_example_wine.field_migrate_example_image.yml => field.field.node.migrate_example_wine.field_migrate_example_adv_image.yml} (81%)
 rename migrate_example_advanced/migrate_example_advanced_setup/config/install/{field.storage.node.field_migrate_example_image.yml => field.storage.node.field_migrate_example_adv_image.yml} (83%)

diff --git a/config/schema/migrate_plus.schema.yml b/config/schema/migrate_plus.schema.yml
index af36c593..63712fbb 100644
--- a/config/schema/migrate_plus.schema.yml
+++ b/config/schema/migrate_plus.schema.yml
@@ -20,3 +20,10 @@ migrate_plus.migration_group.*:
     shared_configuration:
       type: ignore
       label: 'Shared migration configuration'
+migrate.migration.*.third_party.migrate_plus:
+  type: mapping
+  label: 'Group'
+  mapping:
+    migration_group:
+      type: string
+      label: 'Group'
diff --git a/migrate_example/config/install/migrate.migration.beer_comment.yml b/migrate_example/config/install/migrate.migration.beer_comment.yml
index 991c59b1..ad5ad2c8 100644
--- a/migrate_example/config/install/migrate.migration.beer_comment.yml
+++ b/migrate_example/config/install/migrate.migration.beer_comment.yml
@@ -1,7 +1,9 @@
 # Migration configuration for beer comments. No new concepts here.
 id: beer_comment
 label: Comments on beers
-migration_group: beer
+third_party_settings:
+  migrate_plus:
+    migration_group: beer
 source:
   plugin: beer_comment
 destination:
diff --git a/migrate_example/config/install/migrate.migration.beer_node.yml b/migrate_example/config/install/migrate.migration.beer_node.yml
index b8483223..805e9443 100644
--- a/migrate_example/config/install/migrate.migration.beer_node.yml
+++ b/migrate_example/config/install/migrate.migration.beer_node.yml
@@ -1,7 +1,9 @@
 # Migration configuration for beer content.
 id: beer_node
 label: Beers of the world
-migration_group: beer
+third_party_settings:
+  migrate_plus:
+    migration_group: beer
 source:
   plugin: beer_node
 destination:
diff --git a/migrate_example/config/install/migrate.migration.beer_term.yml b/migrate_example/config/install/migrate.migration.beer_term.yml
index a231d104..09df3af7 100644
--- a/migrate_example/config/install/migrate.migration.beer_term.yml
+++ b/migrate_example/config/install/migrate.migration.beer_term.yml
@@ -8,9 +8,17 @@ id: beer_term
 # A human-friendly description of the migration.
 label: Migrate style categories from the source database to taxonomy terms
 
-# The machine name of the group containing this migration (which contains
-# shared configuration to be merged with our own configuration here).
-migration_group: beer
+# This section contains configuration added by modules other than the primary
+# module defining the configuration entity. The 'migrate.migration.' at the
+# beginning of the file name indicates that the migrate module defines the
+# migration configuration entity type. The migrate_plus module extends that
+# entity type with the group concept, thus the settings it provides fall under
+# 'third_party_settings:' and 'migrate_plus:'.
+third_party_settings:
+  migrate_plus:
+    # The machine name of the group containing this migration (which contains
+    # shared configuration to be merged with our own configuration here).
+    migration_group: beer
 
 # Every migration must have a source plugin, which controls the delivery of our
 # source data. In this case, our source plugin has the name "beer_term", which
diff --git a/migrate_example/config/install/migrate.migration.beer_user.yml b/migrate_example/config/install/migrate.migration.beer_user.yml
index 233ae5da..34439d44 100644
--- a/migrate_example/config/install/migrate.migration.beer_user.yml
+++ b/migrate_example/config/install/migrate.migration.beer_user.yml
@@ -3,7 +3,9 @@
 # repeat that here.
 id: beer_user
 label: Beer Drinkers of the world
-migration_group: beer
+third_party_settings:
+  migrate_plus:
+    migration_group: beer
 source:
   plugin: beer_user
 destination:
diff --git a/migrate_example_advanced/config/install/migrate.migration.wine_terms.yml b/migrate_example_advanced/config/install/migrate.migration.wine_terms.yml
index 616346e2..a605cbac 100644
--- a/migrate_example_advanced/config/install/migrate.migration.wine_terms.yml
+++ b/migrate_example_advanced/config/install/migrate.migration.wine_terms.yml
@@ -1,6 +1,8 @@
 id: wine_terms
 label: Migrate all categories into Drupal taxonomy terms
-migration_group: wine
+third_party_settings:
+  migrate_plus:
+    migration_group: wine
 source:
   plugin: wine_term
 destination:
diff --git a/migrate_example_advanced/migrate_example_advanced_setup/config/install/core.entity_form_display.node.migrate_example_wine.default.yml b/migrate_example_advanced/migrate_example_advanced_setup/config/install/core.entity_form_display.node.migrate_example_wine.default.yml
index ed251403..0ef5edb6 100644
--- a/migrate_example_advanced/migrate_example_advanced_setup/config/install/core.entity_form_display.node.migrate_example_wine.default.yml
+++ b/migrate_example_advanced/migrate_example_advanced_setup/config/install/core.entity_form_display.node.migrate_example_wine.default.yml
@@ -4,7 +4,7 @@ dependencies:
   config:
     - field.field.node.migrate_example_wine.body
     - field.field.node.migrate_example_wine.field_migrate_example_comments
-    - field.field.node.migrate_example_wine.field_migrate_example_image
+    - field.field.node.migrate_example_wine.field_migrate_example_adv_image
     - field.field.node.migrate_example_wine.field_migrate_example_top_vintag
     - field.field.node.migrate_example_wine.field_migrate_example_wine_best
     - field.field.node.migrate_example_wine.field_migrate_example_wine_ratin
@@ -39,7 +39,7 @@ content:
     settings: {  }
     third_party_settings: {  }
     type: comment_default
-  field_migrate_example_image:
+  field_migrate_example_adv_image:
     weight: 35
     settings:
       progress_indicator: throbber
diff --git a/migrate_example_advanced/migrate_example_advanced_setup/config/install/core.entity_view_display.node.migrate_example_wine.default.yml b/migrate_example_advanced/migrate_example_advanced_setup/config/install/core.entity_view_display.node.migrate_example_wine.default.yml
index 9f0a2e8e..57ad1155 100644
--- a/migrate_example_advanced/migrate_example_advanced_setup/config/install/core.entity_view_display.node.migrate_example_wine.default.yml
+++ b/migrate_example_advanced/migrate_example_advanced_setup/config/install/core.entity_view_display.node.migrate_example_wine.default.yml
@@ -4,7 +4,7 @@ dependencies:
   config:
     - field.field.node.migrate_example_wine.body
     - field.field.node.migrate_example_wine.field_migrate_example_comments
-    - field.field.node.migrate_example_wine.field_migrate_example_image
+    - field.field.node.migrate_example_wine.field_migrate_example_adv_image
     - field.field.node.migrate_example_wine.field_migrate_example_top_vintag
     - field.field.node.migrate_example_wine.field_migrate_example_wine_best
     - field.field.node.migrate_example_wine.field_migrate_example_wine_ratin
@@ -34,7 +34,7 @@ content:
       pager_id: 0
     third_party_settings: {  }
     type: comment_default
-  field_migrate_example_image:
+  field_migrate_example_adv_image:
     weight: 105
     label: hidden
     settings:
diff --git a/migrate_example_advanced/migrate_example_advanced_setup/config/install/core.entity_view_display.node.migrate_example_wine.teaser.yml b/migrate_example_advanced/migrate_example_advanced_setup/config/install/core.entity_view_display.node.migrate_example_wine.teaser.yml
index 31cfb9dd..85874f7c 100644
--- a/migrate_example_advanced/migrate_example_advanced_setup/config/install/core.entity_view_display.node.migrate_example_wine.teaser.yml
+++ b/migrate_example_advanced/migrate_example_advanced_setup/config/install/core.entity_view_display.node.migrate_example_wine.teaser.yml
@@ -5,7 +5,7 @@ dependencies:
     - core.entity_view_mode.node.teaser
     - field.field.node.migrate_example_wine.body
     - field.field.node.migrate_example_wine.field_migrate_example_comments
-    - field.field.node.migrate_example_wine.field_migrate_example_image
+    - field.field.node.migrate_example_wine.field_migrate_example_adv_image
     - field.field.node.migrate_example_wine.field_migrate_example_top_vintag
     - field.field.node.migrate_example_wine.field_migrate_example_wine_best
     - field.field.node.migrate_example_wine.field_migrate_example_wine_ratin
@@ -31,7 +31,7 @@ content:
     weight: 100
 hidden:
   field_migrate_example_comments: true
-  field_migrate_example_image: true
+  field_migrate_example_adv_image: true
   field_migrate_example_top_vintag: true
   field_migrate_example_wine_best: true
   field_migrate_example_wine_ratin: true
diff --git a/migrate_example_advanced/migrate_example_advanced_setup/config/install/field.field.node.migrate_example_wine.field_migrate_example_image.yml b/migrate_example_advanced/migrate_example_advanced_setup/config/install/field.field.node.migrate_example_wine.field_migrate_example_adv_image.yml
similarity index 81%
rename from migrate_example_advanced/migrate_example_advanced_setup/config/install/field.field.node.migrate_example_wine.field_migrate_example_image.yml
rename to migrate_example_advanced/migrate_example_advanced_setup/config/install/field.field.node.migrate_example_wine.field_migrate_example_adv_image.yml
index 8620370b..8933c38d 100644
--- a/migrate_example_advanced/migrate_example_advanced_setup/config/install/field.field.node.migrate_example_wine.field_migrate_example_image.yml
+++ b/migrate_example_advanced/migrate_example_advanced_setup/config/install/field.field.node.migrate_example_wine.field_migrate_example_adv_image.yml
@@ -2,12 +2,12 @@ langcode: en
 status: true
 dependencies:
   config:
-    - field.storage.node.field_migrate_example_image
+    - field.storage.node.field_migrate_example_adv_image
     - node.type.migrate_example_wine
   module:
     - image
-id: node.migrate_example_wine.field_migrate_example_image
-field_name: field_migrate_example_image
+id: node.migrate_example_wine.field_migrate_example_adv_image
+field_name: field_migrate_example_adv_image
 entity_type: node
 bundle: migrate_example_wine
 label: Image
diff --git a/migrate_example_advanced/migrate_example_advanced_setup/config/install/field.storage.node.field_migrate_example_image.yml b/migrate_example_advanced/migrate_example_advanced_setup/config/install/field.storage.node.field_migrate_example_adv_image.yml
similarity index 83%
rename from migrate_example_advanced/migrate_example_advanced_setup/config/install/field.storage.node.field_migrate_example_image.yml
rename to migrate_example_advanced/migrate_example_advanced_setup/config/install/field.storage.node.field_migrate_example_adv_image.yml
index 0a37a779..62fc9b09 100644
--- a/migrate_example_advanced/migrate_example_advanced_setup/config/install/field.storage.node.field_migrate_example_image.yml
+++ b/migrate_example_advanced/migrate_example_advanced_setup/config/install/field.storage.node.field_migrate_example_adv_image.yml
@@ -5,8 +5,8 @@ dependencies:
     - file
     - image
     - node
-id: node.field_migrate_example_image
-field_name: field_migrate_example_image
+id: node.field_migrate_example_adv_image
+field_name: field_migrate_example_adv_image
 entity_type: node
 type: image
 settings:
diff --git a/migrate_plus.module b/migrate_plus.module
index d37aa246..f48e0bfa 100644
--- a/migrate_plus.module
+++ b/migrate_plus.module
@@ -12,19 +12,6 @@ use Drupal\migrate_plus\Entity\MigrationGroup;
 use Drupal\migrate_plus\Event\MigrateEvents;
 use Drupal\migrate_plus\Event\MigratePrepareRowEvent;
 
-/**
- * Implements hook_config_schema_info_alter().
- */
-function migrate_plus_config_schema_info_alter(&$definitions) {
-  // Add a migration_group key to the Migration configuration schema, so
-  // migrations can reference their containing group.
-  $definitions['migrate.migration.*']['mapping']['migration_group'] =
-    array(
-      'type' => 'string',
-      'label' => 'Group',
-    );
-}
-
 /**
  * Implements hook_ENTITY_TYPE_load().
  */
@@ -32,30 +19,34 @@ function migrate_plus_migration_load($migrations) {
   /** @var MigrationInterface $migration */
   foreach ($migrations as $migration) {
     // If we are pointing to a valid group, merge its properties into ours.
-    if (isset($migration->migration_group)) {
-      $group = MigrationGroup::load($migration->migration_group);
-      if ($group) {
-        $shared_configuration = $group->get('shared_configuration');
-        if ($shared_configuration) {
-          foreach ($shared_configuration as $key => $group_value) {
-            $migration_value = $migration->get($key);
-            // Where both the migration and the group provide arrays,
-            // replace recursively (so each key collision is resolved in favor
-            // of the migration).
-            if (is_array($migration_value) && is_array($group_value)) {
-              $merged_values = array_replace_recursive($group_value, $migration_value);
-              $migration->set($key, $merged_values);
-            }
-            // Where the group provides a value the migration doesn't, use
-            // the group value.
-            elseif (is_null($migration_value)) {
-              $migration->set($key, $group_value);
-            }
-            // Otherwise, the existing migration value overrides the group
-            // value.
-          }
-        }
+    $migration_group = $migration->getThirdPartySetting('migrate_plus', 'migration_group');
+    if (empty($migration_group)) {
+      continue;
+    }
+    $group = MigrationGroup::load($migration_group);
+    if (empty($group)) {
+      continue;
+    }
+    $shared_configuration = $group->get('shared_configuration');
+    if (empty($shared_configuration)) {
+      continue;
+    }
+    foreach ($shared_configuration as $key => $group_value) {
+      $migration_value = $migration->get($key);
+      // Where both the migration and the group provide arrays,
+      // replace recursively (so each key collision is resolved in favor
+      // of the migration).
+      if (is_array($migration_value) && is_array($group_value)) {
+        $merged_values = array_replace_recursive($group_value, $migration_value);
+        $migration->set($key, $merged_values);
+      }
+      // Where the group provides a value the migration doesn't, use
+      // the group value.
+      elseif (is_null($migration_value)) {
+        $migration->set($key, $group_value);
       }
+      // Otherwise, the existing migration value overrides the group
+      // value.
     }
   }
 }
diff --git a/src/Tests/MigrationGroupTest.php b/src/Tests/MigrationGroupTest.php
index 316cf74e..4b67dbc6 100644
--- a/src/Tests/MigrationGroupTest.php
+++ b/src/Tests/MigrationGroupTest.php
@@ -52,7 +52,11 @@ class MigrationGroupTest extends WebTestBase {
     $migration = entity_create('migration', array(
       'id' => 'specific_migration',
       'load' => [],
-      'migration_group' => $group_id,
+      'third_party_settings' => [
+        'migrate_plus' => [
+          'migration_group' => $group_id,
+        ],
+      ],
       'label' => 'Unaffected by the group',
       'migration_tags' => array('Drupal 7'), // Overrides group.
       'destination' => array(),
@@ -68,7 +72,11 @@ class MigrationGroupTest extends WebTestBase {
     $migration->save();
 
     $expected_config = array(
-      'migration_group' => $group_id,
+      'third_party_settings' => [
+        'migrate_plus' => [
+          'migration_group' => $group_id,
+        ],
+      ],
       'label' => 'Unaffected by the group',
       'migration_tags' => array('Drupal 7'),
       'source' => array(
@@ -101,7 +109,11 @@ class MigrationGroupTest extends WebTestBase {
     /** @var MigrationInterface $migration */
     $migration = entity_create('migration', [
       'id' => 'specific_migration',
-      'migration_group' => 'test_group',
+      'third_party_settings' => [
+        'migrate_plus' => [
+          'migration_group' => 'test_group',
+        ],
+      ],
       'migration_tags' => array(),
       'load' => [],
       'destination' => array(),
-- 
GitLab