You are here

public function MigrationDefinitionCreator::setReferenceDependencies in GatherContent 8.5

Set reference migration dependencies and processes.

1 call to MigrationDefinitionCreator::setReferenceDependencies()
MigrationDefinitionCreator::createMigrationDefinition in src/MigrationDefinitionCreator.php
Create migration definitions.

File

src/MigrationDefinitionCreator.php, line 245

Class

MigrationDefinitionCreator
Create dynamic migration definitions.

Namespace

Drupal\gathercontent

Code

public function setReferenceDependencies(array &$definitions) {
  if (empty($this->collectedReferences)) {
    return;
  }
  $collectedSubDependencies = [];
  foreach ($definitions as $definitionId => $definition) {
    if (!isset($this->collectedReferences[$definitionId])) {
      continue;
    }
    $references = $this->collectedReferences[$definitionId];
    foreach ($references as $element => $target) {
      $subDependencies = [];
      foreach ($target as $reference) {
        $subDefinition = $this
          ->buildMigrationDefinition([
          'projectId' => $reference['base_data']['projectId'],
          'templateId' => $reference['base_data']['templateId'],
          'entityType' => $reference['base_data']['entityType'],
          'contentType' => $reference['base_data']['contentType'],
        ], $reference['data'], 'entity_reference_revisions');
        $subDependencies[$subDefinition['id']] = $subDefinition;
        $key = implode('_', [
          $reference['base_data']['projectId'],
          $reference['base_data']['templateId'],
          $reference['base_data']['entityType'],
          $reference['base_data']['contentType'],
        ]);
        $collectedSubDependencies[$key][$subDefinition['id']] = $subDefinition;
      }
      $this
        ->setReferenceDependencies($subDependencies);
      $collected = [];
      foreach ($subDependencies as $subDefinitionId => $subDefinition) {
        $this->migrationDefinitionIds[] = $subDefinitionId;
        $definitions[$definitionId]['migration_dependencies']['optional'][] = $subDefinitionId;
        $definitions[$definitionId]['process']['collect_' . $subDefinitionId] = [
          'plugin' => 'migration_lookup',
          'migration' => $subDefinitionId,
          'source' => 'id',
        ];
        $collected[] = '@collect_' . $subDefinitionId;
      }
      if (!empty($collected)) {
        $definitions[$definitionId]['process']['get_collected_' . $element] = [
          'plugin' => 'get',
          'source' => $collected,
        ];
        $definitions[$definitionId]['process'][$element] = [
          [
            'plugin' => 'gather_content_reference_revision',
            'source' => '@get_collected_' . $element,
          ],
          [
            'plugin' => 'sub_process',
            'process' => [
              'target_id' => [
                'plugin' => 'extract',
                'source' => 'id',
                'index' => [
                  0,
                ],
              ],
              'target_revision_id' => [
                'plugin' => 'extract',
                'source' => 'id',
                'index' => [
                  1,
                ],
              ],
            ],
          ],
        ];
      }
    }
  }
  foreach ($collectedSubDependencies as $dependencies) {
    $this
      ->setLanguageDefinitions($dependencies);
    foreach ($dependencies as $subDefinitionId => $subDefinition) {
      $migration = Migration::create($subDefinition);
      $migration
        ->save();
    }
  }
}