Starting a migration with Migrate programmatically

A combi car

Migrate, a module integrated into the Drupal 8 Core, is a powerful solution for setting up data import processes from any data source (CSV, XML, Database, JSON, etc.) to a Drupal 8 project. The purpose of this post is not to explore all the facets of Migrate, as they are numerous and are already covered in many blog posts or official documentation, but rather to address a particular point: how to programmatically launch a migration?

Indeed it is recommended to use Drush to launch the different migration processes that are available. But in some cases, such as on a limited hosting that does not have drush, or as part of a continuous migration process, or rather a continuous data import from a third-party system to Drupal 8, it may be necessary to be able to trigger the migration processes from the code of a module, and not from a drush command at the server level.

The snippet below will allow us to start one (or more if necessary) migration process (here with the system name my_migrate_id) programmatically. Please note that in case of an error during the execution of the process, we take care to change the migration status in order to be able to restart it later.

 

    $manager = Drupal::service('plugin.manager.migration');
    $migration_ids = ['MY_MIGRATE_ID'];
    foreach ($migration_ids as $migration_id) {
      $migration = $manager->createInstance($migration_id);
      // update existing entity imported.
      $migration->getIdMap()->prepareUpdate();
      $executable = new MigrateExecutable($migration, new MigrateMessage());

      try {
        // Run the migration.
        $executable->import();
      }
      catch (\Exception $e) {
        $migration->setStatus(MigrationInterface::STATUS_IDLE);
      }
    }

So, for example, to set up a continuous integration of third-party content with migrate, all we have to do is to place this snippet within a hook_cron() and we will then be able to launch it at regular intervals without any more manual intervention.

 

Ajouter un commentaire