createInstances example

public function testLabelExist() {
    // Install all available modules.     $module_handler = $this->container->get('module_handler');
    $modules = $this->coreModuleListDataProvider();
    $modules_enabled = $module_handler->getModuleList();
    $modules_to_enable = array_keys(array_diff_key($modules$modules_enabled));
    $this->enableModules($modules_to_enable);

    /** @var \Drupal\migrate\Plugin\MigrationPluginManager $plugin_manager */
    $plugin_manager = $this->container->get('plugin.manager.migration');
    // Get all the migrations     $migrations = $plugin_manager->createInstances(array_keys($plugin_manager->getDefinitions()));
    /** @var \Drupal\migrate\Plugin\Migration $migration */
    foreach ($migrations as $migration) {
      $migration_id = $migration->getPluginId();
      $this->assertNotEmpty($migration->label()new FormattableMarkup('Label found for @migration_id.', ['@migration_id' => $migration_id]));
    }
  }

}
/** * Migration plugin for the Drupal 7 node translations. */
class D7NodeTranslation extends Migration implements MigrationWithFollowUpInterface {

  /** * {@inheritdoc} */
  public function generateFollowUpMigrations() {
    $this->migrationPluginManager->clearCachedDefinitions();
    return $this->migrationPluginManager->createInstances('d7_entity_reference_translation');
  }

}
    $this->systemData = $this->store->get('system_data');

    // If data is missing or this is the wrong step, start over.     if (!$version || !$this->migrations || !$this->systemData ||
      ($this->store->get('step') != 'review')) {
      return $this->restartUpgradeForm();
    }

    $form = parent::buildForm($form$form_state);
    $form['#title'] = $this->t('What will be upgraded?');

    $migrations = $this->migrationPluginManager->createInstances(array_keys($this->store->get('migrations')));

    // Get the upgrade states for the source modules.     $display = $this->migrationState->getUpgradeStates($version$this->systemData, $migrations);

    // Missing migrations.     $missing_module_list = [
      '#type' => 'details',
      '#open' => TRUE,
      '#title' => $this->t('Modules that will not be upgraded'),
      '#summary_attributes' => ['id' => ['error']],
      '#description' => $this->t("The new site is missing modules corresponding to the old site's modules. Unless they are installed prior to the upgrade, configuration and/or content needed by them will not be available on your new site. <a href=':review'>Read the checklist</a> to help decide what to do.", [':review' => 'https://www.drupal.org/docs/8/upgrade/upgrade-using-web-browser#pre-upgrade-analysis']),
      
