function MigrateStub::createStub

Same name in other branches
  1. 9 core/modules/migrate/src/MigrateStub.php \Drupal\migrate\MigrateStub::createStub()
  2. 8.9.x core/modules/migrate/src/MigrateStub.php \Drupal\migrate\MigrateStub::createStub()
  3. 10 core/modules/migrate/src/MigrateStub.php \Drupal\migrate\MigrateStub::createStub()

Creates a stub.

Parameters

string $migration_id: The migration to stub.

array $source_ids: An array of source ids.

array $default_values: (optional) An array of default values to add to the stub.

bool $key_by_destination_ids: (optional) NULL or TRUE to force indexing of the return array by destination id keys (default), or FALSE to return the raw return value of the destination plugin's ::import() method. The return value from MigrateDestinationInterface::import() is very poorly defined as "The entity ID or an indication of success". In practice, the mapping systems expect and all destination plugins return an array of destination identifiers. Unfortunately these arrays are inconsistently keyed. The core destination plugins return a numerically indexed array of destination identifiers, but several contrib destinations return an array of identifiers indexed by the destination keys. This method will generally index all return arrays for consistency and to provide as much information as possible, but this parameter is added for backwards compatibility to allow accessing the original array.

Return value

array|false An array of destination ids for the new stub, keyed by destination id key, or false if the stub failed.

Throws

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\migrate\MigrateException

File

core/modules/migrate/src/MigrateStub.php, line 64

Class

MigrateStub
Provides the migrate stubbing service.

Namespace

Drupal\migrate

Code

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.
    if ($key_by_destination_ids !== FALSE && array_keys($stub) === range(0, count($stub) - 1)) {
        $stub = array_combine(array_keys($migration->getDestinationPlugin()
            ->getIds()), $stub);
    }
    return $stub;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.