-
Notifications
You must be signed in to change notification settings - Fork 12
/
migration_boilerplate.module
executable file
·189 lines (161 loc) · 5.61 KB
/
migration_boilerplate.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
<?php
/**
* @file
* Contains migration_boilerplate.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\MigrateSourceInterface;
use Drupal\migrate\Row;
/**
* Implements hook_help().
*/
function migration_boilerplate_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the migration_boilerplate module.
case 'help.page.migration_boilerplate':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Drupal 8 Migration Boilerplate Code') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_migrate_prepare_row().
*/
function migration_boilerplate_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {
// Content Types we are not importing / skipping.
$skip_types = [
// Put stuff here.
];
// Fields to skip for now.
$skip_fields = [
// Put stuff here.
];
// Entity types to skip for now.
$entity_skip = [
// Put stuff here.
];
// Vocab Skip.
$vocab_skip = [
// Put stuff here.
];
// Skip vocab.
if (in_array($row->getSourceProperty('machine_name'), $vocab_skip)) {
// This makes it so we don't need to reprocess the row.
throw new MigrateSkipRowException('', TRUE);
}
// Skip content types.
if (in_array($row->getSourceProperty('bundle'), $skip_types)) {
// This makes it so we don't need to reprocess the row.
throw new MigrateSkipRowException('', TRUE);
}
// Skip content types.
if (in_array($row->getSourceProperty('type'), $skip_types)) {
// This makes it so we don't need to reprocess the row.
throw new MigrateSkipRowException('', TRUE);
}
// Skip fields.
if (in_array($row->getSourceProperty('field_name'), $skip_fields)) {
// This makes it so we don't need to reprocess the row.
throw new MigrateSkipRowException('', TRUE);
}
// Skip entity types.
if (in_array($row->getSourceProperty('instances')[0]['entity_type'], $entity_skip)) {
// This makes it so we don't need to reprocess the row.
throw new MigrateSkipRowException('', TRUE);
}
// Skip entity types.
if (in_array($row->getSourceProperty('entity_type'), $entity_skip)) {
// This makes it so we don't need to reprocess the row.
throw new MigrateSkipRowException('', TRUE);
}
// Do various tasks based on the migration id.
switch ($migration->id()) {
case 'upgrade_d7_field':
// Move a field to a new type / bundle example.
$instances = $row->getSourceProperty('instances');
switch ($instances[0]['bundle']) {
case 'OLD_BUNDLE':
$instances[0]['bundle'] = 'NEW_BUNDLE';
// Merge fields to person type.
$row->setSourceProperty('instances', $instances);
break;
}
break;
case 'upgrade_d7_node_type':
// Change the content type name example.
switch ($row->getSourceProperty('type')) {
case 'OLD_NAME':
$row->setSourceProperty('name', 'NEW_NAME');
break;
}
break;
case 'upgrade_d7_field_instance':
case 'upgrade_d7_field_formatter_settings':
case 'upgrade_d7_field_instance_widget_settings':
// Move a field to a new type / bundle example continued.
$instances = $row->getSourceProperty('instances');
switch ($instances[0]['bundle']) {
case 'OLD_BUNDLE':
$instances[0]['bundle'] = 'NEW_BUNDLE';
// Merge fields to person type.
$row->setSourceProperty('instances', $instances);
break;
}
// Move a field to a new type / bundle example continued.
$bundle = $row->getSourceProperty('bundle');
switch ($bundle) {
case 'OLD_BUNDLE':
$row->setSourceProperty('bundle', 'NEW_BUNDLE');
// Merge fields to person type.
break;
}
if ($migration->id() === 'upgrade_d7_field_formatter_settings') {
// Some typical legacy formatter type conversions.
$formatter = $row->getSourceProperty('formatter');
switch ($formatter['type']) {
case 'taxonomy_term_reference_plain':
$formatter['type'] = 'entity_reference_label';
$row->setSourceProperty('formatter', $formatter);
break;
case 'text_plain':
$formatter['type'] = 'basic_string';
$row->setSourceProperty('formatter', $formatter);
break;
case 'link_plain':
$formatter['type'] = 'link_default';
$row->setSourceProperty('formatter', $formatter);
break;
case 'file_rendered':
$formatter['type'] = 'entity_reference_entity_view';
$row->setSourceProperty('formatter', $formatter);
break;
case 'rss_enclosure':
$formatter['type'] = 'file_rss_enclosure';
$row->setSourceProperty('formatter', $formatter);
break;
}
}
if ($migration->id() === 'upgrade_d7_field_instance_widget_settings') {
// Adjust a widget type example.
$widget = $row->getSourceProperty('widget');
switch ($widget['type']) {
case 'inline_entity_form':
$widget['type'] = 'inline_entity_form_simple';
$row->setSourceProperty('widget', $widget);
break;
}
}
break;
case 'upgrade_d7_node_NAME':
$title = $row->getSourceProperty('title');
if (stripos($title, 'sad') !== FALSE) {
// Skip old sad posts.
throw new MigrateSkipRowException('', TRUE);
}
break;
}
}