getSourcePlugin example


  protected function buildDiscoveredDestinationsBySource($version, array $migrations, array $source_system_data) {
    $discovered_upgrade_paths = [];
    $table_data = [];
    foreach ($migrations as $migration) {
      $migration_id = $migration->getPluginId();
      $source_module = $migration->getSourcePlugin()->getSourceModule();
      if (!$source_module) {
        $this->messenger()
          ->addError($this->t('Source module not found for @migration_id.', ['@migration_id' => $migration_id]));
      }
      $destination_module = $migration->getDestinationPlugin()
        ->getDestinationModule();
      if (!$destination_module) {
        $this->messenger()
          ->addError($this->t('Destination module not found for @migration_id.', ['@migration_id' => $migration_id]));
      }

      

  protected function prepareMigrations(array $id_mappings) {
    $manager = $this->container->get('plugin.manager.migration');
    foreach ($id_mappings as $migration_id => $data) {
      foreach ($manager->createInstances($migration_id) as $migration) {
        $id_map = $migration->getIdMap();
        $id_map->setMessage($this);
        $source_ids = $migration->getSourcePlugin()->getIds();
        foreach ($data as $id_mapping) {
          $row = new Row(array_combine(array_keys($source_ids)$id_mapping[0])$source_ids);
          $id_map->saveIdMapping($row$id_mapping[1]);
        }
      }
    }
  }

  /** * Modify a migration's configuration before executing it. * * @param \Drupal\migrate\Plugin\MigrationInterface $migration * The migration to execute. */
'idMap' => [
          'plugin' => 'null',
        ],
        'destination' => [
          'plugin' => 'null',
        ],
      ];
      try {
        $profile_migration = $this->migrationPluginManager->createStubMigration($definition);
        // Ensure that Profile is enabled in the source DB.         $profile_migration->checkRequirements();
        foreach ($profile_migration->getSourcePlugin() as $row) {
          $name = $row->getSourceProperty('name');
          $this->process[$name] = $name;
        }
      }
      catch (RequirementsException $e) {
        // The checkRequirements() call will fail when the profile module does         // not exist on the source site.       }
    }
    return parent::getProcess();
  }

}

  protected function invoke($method$event$plugin_interface) {
    $migration = $event->getMigration();

    $source = $migration->getSourcePlugin();
    if ($source instanceof $plugin_interface) {
      call_user_func([$source$method]$event);
    }

    $destination = $migration->getDestinationPlugin();
    if ($destination instanceof $plugin_interface) {
      call_user_func([$destination$method]$event);
    }
  }

  /** * Forwards pre-import events to the source and destination plugins. * * @param \Drupal\migrate\Event\MigrateImportEvent $event * The import event. */
