Migration.php 5.57 KB
Newer Older
1
2
3
4
5
<?php

namespace Drupal\migrate\Plugin\migrate\process;

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6
use Drupal\migrate\MigrateSkipProcessException;
7
use Drupal\migrate\Plugin\MigratePluginManager;
8
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
9
use Drupal\migrate\Plugin\MigrateIdMapInterface;
10
use Drupal\migrate\ProcessPluginBase;
11
use Drupal\migrate\Plugin\MigrationInterface;
12
use Drupal\migrate\MigrateExecutableInterface;
13
14
15
16
17
18
19
20
21
22
23
24
25
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Calculates the value of a property based on a previous migration.
 *
 * @MigrateProcessPlugin(
 *   id = "migration"
 * )
 */
class Migration extends ProcessPluginBase implements ContainerFactoryPluginInterface {

  /**
26
27
   * The process plugin manager.
   *
28
29
30
31
32
   * @var \Drupal\migrate\Plugin\MigratePluginManager
   */
  protected $processPluginManager;

  /**
33
   * The migration plugin manager.
34
   *
35
   * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
36
   */
37
  protected $migrationPluginManager;
38
39
40
41

  /**
   * {@inheritdoc}
   */
42
  public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, MigrationPluginManagerInterface $migration_plugin_manager, MigratePluginManager $process_plugin_manager) {
43
    parent::__construct($configuration, $plugin_id, $plugin_definition);
44
    $this->migrationPluginManager = $migration_plugin_manager;
45
46
47
48
49
50
51
    $this->migration = $migration;
    $this->processPluginManager = $process_plugin_manager;
  }

  /**
   * {@inheritdoc}
   */
52
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
53
54
55
56
57
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $migration,
58
      $container->get('plugin.manager.migration'),
59
60
61
62
63
64
65
      $container->get('plugin.manager.migrate.process')
    );
  }

  /**
   * {@inheritdoc}
   */
66
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
67
68
69
70
71
72
73
74
75
    $migration_ids = $this->configuration['migration'];
    if (!is_array($migration_ids)) {
      $migration_ids = array($migration_ids);
    }
    $scalar = FALSE;
    if (!is_array($value)) {
      $scalar = TRUE;
      $value = array($value);
    }
76
    $this->skipOnEmpty($value);
77
    $self = FALSE;
78
    /** @var \Drupal\migrate\Plugin\MigrationInterface[] $migrations */
79
80
    $destination_ids = NULL;
    $source_id_values = array();
81
    $migrations = $this->migrationPluginManager->createInstances($migration_ids);
82
    foreach ($migrations as $migration_id => $migration) {
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
      if ($migration_id == $this->migration->id()) {
        $self = TRUE;
      }
      if (isset($this->configuration['source_ids'][$migration_id])) {
        $configuration = array('source' => $this->configuration['source_ids'][$migration_id]);
        $source_id_values[$migration_id] = $this->processPluginManager
          ->createInstance('get', $configuration, $this->migration)
          ->transform(NULL, $migrate_executable, $row, $destination_property);
      }
      else {
        $source_id_values[$migration_id] = $value;
      }
      // Break out of the loop as soon as a destination ID is found.
      if ($destination_ids = $migration->getIdMap()->lookupDestinationID($source_id_values[$migration_id])) {
        break;
      }
    }
100

101
102
103
104
    if (!$destination_ids && !empty($this->configuration['no_stub'])) {
      return NULL;
    }

105
    if (!$destination_ids && ($self || isset($this->configuration['stub_id']) || count($migrations) == 1)) {
106
107
108
109
110
111
112
113
114
115
116
      // If the lookup didn't succeed, figure out which migration will do the
      // stubbing.
      if ($self) {
        $migration = $this->migration;
      }
      elseif (isset($this->configuration['stub_id'])) {
        $migration = $migrations[$this->configuration['stub_id']];
      }
      else {
        $migration = reset($migrations);
      }
117
      $destination_plugin = $migration->getDestinationPlugin(TRUE);
118
      // Only keep the process necessary to produce the destination ID.
119
      $process = $migration->getProcess();
120

121
      // We already have the source ID values but need to key them for the Row
122
123
124
125
126
127
      // constructor.
      $source_ids = $migration->getSourcePlugin()->getIds();
      $values = array();
      foreach (array_keys($source_ids) as $index => $source_id) {
        $values[$source_id] = $source_id_values[$migration->id()][$index];
      }
128

129
      $stub_row = new Row($values + $migration->getSourceConfiguration(), $source_ids, TRUE);
130

131
132
133
134
135
136
      // Do a normal migration with the stub row.
      $migrate_executable->processRow($stub_row, $process);
      $destination_ids = array();
      try {
        $destination_ids = $destination_plugin->import($stub_row);
      }
137
      catch (\Exception $e) {
138
139
140
141
142
        $migration->getIdMap()->saveMessage($stub_row->getSourceIdValues(), $e->getMessage());
      }

      if ($destination_ids) {
        $migration->getIdMap()->saveIdMapping($stub_row, $destination_ids, MigrateIdMapInterface::STATUS_NEEDS_UPDATE);
143
144
145
146
147
148
149
150
151
152
153
154
155
156
      }
    }
    if ($destination_ids) {
      if ($scalar) {
        if (count($destination_ids) == 1) {
          return reset($destination_ids);
        }
      }
      else {
        return $destination_ids;
      }
    }
  }

157
  /**
158
   * Skips the migration process entirely if the value is FALSE.
159
160
161
162
163
164
165
166
167
168
169
170
   *
   * @param mixed $value
   *   The incoming value to transform.
   *
   * @throws \Drupal\migrate\MigrateSkipProcessException
   */
  protected function skipOnEmpty($value) {
    if (!array_filter($value)) {
      throw new MigrateSkipProcessException();
    }
  }

171
}