From 42524aa8d843e9935558b4d02fde27e69a43b537 Mon Sep 17 00:00:00 2001
From: Mike Ryan <mikeryan@virtuoso-performance.com>
Date: Mon, 11 Apr 2016 10:16:49 -0500
Subject: [PATCH] Issue #2690345 by mikeryan: Fix group configuration merging
 for 8.1.x

---
 config/schema/migrate_plus.schema.yml         | 10 ++---
 .../migrate_plus.migration.beer_comment.yml   |  5 +--
 .../migrate_plus.migration.beer_node.yml      |  5 +--
 .../migrate_plus.migration.beer_term.yml      | 17 ++-------
 .../migrate_plus.migration.beer_user.yml      |  5 +--
 migrate_plus.module                           | 38 +++++++------------
 6 files changed, 23 insertions(+), 57 deletions(-)

diff --git a/config/schema/migrate_plus.schema.yml b/config/schema/migrate_plus.schema.yml
index a2c682d4..87f59a3b 100644
--- a/config/schema/migrate_plus.schema.yml
+++ b/config/schema/migrate_plus.schema.yml
@@ -13,6 +13,9 @@ migrate_plus.migration.*:
       sequence:
         type: string
         label: 'Tag'
+    migration_group:
+      type: string
+      label: 'Group'
     label:
       type: label
       label: 'Label'
@@ -67,10 +70,3 @@ migrate_plus.migration_group.*:
     shared_configuration:
       type: ignore
       label: 'Shared migration configuration'
-migrate_plus.migration.*.third_party.migrate_plus:
-  type: mapping
-  label: 'Group'
-  mapping:
-    migration_group:
-      type: string
-      label: 'Group'
diff --git a/migrate_example/config/install/migrate_plus.migration.beer_comment.yml b/migrate_example/config/install/migrate_plus.migration.beer_comment.yml
index 20962df7..991c59b1 100644
--- a/migrate_example/config/install/migrate_plus.migration.beer_comment.yml
+++ b/migrate_example/config/install/migrate_plus.migration.beer_comment.yml
@@ -1,12 +1,9 @@
 # Migration configuration for beer comments. No new concepts here.
 id: beer_comment
 label: Comments on beers
-third_party_settings:
-  migrate_plus:
-    migration_group: beer
+migration_group: beer
 source:
   plugin: beer_comment
-  key: default
 destination:
   plugin: entity:comment
 process:
diff --git a/migrate_example/config/install/migrate_plus.migration.beer_node.yml b/migrate_example/config/install/migrate_plus.migration.beer_node.yml
index 19b52257..b8483223 100644
--- a/migrate_example/config/install/migrate_plus.migration.beer_node.yml
+++ b/migrate_example/config/install/migrate_plus.migration.beer_node.yml
@@ -1,12 +1,9 @@
 # Migration configuration for beer content.
 id: beer_node
 label: Beers of the world
-third_party_settings:
-  migrate_plus:
-    migration_group: beer
+migration_group: beer
 source:
   plugin: beer_node
-  key: default
 destination:
   plugin: entity:node
 process:
diff --git a/migrate_example/config/install/migrate_plus.migration.beer_term.yml b/migrate_example/config/install/migrate_plus.migration.beer_term.yml
index b0ee91ac..32573fa9 100644
--- a/migrate_example/config/install/migrate_plus.migration.beer_term.yml
+++ b/migrate_example/config/install/migrate_plus.migration.beer_term.yml
@@ -1,4 +1,4 @@
-# A "migration" is, in technical terms, a configuration entity which describes
+# A "migration" is, in technical terms, a plugin whose configuration describes
 # how to read source data, process it (generally by mapping source fields to
 # destination fields), and write it to Drupal.
 
@@ -8,17 +8,9 @@ id: beer_term
 # A human-friendly description of the migration.
 label: Migrate style categories from the source database to taxonomy terms
 
-# 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
+# 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
@@ -26,7 +18,6 @@ third_party_settings:
 # src/Plugin/migrate/source/BeerTerm.php.
 source:
   plugin: beer_term
-  key: default
 
 # Every migration must also have a destination plugin, which handles writing
 # the migrated data in the appropriate form for that particular kind of data.
diff --git a/migrate_example/config/install/migrate_plus.migration.beer_user.yml b/migrate_example/config/install/migrate_plus.migration.beer_user.yml
index fcbb4702..71d07e6d 100644
--- a/migrate_example/config/install/migrate_plus.migration.beer_user.yml
+++ b/migrate_example/config/install/migrate_plus.migration.beer_user.yml
@@ -3,12 +3,9 @@
 # repeat that here.
 id: beer_user
 label: Beer Drinkers of the world
-third_party_settings:
-  migrate_plus:
-    migration_group: beer
+migration_group: beer
 source:
   plugin: beer_user
-  key: default
 destination:
   plugin: entity:user
 process:
diff --git a/migrate_plus.module b/migrate_plus.module
index 3c9a6f73..c6a11c97 100644
--- a/migrate_plus.module
+++ b/migrate_plus.module
@@ -16,23 +16,12 @@ use Drupal\migrate_plus\Event\MigratePrepareRowEvent;
  * Implements hook_migration_plugins_alter().
  */
 function migrate_tools_migration_plugins_alter(array &$migrations) {
-  // @todo: Implement group merging here.
-}
-
-/**
- * Implements hook_ENTITY_TYPE_load().
- */
-function migrate_plus_migration_load($migrations) {
-  // @todo: Replace with hook_migration_plugins()
-  return;
-  /** @var MigrationInterface $migration */
-  foreach ($migrations as $migration) {
-    // If we are pointing to a valid group, merge its properties into ours.
-    $migration_group = $migration->getThirdPartySetting('migrate_plus', 'migration_group');
-    if (empty($migration_group)) {
+  /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
+  foreach ($migrations as $id => $migration) {
+    if (empty($migration['migration_group'])) {
       continue;
     }
-    $group = MigrationGroup::load($migration_group);
+    $group = MigrationGroup::load($migration['migration_group']);
     if (empty($group)) {
       continue;
     }
@@ -41,21 +30,20 @@ function migrate_plus_migration_load($migrations) {
       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).
+      $migration_value = $migration[$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);
+        $migrations[$id][$key] = $merged_values;
       }
-      // Where the group provides a value the migration doesn't, use
-      // the group value.
+      // Where the group provides a value the migration doesn't, use the group
+      // value.
       elseif (is_null($migration_value)) {
-        $migration->set($key, $group_value);
+        $migrations[$id][$key] = $group_value;
       }
-      // Otherwise, the existing migration value overrides the group
-      // value.
+      // Otherwise, the existing migration value overrides the group value.
     }
   }
 }
-- 
GitLab