MigrationStorage.php
7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
* @file
* Contains \Drupal\migrate_drupal\MigrationStorage.
*/
namespace Drupal\migrate_drupal;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
use Drupal\migrate_drupal\Plugin\CckFieldMigrateSourceInterface;
use Drupal\migrate\MigrationStorage as BaseMigrationStorage;
use Drupal\migrate\Plugin\MigratePluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Storage for migration entities.
*/
class MigrationStorage extends BaseMigrationStorage {
/**
* A cached array of cck field plugins.
*
* @var array
*/
protected $cckFieldPlugins;
/**
* @var \Drupal\migrate_drupal\Plugin\MigratePluginManager
*/
protected $cckPluginManager;
/**
* Constructs a MigrationStorage object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\Component\Uuid\UuidInterface $uuid_service
* The UUID service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\migrate_drupal\Plugin\MigratePluginManager
* The cckfield plugin manager.
*/
public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, MigratePluginManager $cck_plugin_manager) {
parent::__construct($entity_type, $config_factory, $uuid_service, $language_manager);
$this->cckPluginManager = $cck_plugin_manager;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('config.factory'),
$container->get('uuid'),
$container->get('language_manager'),
$container->get('plugin.manager.migrate.cckfield')
);
}
/**
* {@inheritdoc}
*/
public function loadMultiple(array $ids = NULL) {
$ids_to_load = array();
$dynamic_ids = array();
if (isset($ids)) {
foreach ($ids as $id) {
// Evaluate whether or not this migration is dynamic in the form of
// migration_id:* to load all the additional migrations.
if (($n = strpos($id, ':')) !== FALSE) {
$base_id = substr($id, 0, $n);
$ids_to_load[] = $base_id;
// Get the ids of the additional migrations.
$sub_id = substr($id, $n + 1);
if ($sub_id == '*') {
// If the id of the additional migration is '*', get all of them.
$dynamic_ids[$base_id] = NULL;
}
elseif (!isset($dynamic_ids[$base_id]) || is_array($dynamic_ids[$base_id])) {
$dynamic_ids[$base_id][] = $sub_id;
}
}
else {
$ids_to_load[] = $id;
}
}
$ids = array_flip($ids);
}
else {
$ids_to_load = NULL;
}
/** @var \Drupal\migrate_drupal\Entity\MigrationInterface[] $entities */
$entities = parent::loadMultiple($ids_to_load);
if (!isset($ids)) {
// Changing the array being foreach()'d is not a good idea.
$return = array();
foreach ($entities as $entity_id => $entity) {
if ($plugin = $entity->getLoadPlugin()) {
$new_entities = $plugin->loadMultiple($this);
$this->postLoad($new_entities);
$this->getDynamicIds($dynamic_ids, $new_entities);
$return += $new_entities;
}
else {
$return[$entity_id] = $entity;
}
}
$entities = $return;
}
else {
foreach ($dynamic_ids as $base_id => $sub_ids) {
$entity = $entities[$base_id];
if ($plugin = $entity->getLoadPlugin()) {
unset($entities[$base_id]);
$new_entities = $plugin->loadMultiple($this, $sub_ids);
$this->postLoad($new_entities);
if (!isset($sub_ids)) {
unset($dynamic_ids[$base_id]);
$this->getDynamicIds($dynamic_ids, $new_entities);
}
$entities += $new_entities;
}
}
}
// Allow modules providing cck field plugins to alter the required
// migrations to assist with the migration a custom field type.
$this->applyCckFieldProcessors($entities);
// Build an array of dependencies and set the order of the migrations.
return $this->buildDependencyMigration($entities, $dynamic_ids);
}
/**
* Extract the dynamic id mapping from entities loaded by plugin.
*
* @param array $dynamic_ids
* Get the dynamic migration ids.
* @param array $entities
* An array of entities.
*/
protected function getDynamicIds(array &$dynamic_ids, array $entities) {
foreach (array_keys($entities) as $new_id) {
list($base_id, $sub_id) = explode(':', $new_id, 2);
$dynamic_ids[$base_id][] = $sub_id;
}
}
/**
* {@inheritdoc}
*/
public function save(EntityInterface $entity) {
if (strpos($entity->id(), ':') !== FALSE) {
throw new EntityStorageException("Dynamic migration '{$entity->id()}' can't be saved");
}
return parent::save($entity);
}
/**
* Allow any field type plugins to adjust the migrations as required.
*
* @param \Drupal\migrate\Entity\Migration[] $entities
* An array of migration entities.
*/
protected function applyCckFieldProcessors(array $entities) {
$method_map = $this->getMigrationPluginMethodMap();
foreach ($entities as $entity_id => $migration) {
// Allow field plugins to process the required migrations.
if (isset($method_map[$entity_id])) {
$method = $method_map[$entity_id];
$cck_plugins = $this->getCckFieldPlugins();
array_walk($cck_plugins, function ($plugin) use ($method, $migration) {
$plugin->$method($migration);
});
}
// If this is a CCK bundle migration, allow the cck field plugins to add
// any field type processing.
$source_plugin = $migration->getSourcePlugin();
if ($source_plugin instanceof CckFieldMigrateSourceInterface && strpos($entity_id, SourcePluginBase::DERIVATIVE_SEPARATOR)) {
$plugins = $this->getCckFieldPlugins();
foreach ($source_plugin->fieldData() as $field_name => $data) {
if (isset($plugins[$data['type']])) {
$plugins[$data['type']]->processCckFieldValues($migration, $field_name, $data);
}
}
}
}
}
/**
* Get an array of loaded cck field plugins.
*
* @return \Drupal\migrate_drupal\Plugin\MigrateCckFieldInterface[]
* An array of cck field process plugins.
*/
protected function getCckFieldPlugins() {
if (!isset($this->cckFieldPlugins)) {
$this->cckFieldPlugins = [];
foreach ($this->cckPluginManager->getDefinitions() as $definition) {
$this->cckFieldPlugins[$definition['id']] = $this->cckPluginManager->createInstance($definition['id']);
}
}
return $this->cckFieldPlugins;
}
/**
* Provides a map between migration ids and the cck field plugin method.
*
* @return array
* The map between migrations and cck field plugin processing methods.
*/
protected function getMigrationPluginMethodMap() {
return [
'd6_field' => 'processField',
'd6_field_instance' => 'processFieldInstance',
'd6_field_instance_widget_settings' => 'processFieldWidget',
'd6_field_formatter_settings' => 'processFieldFormatter',
];
}
}