Commit 23b38b72 authored by alexpott's avatar alexpott

Issue #2190561 by chx, pcambra, benjy: Migrate in core: Add a load system for migrate plugins.

parent e0a47b4f
<?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);
}
<?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;
}
}
<?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;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment