cforge-2.0.x-dev/modules/cforge_gallery/src/MigrationSubscriber.php
modules/cforge_gallery/src/MigrationSubscriber.php
<?php
namespace Drupal\cforge_gallery;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Event\MigratePreRowSaveEvent;
use Drupal\migrate\Event\MigrateImportEvent;
use Drupal\taxonomy\Entity\Term;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Migration modifications
*
* @todo inject \Drupal::entityQuery('taxonomy_term')
*/
class MigrationSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() : array {
return [
'migrate.pre_import' => [['migratePreImport']],
'migrate.pre_row_save' => [['migratePreRowSave']],
];
}
/**
* @param Drupal\migrate\Event\MigrateImportEvent $event
*/
public function migratePreImport(MigrateImportEvent $event) {
$migration = $event->getMigration();
// Delete taxonomy terms which were installed in d8
if ($migration->id() == 'd7_taxonomy_term:galleries') {
// get all the terms in that vocab
$tids = \Drupal::entityQuery('taxonomy_term')->condition('vid', 'galleries')->execute();
$existing = Term::loadMultiple($tids);
foreach ($existing as $term) {
$term->delete();
}
$migration->messenger()->addStatus('Cleared '.count($existing).' terms from gallery vocab');
}
}
/**
* @param Drupal\migrate\Event\MigratePreRowSaveEvent $event
*/
public function migratePreRowSave(MigratePreRowSaveEvent $event) {
$row = $event->getRow();
$migration = $event->getMigration();
// Copy the term references to the new 'terms' field
if ($migration->id() == 'd7_node:image') {
$row->setDestinationProperty('terms', $row->getDestinationProperty('galleries'));
}
// Don't copy the categories field or field instance
if ($migration->id() == 'd7_field' or $migration->id() == 'd7_field_instance') {
if ($row->getSourceProperty('field_name') == 'galleries') {
throw new MigrateSkipRowException('Galleries term reference field is already installed.');
}
elseif ($row->getSourceProperty('field_name') == 'image') {
throw new MigrateSkipRowException('Node image field is already installed.');
}
}
// Don't copy the node_type
if ($migration->id() == 'd7_node_type') {
if ($row->getSourceProperty('type') == 'image') {
throw new MigrateSkipRowException("Image node type is already installed");
}
}
if($migration->id() == 'd7_comment_type' or $migration->id() == 'd7_comment_field') {
if ($row->getSourceProperty('type') == 'image') {
throw new MigrateSkipRowException('Comments for images already exist');
}
}
}
}
