-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate_plugins.module
152 lines (131 loc) · 4.44 KB
/
migrate_plugins.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
<?php
/**
* @file
* Contains migrate_plugins.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\migrate\Plugin\MigrateSourceInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
/**
* Implements hook_help().
*/
function migrate_plugins_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the migrate_plugins module.
case 'help.page.migrate_plugins':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Provides migrate source and processor plugins.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_migrate_prepare_row().
*/
function migrate_plugins_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {
$references = [];
// Get the source primary ID key.
$source_ids = array_keys($source->getIds());
$source_primary_id = $source_ids[0];
// Migrations IDS that have OG reference fields to process.
$migration_ids = [
'iul_users' => 'user',
'iul_node_subject_posts' => 'node',
'iul_node_news' => 'node',
'iul_node_user_profiles' => 'node',
'iul_node_basic_pages' => 'node',
'iul_node_events' => 'node',
'iul_node_services' => 'node',
'iul_node_onsite_exhibit' => 'node',
'iul_node_digital_exhibit' => 'node',
'iul_node_feed_items' => 'node',
];
$name_patterns = "^og_|^field_affiliation$";
// Alter the migrations that has OG reference fields that needs
// custom select to load the OG references properly.
if (isset($migration_ids[$migration->id()])) {
$source_data = $row->getSource();
$field_names = array_keys($source_data);
$entity_type = $migration_ids[$migration->id()];
// Identify the OG reference fields.
foreach ($field_names as $field_name) {
// For OG fields we need to run a query on og_membership
// table and get the reference values.
if (preg_match("/$name_patterns/", $field_name)) {
$params = [
':name' => $field_name,
':entity_type' => $entity_type,
':group_type' => 'node',
':eid' => $row->getSourceProperty($source_primary_id),
];
$select = 'SELECT gid FROM {og_membership}
WHERE field_name = :name AND etid = :eid
AND entity_type = :entity_type AND group_type = :group_type';
// @var \Drupal\Core\Database\Statement $query
$query = $source->getDatabase()->query($select, $params);
$values = $query->fetchAll();
foreach ($values as $value) {
$references[] = ['target_id' => $value->gid];
}
// Set the OG references as property value.
if ($references) {
$row->setSourceProperty($field_name, $references);
}
}
}
}
// Get the source entity alias, we support the most common.
// @var \Drupal\file_entity\Plugin\migrate\source\d7\FileEntity $source
$plugin_definition = $source->getPluginDefinition();
$source_module = $source->getSourceModule();
// Fallback on source_provider when source module not defined.
if (empty($source_module)) {
$source_module = $plugin_definition['source_provider'];
}
switch ($source_module) {
case 'node':
$entity_id = $row->getSourceProperty('nid');
$source_path = "node/{$entity_id}";
break;
case 'user':
$entity_id = $row->getSourceProperty('uid');
$source_path = "user/{$entity_id}";
break;
case 'taxonomy':
$entity_id = $row->getSourceProperty('tid');
$source_path = "taxonomy/term/{$entity_id}";
break;
case 'file_entity':
$entity_id = $row->getSourceProperty('fid');
$source_path = "file/{$entity_id}";
break;
default:
$source_path = NULL;
break;
}
// Query for URL alias retrieval.
if ($source_path) {
$query = $source->getDatabase()->select('url_alias', 'ua');
$query->fields('ua', ['alias']);
$query->condition('ua.source', $source_path);
$alias = $query->execute()->fetchField();
// Set the alias property.
if (!empty($alias)) {
$row->setSourceProperty('path_alias', '/' . $alias);
}
}
}
/**
* Extract the relative path using 'files' as root directory.
*
* @param string $filepath
* The file path string.
*
* @return string
* The file path relative to files directory.
*/
function migrate_plugins_extract_relative_path($filepath) {
return str_replace('sites/default/files/', '', $filepath);
}