From 23b38b72943c3f790e28b16c65643ea132629945 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Mon, 14 Apr 2014 13:04:21 +0100
Subject: [PATCH] Issue #2190561 by chx, pcambra, benjy: Migrate in core: Add a
 load system for migrate plugins.

---
 .../Plugin/MigrateLoadInterface.php           | 37 ++++++++
 .../Plugin/migrate/load/LoadEntity.php        | 90 +++++++++++++++++++
 .../Plugin/migrate/load/d6/LoadTermNode.php   | 64 +++++++++++++
 3 files changed, 191 insertions(+)
 create mode 100644 core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Plugin/MigrateLoadInterface.php
 create mode 100644 core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Plugin/migrate/load/LoadEntity.php
 create mode 100644 core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Plugin/migrate/load/d6/LoadTermNode.php

diff --git a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Plugin/MigrateLoadInterface.php b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Plugin/MigrateLoadInterface.php
new file mode 100644
index 000000000000..474d445b4a45
--- /dev/null
+++ b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Plugin/MigrateLoadInterface.php
@@ -0,0 +1,37 @@
+<?php
+
+/**
+ * @file
+ * Contains Drupal\migrate\Plugin\MigrateLoadInterface
+ */
+
+namespace Drupal\migrate_drupal\Plugin;
+
+use Drupal\Core\Entity\EntityStorageInterface;
+
+interface MigrateLoadInterface {
+
+  /**
+   * Load an additional migration.
+   *
+   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
+   *   The migration storage controller.
+   * @param string $sub_id
+   *   For example, when loading d6_node:article, this will be article.
+   * @return \Drupal\migrate\Entity\MigrationInterface
+   */
+  public function load(EntityStorageInterface $storage, $sub_id);
+
+  /**
+   * Load additional migrations.
+   *
+   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
+   *   The migration storage controller.
+   * @param array $sub_ids
+   *   For example, when loading d6_node:article, sub_id will be article.
+   *   If NULL then load all sub-migrations.
+   * @return \Drupal\migrate\Entity\MigrationInterface[]
+   */
+  public function loadMultiple(EntityStorageInterface $storage, array $sub_ids = NULL);
+
+}
diff --git a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Plugin/migrate/load/LoadEntity.php b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Plugin/migrate/load/LoadEntity.php
new file mode 100644
index 000000000000..113d473d0ccf
--- /dev/null
+++ b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Plugin/migrate/load/LoadEntity.php
@@ -0,0 +1,90 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\migrate\Plugin\load\LoadEntity.
+ */
+
+namespace Drupal\migrate_drupal\Plugin\migrate\load;
+
+use Drupal\Component\Utility\String;
+use Drupal\Core\Entity\EntityStorageInterface;
+use Drupal\Core\Plugin\PluginBase;
+use Drupal\migrate\Entity\MigrationInterface;
+use Drupal\migrate\MigrateException;
+use Drupal\migrate\Plugin\SourceEntityInterface;
+use Drupal\migrate_drupal\Plugin\MigrateLoadInterface;
+
+/**
+ * Base class for entity load plugins.
+ *
+ * @PluginID("drupal_entity")
+ */
+class LoadEntity extends PluginBase implements MigrateLoadInterface {
+
+  /**
+   * The list of bundles being loaded.
+   *
+   * @var array
+   */
+  protected $bundles;
+
+  /**
+   * {@inheritdoc}
+   */
+  public function __construct(array $configuration, $plugin_id, array $plugin_definition, MigrationInterface $migration) {
+    parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
+    $this->migration = $migration;
+    $source_plugin = $this->migration->getSourcePlugin();
+    if (!$source_plugin instanceof SourceEntityInterface) {
+      throw new MigrateException('Migrations with a load plugin using LoadEntity should have an entity as source.');
+    }
+    if ($source_plugin->bundleMigrationRequired() && empty($configuration['bundle_migration'])) {
+      throw new MigrateException(String::format('Source plugin @plugin requires the bundle_migration key to be set.', array('@plugin' => $source_plugin->getPluginId())));
+    }
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function load(EntityStorageInterface $storage, $sub_id) {
+    $entities = $this->loadMultiple($storage, array($sub_id));
+    return isset($entities[$sub_id]) ? $entities[$sub_id] : FALSE;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function loadMultiple(EntityStorageInterface $storage, array $sub_ids = NULL) {
+    if (isset($this->configuration['bundle_migration'])) {
+      /** @var \Drupal\migrate\Entity\MigrationInterface $bundle_migration */
+      $bundle_migration = $storage->load($this->configuration['bundle_migration']);
+      $source_id = array_keys($bundle_migration->getSourcePlugin()->getIds())[0];
+      $this->bundles = array();
+      foreach ($bundle_migration->getSourcePlugin()->getIterator() as $row) {
+        $this->bundles[] = $row[$source_id];
+      }
+    }
+    else {
+      // This entity type has no bundles ('user', 'feed', etc).
+      $this->bundles = array($this->migration->getSourcePlugin()->entityTypeId());
+    }
+    $sub_ids_to_load = isset($sub_ids) ? array_intersect($this->bundles, $sub_ids) : $this->bundles;
+    $migrations = array();
+    foreach ($sub_ids_to_load as $id) {
+      $values = $this->migration->toArray();
+      $values['id'] = $this->migration->id() . ':' . $id;
+      $values['source']['bundle'] = $id;
+      /** @var \Drupal\migrate_drupal\Entity\MigrationInterface $migration */
+      $migration = $storage->create($values);
+      if ($migration->getSourcePlugin()->checkRequirements()) {
+        $fields = array_keys($migration->getSourcePlugin()->fields());
+        $migration->process += array_combine($fields, $fields);
+        $migrations[$migration->id()] = $migration;
+      }
+    }
+
+    return $migrations;
+  }
+
+}
diff --git a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Plugin/migrate/load/d6/LoadTermNode.php b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Plugin/migrate/load/d6/LoadTermNode.php
new file mode 100644
index 000000000000..e5b3f5e1aaf6
--- /dev/null
+++ b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Plugin/migrate/load/d6/LoadTermNode.php
@@ -0,0 +1,64 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\migrate_drupal\Plugin\migrate\load\d6\TermNode.
+ */
+
+namespace Drupal\migrate_drupal\Plugin\migrate\load\d6;
+
+use Drupal\Core\Entity\EntityStorageInterface;
+use Drupal\migrate\Entity\MigrationInterface;
+use Drupal\migrate\MigrateExecutable;
+use Drupal\migrate\MigrateMessage;
+use Drupal\migrate\Row;
+use Drupal\migrate_drupal\Plugin\migrate\load\LoadEntity;
+
+/**
+ * @PluginID("d6_term_node")
+ */
+class LoadTermNode extends LoadEntity {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function __construct(array $configuration, $plugin_id, array $plugin_definition, MigrationInterface $migration) {
+    $configuration['bundle_migration'] = 'd6_taxonomy_vocabulary';
+    parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function loadMultiple(EntityStorageInterface $storage, array $sub_ids = NULL) {
+    /** @var \Drupal\migrate\Entity\MigrationInterface $bundle_migration */
+    $bundle_migration = $storage->load('d6_taxonomy_vocabulary');
+    $migrate_executable = new MigrateExecutable($bundle_migration, new MigrateMessage());
+    $process = array_intersect_key($bundle_migration->get('process'), $bundle_migration->getDestinationPlugin()->getIds());
+    $migrations = array();
+    $vid_map = array();
+    foreach ($bundle_migration->getIdMap() as $key => $value) {
+      $old_vid = unserialize($key)['sourceid1'];
+      $new_vid = $value['destid1'];
+      $vid_map[$old_vid] = $new_vid;
+    }
+    foreach ($bundle_migration->getSourcePlugin()->getIterator() as $source_row) {
+      $row = new Row($source_row, $source_row);
+      $migrate_executable->processRow($row, $process);
+      $old_vid = $source_row['vid'];
+      $new_vid = $row->getDestinationProperty('vid');
+      $vid_map[$old_vid] = $new_vid;
+    }
+    foreach ($vid_map as $old_vid => $new_vid) {
+      $values = $this->migration->toArray();
+      $migration_id = $this->migration->id() . ':' . $old_vid;
+      $values['id'] = $migration_id;
+      $values['source']['vid'] = $old_vid;
+      $values['process'][$new_vid] = 'tid';
+      $migrations[$migration_id] = $storage->create($values);;
+    }
+
+    return $migrations;
+  }
+
+}
-- 
GitLab