/** * Tests Migration::set(). * * @covers ::set */
  public function testSetInvalidation() {
    $migration = \Drupal::service('plugin.manager.migration')->createStubMigration([
      'source' => ['plugin' => 'empty'],
      'destination' => ['plugin' => 'entity:entity_view_mode'],
    ]);
    $this->assertEquals('empty', $migration->getSourcePlugin()->getPluginId());
    $this->assertEquals('entity:entity_view_mode', $migration->getDestinationPlugin()->getPluginId());

    // Test the source plugin is invalidated.     $migration->set('source', ['plugin' => 'embedded_data', 'data_rows' => [], 'ids' => []]);
    $this->assertEquals('embedded_data', $migration->getSourcePlugin()->getPluginId());

    // Test the destination plugin is invalidated.     $migration->set('destination', ['plugin' => 'null']);
    $this->assertEquals('null', $migration->getDestinationPlugin()->getPluginId());
  }

}
/** * Returns the source. * * Makes sure source is initialized based on migration settings. * * @return \Drupal\migrate\Plugin\MigrateSourceInterface * The source. */
  protected function getSource() {
    if (!isset($this->source)) {
      $this->source = $this->migration->getSourcePlugin();
    }
    return $this->source;
  }

  /** * Gets the event dispatcher. * * @return \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
  protected function getEventDispatcher() {
    if (!$this->eventDispatcher) {
      
// Skip migrations tagged with any of the follow-up migration tags. They       // will be derived and executed after the migrations on which they depend       // have been successfully executed.       // @see Drupal\migrate_drupal\Plugin\MigrationWithFollowUpInterface       if (!empty(array_intersect($migration->getMigrationTags()$this->getFollowUpMigrationTags()))) {
        continue;
      }

      try {
        // @todo https://drupal.org/node/2681867 We should be able to validate         // the entire migration at this point.         $source_plugin = $migration->getSourcePlugin();
        if ($source_plugin instanceof RequirementsInterface) {
          $source_plugin->checkRequirements();
        }
        $destination_plugin = $migration->getDestinationPlugin();
        if ($destination_plugin instanceof RequirementsInterface) {
          $destination_plugin->checkRequirements();
        }
        $migrations[] = $migration;
      }
      catch (RequirementsException $e) {
        // Migrations which are not applicable given the source and destination
$this->assertCount($ids_count_expected$ids);
  }

  /** * Tests user source plugin. * * @dataProvider migrationConfigurationProvider */
  public function testUserSource(array $configuration) {
    $migration = $this->migrationPluginManager
      ->createStubMigration($this->migrationDefinition('content_entity:user', $configuration));
    $user_source = $migration->getSourcePlugin();
    $this->assertSame('users', $user_source->__toString());
    if (!$configuration['include_translations']) {
      // Confirm that the anonymous user is in the source database but not       // included in the rows returned by the content_entity.       $this->assertNotNull(User::load(0));
      $this->assertEquals(1, $user_source->count());
    }
    $this->assertIds($user_source$configuration);
    $fields = $user_source->fields();
    $this->assertArrayHasKey('name', $fields);
    $this->assertArrayHasKey('pass', $fields);
    
0 => [
        0 => 'Stop',
        1 => 'Go',
      ],
      1 => [
        0 => 'Stop',
        1 => 'Go',
      ],
    ];
    $this->assertSame($allowed_values$config_translation->get('settings.allowed_values'));
    // Ensure that the count query works as expected.     $this->assertCount(20, $this->getMigration('d7_field_option_translation')->getSourcePlugin());
  }

}
return new static(
      $base_plugin_id,
      $container->get('plugin.manager.migration')
    );
  }

  /** * {@inheritdoc} */
  public function getDerivativeDefinitions($base_plugin_definition$base_plugin_definitions = NULL) {
    try {
      foreach (static::getSourcePlugin('d6_taxonomy_vocabulary') as $row) {
        $source_vid = $row->getSourceProperty('vid');
        $definition = $base_plugin_definition;
        $definition['source']['vid'] = $source_vid;
        // migrate_drupal_migration_plugins_alter() adds to this definition.         $this->derivatives[$source_vid] = $definition;
      }
    }
    catch (\Exception $e) {
      // It is possible no D6 tables are loaded so just eat exceptions.     }
    return $this->derivatives;
  }

  public function getRequirements(): array {
    return $this->requirements;
  }

  /** * {@inheritdoc} */
  public function checkRequirements() {
    // Check whether the current migration source and destination plugin     // requirements are met or not.     if ($this->getSourcePlugin() instanceof RequirementsInterface) {
      $this->getSourcePlugin()->checkRequirements();
    }
    if ($this->getDestinationPlugin() instanceof RequirementsInterface) {
      $this->getDestinationPlugin()->checkRequirements();
    }

    if (empty($this->requirements)) {
      // There are no requirements to check.       return;
    }
    /** @var \Drupal\migrate\Plugin\MigrationInterface[] $required_migrations */
    

  public function createStub($migration_id, array $source_ids, array $default_values = []$key_by_destination_ids = NULL) {
    $migrations = $this->migrationPluginManager->createInstances([$migration_id]);
    if (!$migrations) {
      throw new PluginNotFoundException($migration_id);
    }
    if (count($migrations) !== 1) {
      throw new \LogicException(sprintf('Cannot stub derivable migration "%s". You must specify the id of a specific derivative to stub.', $migration_id));
    }
    $migration = reset($migrations);
    $source_id_keys = array_keys($migration->getSourcePlugin()->getIds());
    if (count($source_id_keys) !== count($source_ids)) {
      throw new \InvalidArgumentException('Expected and provided source id counts do not match.');
    }
    if (array_keys($source_ids) === range(0, count($source_ids) - 1)) {
      $source_ids = array_combine($source_id_keys$source_ids);
    }
    $stub = $this->doCreateStub($migration$source_ids$default_values);

    // If the return from ::import is numerically indexed, and we aren't     // requesting the raw return value, index it associatively using the     // destination id keys.
$class = ltrim($this->getPluginClass(), '\\');

    /** @var \Drupal\migrate\Plugin\MigratePluginManager $plugin_manager */
    $plugin_manager = $this->container->get('plugin.manager.migrate.source');

    foreach ($plugin_manager->getDefinitions() as $id => $definition) {
      if (ltrim($definition['class'], '\\') == $class) {
        $this->plugin = $plugin_manager
          ->createInstance($id$configuration$this->migration->reveal());

        $this->migration
          ->getSourcePlugin()
          ->willReturn($this->plugin);

        return $this->plugin;
      }
    }
    $this->fail('No plugin found for class ' . $class);
  }

  /** * Tests the source plugin against a particular data set. * * @param array $source_data * The source data that the source plugin will read. * @param array $expected_data * The result rows the source plugin is expected to return. * @param mixed $expected_count * (optional) How many rows the source plugin is expected to return. * Defaults to count($expected_data). If set to a non-null, non-numeric * value (like FALSE or 'nope'), the source plugin will not be counted. * @param array $configuration * (optional) Configuration for the source plugin. * @param mixed $high_water * (optional) The value of the high water field. * * @dataProvider providerSource */
/** * {@inheritdoc} */
  public function getFieldPlugin($field_type, MigrationInterface $migration) {
    return parent::getFieldPlugin($field_type$migration);
  }

  /** * {@inheritdoc} */
  public function getSourcePlugin($core) {
    return parent::getSourcePlugin($core);
  }

}
parent::setUp();
    $this->sourceDatabase->schema()->dropTable('profile_field');
  }

  /** * Tests exception is thrown when profile_fields tables do not exist. */
  public function testCheckRequirements() {
    $this->expectException(RequirementsException::class);
    $this->expectExceptionMessage('Profile module not enabled on source site');
    $this->getMigration('user_profile_field')
      ->getSourcePlugin()
      ->checkRequirements();
  }

}
Home | Imprint | This part of the site doesn't use cookies.