$id_map = $this->prophesize(MigrateIdMapInterface::class);

    $migration = $this->prophesize(MigrationInterface::class);
    $migration->getIdMap()->willReturn($id_map->reveal());
    $migration->getDestinationPlugin(TRUE)->willReturn($destination_plugin->reveal());
    $migration->getProcessPlugins([])->willReturn([]);
    $migration->getProcess()->willReturn([]);
    $migration->getSourceConfiguration()->willReturn([]);
    $migration->getSourcePlugin()->willReturn($source_plugin->reveal());

    $this->migrationPluginManager->createInstances(['test_migration'])->willReturn([$migration->reveal()]);

    $stub = new MigrateStub($this->migrationPluginManager->reveal());

    $this->assertSame(['id' => 2]$stub->createStub('test_migration', ['id' => 1][]));
  }

  /** * Tests that an error is logged if the plugin manager throws an exception. */
  public function testExceptionOnPluginNotFound() {
    $this->migrationPluginManager->createInstances(['test_migration'])->willReturn([]);
    
// Disable the test module and the list should be empty again.     $this->disableModules(['migrate_external_translated_test']);
    $migration_plugins = $this->container->get('plugin.manager.migration')->getDefinitions();
    // All the plugins provided by core depend on migrate_drupal.     $this->assertEmpty($migration_plugins);

    // Enable migrate_drupal to test that the plugins can now be discovered.     $this->enableModules(['migrate_drupal']);

    // Make sure retrieving these migration plugins in the absence of a database     // connection does not throw any errors.     $migration_plugins = $this->container->get('plugin.manager.migration')->createInstances([]);
    // Any database-based source plugins should fail a requirements test in the     // absence of a source database connection (e.g., a connection with the     // 'migrate' key).     $source_plugins = array_map(function D$migration_plugin) {
      return $migration_plugin->getSourcePlugin();
    }$migration_plugins);
    foreach ($source_plugins as $id => $source_plugin) {
      if ($source_plugin instanceof RequirementsInterface) {
        try {
          $source_plugin->checkRequirements();
        }
        
/** * {@inheritdoc} */
  protected static $modules = ['node'];

  /** * Tests node translation migrations with translation disabled. */
  public function testNoTranslations() {
    // Without content_translation, there should be no translation migrations.     $migrations = $this->container->get('plugin.manager.migration')->createInstances('d7_node_translation');
    $this->assertEmpty($migrations);
  }

  /** * Tests node translation migrations with translation enabled. */
  public function testTranslations() {
    // With content_translation, there should be translation migrations for     // each content type.     $this->enableModules(['language', 'content_translation', 'filter']);
    $this->assertTrue($this->container->get('plugin.manager.migration')->hasDefinition('d7_node_translation:article'), "Node translation migrations exist after content_translation installed");
  }

  public function testTransformWithStubSkipping() {
    $migration_plugin = $this->prophesize(MigrationInterface::class);
    $migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);

    $destination_id_map = $this->prophesize(MigrateIdMapInterface::class);
    $destination_migration = $this->prophesize(MigrationInterface::class);
    $destination_migration->getIdMap()->willReturn($destination_id_map->reveal());
    $destination_id_map->lookupDestinationIds([1])->willReturn(NULL);

    // Ensure the migration plugin manager returns our migration.     $migration_plugin_manager->createInstances(Argument::exact(['destination_migration']))
      ->willReturn(['destination_migration' => $destination_migration->reveal()]);

    $configuration = [
      'no_stub' => TRUE,
      'migration' => 'destination_migration',
    ];

    $migration_plugin->id()->willReturn('actual_migration');
    $destination_migration->getDestinationPlugin(TRUE)->shouldNotBeCalled();

    $migration = MigrationLookup::create($this->prepareContainer()$configuration, '', []$migration_plugin->reveal());
    
$this->assertSame('3', $node->get('field_reference_2')->target_id);
    $translation = $node->getTranslation('en');
    $this->assertSame('2', $translation->get('field_reference')->target_id);
    $this->assertSame('2', $translation->get('field_reference_2')->target_id);

    $user = User::load(2);
    $this->assertSame('3', $user->get('field_reference')->target_id);

    // Run the follow-up migrations.     $migration_plugin_manager = $this->container->get('plugin.manager.migration');
    $migration_plugin_manager->clearCachedDefinitions();
    $follow_up_migrations = $migration_plugin_manager->createInstances('d7_entity_reference_translation');
    $this->executeMigrations(array_keys($follow_up_migrations));

    // Test the entity reference field after the follow-up migrations.     $node = Node::load(2);
    $this->assertSame('4', $node->get('field_reference')->target_id);
    $this->assertSame('6', $node->get('field_reference_2')->target_id);
    $translation = $node->getTranslation('is');
    $this->assertSame('4', $translation->get('field_reference')->target_id);
    $this->assertSame('4', $translation->get('field_reference_2')->target_id);

    $node = Node::load(4);
    

  public function execute() {
    $migration_plugin_manager = \Drupal::service('plugin.manager.migration');
    $definitions = $migration_plugin_manager->getDefinitions();
    if ($definitions['node_migration_no_migrate_drupal']['label'] !== 'Node Migration No Migrate Drupal') {
      throw new InvalidPluginDefinitionException('node_migration_no_migrate_drupal');
    }
    $migrations = $migration_plugin_manager->createInstances('');
    $result = (new MigrateExecutable($migrations['node_migration_no_migrate_drupal']))->import();
    if ($result !== MigrationInterface::RESULT_COMPLETED) {
      throw new \RuntimeException('Migration failed');
    }

    return [
      '#type' => 'markup',
      '#markup' => 'Migration was successful.',
    ];
  }

}
/** * Migration plugin for the Drupal 6 node translations. */
class D6NodeTranslation extends Migration implements MigrationWithFollowUpInterface {

  /** * {@inheritdoc} */
  public function generateFollowUpMigrations() {
    $this->migrationPluginManager->clearCachedDefinitions();
    return $this->migrationPluginManager->createInstances('d6_entity_reference_translation');
  }

}
/** * Prepare any dependent migrations. * * @param array $id_mappings * A list of ID mappings keyed by migration IDs. Each ID mapping is a list * of two arrays, the first are source IDs and the second are destination * IDs. */
  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]);
        }
      }
    }
  }

  
