You are here

public function MigrateUpgradeDrushRunner::export in Migrate Upgrade 8.3

Same name and namespace in other branches
  1. 8.2 src/MigrateUpgradeDrushRunner.php \Drupal\migrate_upgrade\MigrateUpgradeDrushRunner::export()

Export the configured migration plugins as configuration entities.

Return value

array The exported migration names.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Core\Entity\EntityStorageException

File

src/MigrateUpgradeDrushRunner.php, line 253

Class

MigrateUpgradeDrushRunner
Class MigrateUpgradeDrushRunner.

Namespace

Drupal\migrate_upgrade

Code

public function export() {
  $migration_ids = [];
  $db_info = \Drupal::state()
    ->get($this->databaseStateKey);

  // Create a group to hold the database configuration.
  $group_details = [
    'id' => $this->databaseStateKey,
    'label' => 'Import from Drupal ' . $this->version,
    'description' => 'Migrations originally generated from drush migrate-upgrade --configure-only',
    'source_type' => 'Drupal ' . $this->version,
    'shared_configuration' => [
      'source' => [
        'key' => 'drupal_' . $this->version,
      ],
    ],
  ];

  // Only add the database connection info to the configuration entity
  // if it was passed in as a parameter.
  if (!empty($this->options['legacy-db-url'])) {
    $group_details['shared_configuration']['source']['database'] = $db_info['database'];
  }

  // Ditto for the key.
  if (!empty($this->options['legacy-db-key'])) {
    $group_details['shared_configuration']['source']['key'] = $this->options['legacy-db-key'];
  }

  // Load existing migration group and update it with changed settings,
  // or create a new one if none exists.
  $group = MigrationGroup::load($group_details['id']);
  if (empty($group)) {
    $group = MigrationGroup::create($group_details);
  }
  else {
    $this
      ->setEntityProperties($group, $group_details);
  }
  $group
    ->save();
  foreach ($this->migrationList as $migration_id => $migration) {
    $migration_details = [];
    $migration_details['id'] = $migration_id;
    $migration_details['label'] = $migration
      ->label();
    $plugin_definition = $migration
      ->getPluginDefinition();
    $migration_details['class'] = $plugin_definition['class'];
    if (isset($plugin_definition['field_plugin_method'])) {
      $migration_details['field_plugin_method'] = $plugin_definition['field_plugin_method'];
    }
    if (isset($plugin_definition['cck_plugin_method'])) {
      $migration_details['cck_plugin_method'] = $plugin_definition['cck_plugin_method'];
    }
    $migration_details['migration_group'] = $this->databaseStateKey;
    $migration_details['migration_tags'] = isset($plugin_definition['migration_tags']) ? $plugin_definition['migration_tags'] : [];
    $migration_details['source'] = $migration
      ->getSourceConfiguration();
    $migration_details['destination'] = $migration
      ->getDestinationConfiguration();
    $migration_details['process'] = $migration
      ->getProcess();
    $migration_details['migration_dependencies'] = $migration
      ->getMigrationDependencies();
    $migration_details = $this
      ->substituteIds($migration_details);
    $migration_entity = Migration::load($migration_details['id']);
    if (empty($migration_entity)) {
      $migration_entity = Migration::create($migration_details);
    }
    else {
      $this
        ->setEntityProperties($migration_entity, $migration_details);
    }
    $migration_entity
      ->save();
    $migration_ids[$migration_entity
      ->id()] = [
      'original' => $migration_id,
      'generated' => $migration_entity
        ->id(),
    ];
  }
  return $migration_ids;
}