Email us at info@henrytech.org to discuss your Drupal Migration today!
Sometimes during a migration, fields are not directly migrateable in the .yml file. In this case, or if there is more processing of the data that cannot be handled by a plugin, you can use an event subscriber that fires on the migration of each row.
In your your_custom_migration_module/src/EventSubscriber/CustomMigrateSubscriber.php:
<?php
namespace Drupal\your_custom_module\EventSubscriber;
use Drupal\migrate\Event\MigrateEvents;
use Drupal\migrate\Event\MigrateImportEvent;
use Drupal\migrate\Event\MigratePostRowSaveEvent;
use Drupal\migrate\Event\MigrateRollbackEvent;
use Drupal\Core\Database\Database;
/**
* Processes data after migrations.
*/
class CustomMigrateSubscriber implements EventSubscriberInterface {
/**
* MigrateSubscriber constructor.
*/
public function __construct() {}
/**
* Executes on post row save.
*
* @param \Drupal\migrate\Event\MigratePostRowSaveEvent $event
* The migrate post row save event.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function onPostRowSave(MigratePostRowSaveEvent $event) {
$d8_connection = \Drupal::database();
$connection = Database::getConnection('default', 'migrate');
$migration_id = $event->getMigration()->id();
$nid = $event->getRow()->getSourceProperty('nid');
$uid = $event->getRow()->getSourceProperty('uid');
if ($migration_id == 'your_custom_migration_id') {
// Get fields from source plugin
$some_array = $event->getRow()->getSourceProperty('ids'); //We set this in the source plugin!
$nid = $event->getRow()->getSourceProperty('nid');
// Do something with this data!
}
}

Comments