MigrationImportSync.php in Migrate Tools 8.5
File
src/EventSubscriber/MigrationImportSync.php
View source
<?php
namespace Drupal\migrate_tools\EventSubscriber;
use Drupal\migrate\Event\MigrateEvents;
use Drupal\migrate\Event\MigrateImportEvent;
use Drupal\migrate\Event\MigrateRollbackEvent;
use Drupal\migrate\Event\MigrateRowDeleteEvent;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate_plus\Event\MigrateEvents as MigratePlusEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MigrationImportSync implements EventSubscriberInterface {
protected $dispatcher;
public function __construct(EventDispatcherInterface $dispatcher) {
$this->dispatcher = $dispatcher;
}
public static function getSubscribedEvents() {
$events = [];
$events[MigrateEvents::PRE_IMPORT][] = [
'sync',
];
return $events;
}
public function sync(MigrateImportEvent $event) {
$migration = $event
->getMigration();
if (!empty($migration->syncSource)) {
$id_map = $migration
->getIdMap();
$id_map
->prepareUpdate();
$source = clone $migration
->getSourcePlugin();
$source
->rewind();
$source_id_values = [];
while ($source
->valid()) {
$source_id_values[] = $source
->current()
->getSourceIdValues();
$source
->next();
}
$id_map
->rewind();
$destination = $migration
->getDestinationPlugin();
while ($id_map
->valid()) {
$map_source_id = $id_map
->currentSource();
if (!in_array($map_source_id, $source_id_values, TRUE)) {
$destination_ids = $id_map
->currentDestination();
$this
->dispatchRowDeleteEvent(MigrateEvents::PRE_ROW_DELETE, $migration, $destination_ids);
$this
->dispatchRowDeleteEvent(MigratePlusEvents::MISSING_SOURCE_ITEM, $migration, $destination_ids);
$destination
->rollback($destination_ids);
$this
->dispatchRowDeleteEvent(MigrateEvents::POST_ROW_DELETE, $migration, $destination_ids);
$id_map
->delete($map_source_id);
}
$id_map
->next();
}
$this->dispatcher
->dispatch(MigrateEvents::POST_ROLLBACK, new MigrateRollbackEvent($migration));
}
}
protected function dispatchRowDeleteEvent($event_name, MigrationInterface $migration, array $destination_ids) {
$this->dispatcher
->dispatch($event_name, new MigrateRowDeleteEvent($migration, $destination_ids));
}
}