Voici comment lancer simplement une migration via le code, ici via une commande drush :
<?php
use Drupal\migrate\MigrateMessage;
use Drupal\migrate_tools\MigrateExecutable;
$manager = Drupal::service('plugin.manager.migration');
$migration = $manager->createInstance('ma_migration');
$executable = new MigrateExecutable($migration, new MigrateMessage());
$migrationResult = $executable->import();
Pour autoriser l'update il faut ajouter la ligne $migration->getIdMap()->prepareUpdate();
<?php
use Drupal\migrate\MigrateMessage;
use Drupal\migrate_tools\MigrateExecutable;
$manager = Drupal::service('plugin.manager.migration');
$migration = $manager->createInstance('ma_migration');
$migration->getIdMap()->prepareUpdate();
$executable = new MigrateExecutable($migration, new MigrateMessage());
$migrationResult = $executable->import();
On peut aussi passer des paramètres comme les sourceIds, pour ne mettre à jour que certains contenu qui ne s'étaient pas bien importé (et dont la destination était NULL dans la table de migration :
<?php
use Drupal\migrate\MigrateMessage;
use Drupal\migrate_tools\MigrateExecutable;
$results = \Drupal::database()->query('select sourceid1, sourceid2 from {migrate_map_ma_migration} where destid1 IS NULL')->fetchAllAssoc('sourceid1');
$manager = Drupal::service('plugin.manager.migration');
$migration = $manager->createInstance('ma_migration');
$migration->getIdMap()->prepareUpdate();
foreach ($results as $result) {
$executable = new MigrateExecutable(
$migration,
new MigrateMessage(),
[
'idlist' => $result->sourceid1 . ':' . $result->sourceid2,
]
);
$migrationResult = $executable->import();
}
Contenus en rapport
Dans le cas d'une migration, un plugin très pratique permet de retrouver une référence avec son nouvel id.
Ce site à été construit à l'origine sous Drupal 6, migré sous drupal 7 en 2012, Je suis en train de préparer la migration de ce site vers Drupal 8 via la Migrate API.
Ajouter un commentaire