public function buildForm(array $form, FormStateInterface $form_state) {
    // Get all the data needed for this form.     $migrations = $this->store->get('migrations');

    // If data is missing or this is the wrong step, start over.     if (!$migrations || ($this->store->get('step') != 'idconflict')) {
      return $this->restartUpgradeForm();
    }

    $migration_ids = array_keys($migrations);
    // Check if there are conflicts. If none, just skip this form!     $migrations = $this->migrationPluginManager->createInstances($migration_ids);

    $translated_content_conflicts = $content_conflicts = [];

    $results = (new IdAuditor())->auditMultiple($migrations);

    /** @var \Drupal\migrate\Audit\AuditResult $result */
    foreach ($results as $result) {
      $destination = $result->getMigration()->getDestinationPlugin();
      if ($destination instanceof EntityContentBase && $destination->isTranslationDestination()) {
        // Translations are not yet supported by the audit system. For now, we         // only warn the user to be cautious when migrating translated content.
/** * Tests Migration::createInstancesByTag(). * * @covers ::createInstancesByTag * * @dataProvider providerCreateInstanceByTag */
  public function testCreateInstancesByTag($tags$expected) {
    // The test module includes a migration that does not use the migration_tags     // property. It is there to confirm that it is not included in the results.     // We create it to ensure it is a valid migration.     $migration = $this->migrationPluginManager->createInstances(['tag_test_no_tag']);
    $this->assertArrayHasKey('tag_test_no_tag', $migration);
    $migrations = $this->migrationPluginManager->createInstancesByTag($tags);
    $actual = array_keys($migrations);
    $this->assertSame($expected$actual);
  }

  /** * Data provider for testCreateInstancesByTag. */
  public function providerCreateInstanceByTag() {
    return [
      

  public function __construct(MigrationPluginManagerInterface $migration_plugin_manager) {
    $this->migrationPluginManager = $migration_plugin_manager;
  }

  /** * {@inheritdoc} */
  public function lookup($migration_id, array $source_id_values) {
    $results = [];
    $migrations = $this->migrationPluginManager->createInstances($migration_id);
    if (!$migrations) {
      if (is_array($migration_id)) {
        if (count($migration_id) != 1) {
          throw new PluginException("Plugin IDs '" . implode("', '", $migration_id) . "' were not found.");
        }
        $migration_id = reset($migration_id);
      }
      throw new PluginNotFoundException($migration_id);
    }
    foreach ($migrations as $migration) {
      if ($result = $this->doLookup($migration$source_id_values)) {
        
$node = Node::load(12)->getTranslation('en');
    $this->assertSame('10', $node->get('field_reference')->target_id);
    $this->assertSame('10', $node->get('field_reference_2')->target_id);
    $translation = $node->getTranslation('fr');
    $this->assertSame('11', $translation->get('field_reference')->target_id);
    $this->assertSame('11', $translation->get('field_reference_2')->target_id);

    // Run the follow-up migrations.     $migration_plugin_manager = $this->container->get('plugin.manager.migration');
    $migration_plugin_manager->clearCachedDefinitions();
    $follow_up_migrations = $migration_plugin_manager->createInstances('d6_entity_reference_translation');
    $this->executeMigrations(array_keys($follow_up_migrations));

    // Test the entity reference field after the follow-up migrations.     $node = Node::load(10);
    $this->assertSame('12', $node->get('field_reference')->target_id);
    $this->assertSame('12', $node->get('field_reference_2')->target_id);
    $translation = $node->getTranslation('fr');
    $this->assertSame('12', $translation->get('field_reference')->target_id);
    $this->assertSame('12', $translation->get('field_reference_2')->target_id);

    $node = Node::load(12)->getTranslation('en');
    
Home | Imprint | This part of the site doesn't use cookies.