Newer
Older
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
<?php
/**
* @file
* File for Drush Integration.
*/
use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface;
use Drupal\config_update\ConfigListInterface;
use Drupal\config_update\ConfigRevertInterface;
use Drupal\Core\Config\StorageInterface;
use Drush\Log\LogLevel;
/**
* Implements hook_drush_help_alter().
*/
function migrate_devel_drush_help_alter(&$command) {
if ($command['command'] === 'migrate-import') {
$command['options']['migrate-debug'] = 'Enable Debug Mode';
$command['options']['migrate-debug-pre'] = 'Enable Debug Mode (Before Row Save)';
}
if ($command['command'] === 'migrate-status') {
$command['options']['migrate-debug'] = 'Enable Debug Mode';
}
}
/**
* Implements hook_drush_command_alter().
*/
function migrate_devel_drush_command_alter(&$command) {
$cmd = $command['command'];
if ($cmd === 'migrate-import' || $cmd === 'migrate-status') {
// Reset all migrations
if (drush_get_option('migrate-debug')) {
migrate_devel_rebuild_migrations();
}
}
}
/**
* Clears cache for migrations and reverts config of migrations if needed.
*/
function migrate_devel_rebuild_migrations() {
// @TODO: Remove this legacy support of migrate_plus.
// After #2752335, it uses the default Migrate Plugin Manager
/* @var $discovery CachedDiscoveryInterface */
if (\Drupal::hasService('plugin.manager.config_entity_migration')) {
$discovery = \Drupal::service('plugin.manager.config_entity_migration');
} else {
$discovery = \Drupal::service('plugin.manager.migration');
}
// Reset cached migrations
$discovery->clearCachedDefinitions();
/* @var $discovery CachedDiscoveryInterface */
if (\Drupal::moduleHandler()->moduleExists('migrate_plus')) {
migrate_devel_revert_migrate_config($discovery);
}
drush_log(dt('Reset Cached Migrations'), LogLevel::DEBUG);
}
/**
* Reverts migrate config for migrate_plus
*
* @param CachedDiscoveryInterface $discovery
*/
function migrate_devel_revert_migrate_config($discovery) {
// If migrate_plus and config_update exists, revert the config.
if (\Drupal::moduleHandler()->moduleExists('config_update')) {
/* @var $config_revert ConfigRevertInterface */
$config_revert = \Drupal::service('config_update.config_update');
foreach ($discovery->getDefinitions() as $definition) {
$config_revert->revert('migration', $definition['id']);
}
drush_log(dt('Reverted Existing Migration Configs'));
// Revert missing config
migrate_devel_revert_missing_config('migration');
} else {
drush_log(dt('Missing config_update for revert.'));
}
}
/**
* Reverts missing config for a specific config type.
*
* @param string $type
*/
function migrate_devel_revert_missing_config($type) {
/* @var ConfigRevertInterface $config_revert */
$config_revert = \Drupal::service('config_update.config_update');
// Now we need to add any new migrations.
/* @var ConfigListInterface $config_lister */
$config_lister = \Drupal::service('config_update.config_list');
// Add any new migrations we need to.
list($active_list, $install_list, $optional_list) = $config_lister->listConfig('type', $type);
$missing = array_diff($install_list, $active_list);
/* @var StorageInterface $config_reader */
$config_reader = \Drupal::service('config_update.extension_storage');
foreach ($missing as $name) {
$config = $config_reader->read($name);
$config_type = $config_lister->getTypeNameByConfigName($name);
$definition = $config_lister->getType($config_type);
$id_key = $definition->getKey('id');
if ($config_revert->import($config_type, $config[$id_key])) {
drush_log(dt('Imported @config', ['@config' => $name]), LogLevel::ALERT);
}
}
}