cforge-2.0.x-dev/modules/cforge_docs/src/MigrationSubscriber.php

modules/cforge_docs/src/MigrationSubscriber.php
<?php

namespace Drupal\cforge_docs;

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 but will be overwritten by
    // migrated terms.
    if ($migration->id() == 'd7_taxonomy_term:cforge_docs_categories') {
      // get all the terms in that vocab
      $tids = \Drupal::entityQuery('taxonomy_term')->condition('vid', 'binders')->execute();
      foreach (Term::loadMultiple($tids) as $term) {
        $term->delete();
      }
      $migration->messenger()->addStatus('Cleared '.count($tids).' terms from binders vocab');
    }
  }

  /**
   * @param Drupal\migrate\Event\MigratePreRowSaveEvent $event
   */
  public function migratePreRowSave(MigratePreRowSaveEvent $event) {
    $row = $event->getRow();
    $migration = $event->getMigration();

    // Copy the terms to the new vocab
    if ($migration->id() == 'd7_taxonomy_term:cforge_docs_categories') {
      $row->setDestinationProperty('vid', 'binders');
    }

    // Copy the term references to the new field
    if ($event->getMigration()->id() == 'd7_node:document') {
      $row->setDestinationProperty('terms', $row->getDestinationProperty('cforge_docs_categories'));
    }

    // 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') == 'cforge_docs_categories') {
        throw new MigrateSkipRowException('Binders field is already installed.');
      }
    }

    // Don't copy the node_type
    if ($migration->id() == 'd7_node_type') {
      if ($row->getSourceProperty('type') == 'document') {
        throw new MigrateSkipRowException("Document node type is already installed");
      }
    }

  }



}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc