forked from Ymbra/migrate_default_content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate_default_content.module
321 lines (290 loc) · 11.1 KB
/
migrate_default_content.module
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/**
* @file
* Functions for the migrate_default_content module.
*/
use Symfony\Component\Yaml\Parser;
/**
* Implements hook_migration_plugins_alter().
*
* Create migrations based on the csv files.
*/
function migrate_default_content_migration_plugins_alter(&$definitions) {
$source_dir = DRUPAL_ROOT . '/' . \Drupal::config('migrate_default_content.settings')->get('source_dir');
// First we create a structure with all the migrations because we want to know
// which ones are available and set dependencies.
$migrations = [];
if ($handle = opendir($source_dir)) {
while (($file = readdir($handle)) !== FALSE) {
$fileparts = explode('.', $file);
if ($file != "." && $file != "..") {
$language = '';
$entity_type = $fileparts[0];
$bundle = $fileparts[1];
// If the file is a translation save the language that is informed in
// the filename before the file extension.
if (isset($fileparts[3])) {
$language = $fileparts[2];
$type = $fileparts[3];
}
else {
$type = $fileparts[2];
}
$line = NULL;
switch ($type) {
case 'csv':
// Read first line of the file to get the fields to be migrated.
$csv = fopen($source_dir . '/' . $file, 'r');
$line = explode(',', fgets($csv));
fclose($csv);
break;
case 'yml':
$parser = isset($parser) ? $parser : new Parser();
$data = $parser->parse(file_get_contents($source_dir . '/' . $file));
$line = array_keys($data[0]);
break;
}
// Add the language to the migration name in the case it is a
// translation migration.
$id = empty($language) ? 'migrate_default_content_' . $entity_type . '_' . $bundle : 'migrate_default_content_' . $entity_type . '_' . $bundle . '_' . $language;
if ($line) {
$migrations[$id] = [
'first_line' => $line,
'entity_type' => $entity_type,
'bundle' => $bundle,
'type' => $type,
'filename' => $file,
'language' => $language,
];
}
}
}
closedir($handle);
}
// Add files migration.
if (file_exists($source_dir . '/files') && $handle = opendir($source_dir . '/files')) {
$data_rows = [];
while (($file = readdir($handle)) !== FALSE) {
if ($file != "." && $file != "..") {
$data_rows[] = ['filename' => $file];
}
}
$migrations['migrate_default_content_file'] = [
'data_rows' => $data_rows,
];
closedir($handle);
}
foreach ($migrations as $id => $migration) {
if ($id === 'migrate_default_content_file') {
continue;
}
$headers = $migration['first_line'];
// Assume the key is the first field of the csv.
$key = trim(reset($headers));
$migration_plugin = [
'id' => $id,
'migration_group' => 'migrate_default_content',
'label' => $id,
'class' => '\Drupal\migrate\Plugin\Migration',
'status' => 1,
'destination' => [
'plugin' => 'entity:' . $migration['entity_type'],
'translations' => FALSE,
],
];
if ('csv' === $migration['type']) {
$migration_plugin['source'] = [
'plugin' => $migration['type'],
'path' => $source_dir . '/' . $migration['filename'],
'header_row_count' => 1,
'keys' => [
0 => $key,
],
'constants' => [
'langcode' => $migration['language'],
],
];
}
if ('yml' === $migration['type']) {
$migration_plugin['source'] = [
'plugin' => 'yaml',
'file' => $source_dir . '/' . $migration['filename'],
'ids' => [
$key => [
'type' => 'string',
],
],
'fields' => array_combine($headers, $headers),
'constants' => [
'langcode' => $migration['language'],
],
];
}
// Get bundle info.
$bundle_key = \Drupal::service('entity_type.manager')->getDefinition($migration['entity_type'])->getKey('bundle');
if ($bundle_key) {
$migration_plugin['process'][$bundle_key] = [
'plugin' => 'default_value',
'default_value' => $migration['bundle'],
];
}
foreach ($headers as $field) {
// Headers can have spaces or line endings as is the case for the last
// field.
$field = trim($field);
if ($field == 'translation_origin') {
// This is the field that references the origin node of a translation,
// it is used to detect that the migration is a translation.
// Define the id type of the origin migration.
$id_type = \Drupal::service('entity_type.manager')->getDefinition($migration['entity_type'])->getKey('id');
// Define the origin migration.
$migration_plugin['process'][$id_type] = [
[
'plugin' => 'migration',
'source' => $field,
'migration' => 'migrate_default_content_' . $migration['entity_type'] . '_' . $migration['bundle'],
],
];
// Define the langcode of the translation.
$migration_plugin['process']['langcode'] = 'constants/langcode';
// Define the content as translatable.
$migration_plugin['destination']['translations'] = TRUE;
// Define the dependency to the origin migration.
$migration_plugin['migration_dependencies']['required'][] = 'migrate_default_content_' . $migration['entity_type'] . '_' . $migration['bundle'];
}
else {
// Extra config can be specified like field_name:bundle.
$extra = explode(':', $field);
// Handle CSV header fieldSubfield e.g. "bodyFormat".
$components = preg_split('/([A-Z])/', array_shift($extra), -1, PREG_SPLIT_DELIM_CAPTURE);
$field = array_shift($components);
$subfield = strtolower(implode($components));
$dest_field = empty($subfield) ? $field : $field . '/' . $subfield;
// It needs to be a normalized process.
// See: https://api.drupal.org/api/drupal/core!modules!migrate!src!Plugin!Migration.php/function/Migration%3A%3AgetProcessNormalized/
$migration_plugin['process'][$dest_field] = [
[
'plugin' => 'get',
'source' => $field . ucfirst($subfield) . (empty($extra) ? '' : ':' . implode(':', $extra)),
],
];
// If this an entity_reference field and a migrate_default_content
// migration exists add it automatically.
$field_definition = \Drupal::service('entity_field.manager')->getFieldDefinitions($migration['entity_type'], $migration['bundle'])[$field];
// Handle special field types.
if ($field_definition) {
switch ($field_definition->getType()) {
case 'entity_reference':
case 'entity_reference_revisions':
$settings = $field_definition->getItemDefinition()->getSettings();
$target_entity_type = $settings['target_type'];
// In the case of an entity reference the extra configuration is
// the bundles it should reference.
if (isset($settings['handler_settings']['target_bundles'])) {
$extra = array_merge($extra, $settings['handler_settings']['target_bundles']);
}
if (!empty($extra)) {
// Add all posible referenceable bundles.
foreach ($extra as $target_bundle) {
$target_id = 'migrate_default_content_' . $target_entity_type . '_' . $target_bundle;
$migration_plugin = migrate_default_content_add_target_migration($migrations, $migration_plugin, $dest_field, $target_id);
}
}
// Assume is a entity with only one bundle. e.g. user.
else {
$target_id = 'migrate_default_content_' . $target_entity_type . '_' . $target_entity_type;
$migration_plugin = migrate_default_content_add_target_migration($migrations, $migration_plugin, $dest_field, $target_id);
}
break;
case 'file':
case 'image':
$target_id = 'migrate_default_content_file';
$migration_plugin = migrate_default_content_add_target_migration($migrations, $migration_plugin, $dest_field, $target_id);
break;
default:
break;
}
}
}
}
// Look up for an override yml file.
$override_file_path = $source_dir . '/overrides/' . $migration['entity_type'] . '.' . $migration['bundle'] . '.yml';
if (file_exists($override_file_path)) {
$yaml = new Parser();
$override = $yaml->parse(file_get_contents($override_file_path));
$migration_plugin = array_merge_recursive($migration_plugin, $override);
}
$definitions[$id] = $migration_plugin;
}
if (isset($migrations['migrate_default_content_file'])) {
$definitions['migrate_default_content_file'] = [
'id' => 'migrate_default_content_file',
'migration_group' => 'migrate_default_content',
'label' => 'migrate_default_content_file',
'class' => '\Drupal\migrate\Plugin\Migration',
'status' => 1,
'source' => [
'plugin' => 'embedded_data',
'ids' => ['filename' => ['type' => 'string']],
'data_rows' => $migrations['migrate_default_content_file']['data_rows'],
'constants' => [
'source_base_path' => $source_dir . '/files/',
'destination_base_uri' => 'public://migrate_default_content_files/',
],
],
'process' => [
'filename' => [
'plugin' => 'get',
'source' => 'filename',
],
'destination_full_uri' => [
'plugin' => 'concat',
'source' => [
'constants/destination_base_uri',
'filename',
],
],
'source_full_path' => [
'plugin' => 'concat',
'source' => [
'constants/source_base_path',
'filename',
],
],
'uri' => [
'plugin' => 'file_copy',
'source' => [
'@source_full_path',
'@destination_full_uri',
],
],
],
'destination' => [
'plugin' => 'entity:file',
],
];
}
}
/**
* Add a migration to the process pipeline with its dependency.
*/
function migrate_default_content_add_target_migration($migrations, $migration_plugin, $dest_field, $target_id) {
if (isset($migrations[$target_id])) {
$dest_subfield = explode('/', $dest_field);
if (isset($dest_subfield[1]) && $dest_subfield[1] != 'target_id') {
return $migration_plugin;
}
// We don't need a source because only the first process plugin in
// the pipeline should have it and we already have the explode plugin.
$migration_plugin['process'][$dest_field][] = [
'plugin' => 'migration',
'migration' => $target_id,
'no_stub' => TRUE,
];
// Avoid dependencies on itself.
if ($target_id != $migration_plugin['id']) {
$migration_plugin['migration_dependencies']['required'][] = $target_id;
}
}
return $migration_plugin;
}