function MigrationPluginManager::buildDependencyMigration

Same name and namespace in other branches
  1. 9 core/modules/migrate/src/Plugin/MigrationPluginManager.php \Drupal\migrate\Plugin\MigrationPluginManager::buildDependencyMigration()
  2. 8.9.x core/modules/migrate/src/Plugin/MigrationPluginManager.php \Drupal\migrate\Plugin\MigrationPluginManager::buildDependencyMigration()
  3. 11.x core/modules/migrate/src/Plugin/MigrationPluginManager.php \Drupal\migrate\Plugin\MigrationPluginManager::buildDependencyMigration()

Builds a dependency tree for the migrations and set their order.

Parameters

\Drupal\migrate\Plugin\MigrationInterface[] $migrations: Array of loaded migrations with their declared dependencies.

array $dynamic_ids: Keys are dynamic ids (for example node:*) values are a list of loaded migration ids (for example node:page, node:article).

Return value

array An array of migrations.

Overrides MigrateBuildDependencyInterface::buildDependencyMigration

1 call to MigrationPluginManager::buildDependencyMigration()
MigrationPluginManager::createInstances in core/modules/migrate/src/Plugin/MigrationPluginManager.php
Create pre-configured instance of plugin derivatives.

File

core/modules/migrate/src/Plugin/MigrationPluginManager.php, line 159

Class

MigrationPluginManager
Plugin manager for migration plugins.

Namespace

Drupal\migrate\Plugin

Code

public function buildDependencyMigration(array $migrations, array $dynamic_ids) {
  // Migration dependencies can be optional or required. If an optional
  // dependency does not run, the current migration is still OK to go. Both
  // optional and required dependencies (if run at all) must run before the
  // current migration.
  $dependency_graph = [];
  $required_dependency_graph = [];
  $have_optional = FALSE;
  foreach ($migrations as $migration) {
    /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
    $id = $migration->id();
    $requirements[$id] = [];
    $dependency_graph[$id]['edges'] = [];
    $migration_dependencies = $migration->getMigrationDependencies(TRUE);
    if (isset($migration_dependencies['required'])) {
      foreach ($migration_dependencies['required'] as $dependency) {
        if (!isset($dynamic_ids[$dependency])) {
          $this->addDependency($required_dependency_graph, $id, $dependency, $dynamic_ids);
        }
        $this->addDependency($dependency_graph, $id, $dependency, $dynamic_ids);
      }
    }
    if (!empty($migration_dependencies['optional'])) {
      foreach ($migration_dependencies['optional'] as $dependency) {
        $this->addDependency($dependency_graph, $id, $dependency, $dynamic_ids);
      }
      $have_optional = TRUE;
    }
  }
  $dependency_graph = (new Graph($dependency_graph))->searchAndSort();
  if ($have_optional) {
    $required_dependency_graph = (new Graph($required_dependency_graph))->searchAndSort();
  }
  else {
    $required_dependency_graph = $dependency_graph;
  }
  $weights = [];
  foreach ($migrations as $migration_id => $migration) {
    // Populate a weights array to use with array_multisort() later.
    $weights[] = $dependency_graph[$migration_id]['weight'];
    if (!empty($required_dependency_graph[$migration_id]['paths'])) {
      $migration->set('requirements', $required_dependency_graph[$migration_id]['paths']);
    }
  }
  // Sort weights, labels, and keys in the same order as each other.
  array_multisort($weights, SORT_DESC, SORT_NUMERIC, array_keys($migrations), SORT_ASC, SORT_NATURAL, $migrations);
  return $migrations;
}

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