Skip to content

Instantly share code, notes, and snippets.

@xurizaemon
Created July 10, 2019 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xurizaemon/50eb4996aec5fbe59fcfdde7b1610f5d to your computer and use it in GitHub Desktop.
Save xurizaemon/50eb4996aec5fbe59fcfdde7b1610f5d to your computer and use it in GitHub Desktop.
Running a Drupal migration from form submit
// You should probably just read Migrate Source UI code :) but ...
$migration_id = $form_state->getValue('migrations');
// Aha! We'll target the final migration, and let Migration
// dependencies do the legwork of starting the other two.
$migration_id = str_replace('node', 'media', $migration_id);
/** @var \Drupal\migrate\Plugin\Migration $migration */
$migration = $this->pluginManagerMigration->createInstance($migration_id);
// Reset status.
$status = $migration->getStatus();
if ($status != MigrationInterface::STATUS_IDLE) {
$migration->setStatus(MigrationInterface::STATUS_IDLE);
drupal_set_message($this->t('Migration @id reset to Idle', ['@id' => $migration_id]), 'warning');
}
$options = [
'file_path' => $form_state->getValue('file_path'),
];
// Force updates or not.
if ($form_state->getValue('update_existing_records')) {
$options['update'] = TRUE;
}
$executable = new MigrateBatchExecutable($migration, new StubMigrationMessage(), $options);
$executable->batchImport();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment