-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiversion.drush.inc
177 lines (161 loc) · 6.26 KB
/
multiversion.drush.inc
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
<?php
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Psr\Log\LogLevel;
/**
* Implements of hook_drush_command().
*/
function multiversion_drush_command() {
$items = [];
$items['multiversion-uninstall'] = [
'bootstrap' => DRUSH_BOOTSTRAP_NONE,
'description' => 'Uninstall Multiversion.',
'aliases' => ['mun'],
];
$items['multiversion-enable-entity-types'] = [
'bootstrap' => DRUSH_BOOTSTRAP_NONE,
'description' => 'Enable entity types (make them multiversionable).',
'arguments' => [
'entity_types' => dt('The list of entity types, comma or space separated.'),
],
'examples' => [
'drush multiversion-enable-entity-types my_et' => 'Makes my_et entity type multiversionable.',
'drush multiversion-enable-entity-types my_et my_et2' => 'Makes my_et and my_et2 entity types multiversionable.',
'drush met my_et' => 'Makes my_et entity type multiversionable.',
'drush met my_et my_et2' => 'Makes my_et and my_et2 entity types multiversionable.',
],
'aliases' => ['met'],
];
$items['multiversion-disable-entity-types'] = [
'bootstrap' => DRUSH_BOOTSTRAP_NONE,
'description' => 'Disable entity types (make them non-multiversionable).',
'arguments' => [
'entity_types' => dt('The list of entity types, comma or space separated.'),
],
'examples' => [
'drush multiversion-disable-entity-types my_et' => 'Makes my_et entity type non-multiversionable.',
'drush multiversion-disable-entity-types my_et1 my_et2' => 'Makes my_et and my_et2 entity types non-multiversionable.',
'drush mdt my_et' => 'Makes my_et entity type non-multiversionable.',
'drush mdt my_et1 my_et2' => 'Makes my_et and my_et2 entity types non-multiversionable.',
],
'aliases' => ['mdt'],
];
return $items;
}
/**
* Implements drush_hook_COMMAND().
*/
function drush_multiversion_uninstall() {
$extension = 'multiversion';
$uninstall = TRUE;
$extension_info = drush_get_extensions();
$required = drush_drupal_required_modules($extension_info);
if (in_array($extension, $required)) {
$info = $extension_info[$extension]->info;
$explanation = !empty($info['explanation']) ? ' ' . dt('Reason: !explanation.', ['!explanation' => strip_tags($info['explanation'])]) : '';
drush_log(dt('!extension is a required extension and can\'t be uninstalled.', ['!extension' => $extension]) . $explanation, LogLevel::INFO);
$uninstall = FALSE;
}
elseif (!$extension_info[$extension]->status) {
drush_log(dt('!extension is already uninstalled.', ['!extension' => $extension]), LogLevel::INFO);
$uninstall = FALSE;
}
elseif (drush_extension_get_type($extension_info[$extension]) == 'module') {
$dependents = [];
foreach (drush_module_dependents([$extension], $extension_info) as $dependent) {
if (!in_array($dependent, $required) && ($extension_info[$dependent]->status)) {
$dependents[] = $dependent;
}
}
if (count($dependents)) {
drush_log(dt('To uninstall !extension, the following extensions must be uninstalled first: !required', ['!extension' => $extension, '!required' => implode(', ', $dependents)]), LogLevel::ERROR);
$uninstall = FALSE;
}
}
if ($uninstall) {
drush_print(dt('Multiversion will be uninstalled.'));
if(!drush_confirm(dt('Do you really want to continue?'))) {
return drush_user_abort();
}
drush_log('*** ' . dt('The uninstall process can take a few minutes, it depends by the number of entities on the site. Please be patient.'), LogLevel::WARNING);
try {
\Drupal::service('multiversion.manager')->disableEntityTypes();
// Delete workspace entities before uninstall.
$storage = \Drupal::entityTypeManager()->getStorage('workspace');
$entities = $storage->loadMultiple();
$storage->delete($entities);
drush_module_uninstall(['multiversion']);
}
catch (Exception $e) {
drush_log($e->getMessage(), LogLevel::ERROR);
}
// Inform the user of final status.
drush_log(dt('!extension was successfully uninstalled.', ['!extension' => $extension]), LogLevel::INFO);
}
}
/**
* Implements drush_hook_COMMAND().
*/
function drush_multiversion_enable_entity_types() {
$args = pm_parse_arguments(func_get_args());
if (empty($args)) {
drush_log(dt('Entity types list argument is missing.'), LogLevel::ERROR);
}
elseif ($entity_types = _drush_multiversion_get_entity_types($args)) {
if(!drush_confirm(dt('Do you really want to continue?'))) {
return drush_user_abort();
}
try {
$multiversion_settings = \Drupal::configFactory()->getEditable('multiversion.settings');
$supported_entity_types = $multiversion_settings->get('supported_entity_types') ?: [];
foreach (array_keys($entity_types) as $id) {
if (!in_array($id, $supported_entity_types)) {
$supported_entity_types[] = $id;
}
}
// Add new entity types to the supported entity types list.
$multiversion_settings
->set('supported_entity_types', $supported_entity_types)
->save();
\Drupal::service('multiversion.manager')->enableEntityTypes($entity_types);
}
catch (Exception $e) {
drush_log($e->getMessage(), LogLevel::ERROR);
}
}
}
/**
* Implements drush_hook_COMMAND().
*/
function drush_multiversion_disable_entity_types() {
$args = pm_parse_arguments(func_get_args());
if (empty($args)) {
drush_log(dt('Entity types list argument is missing.'), LogLevel::ERROR);
}
elseif ($entity_types = _drush_multiversion_get_entity_types($args)) {
if(!drush_confirm(dt('Do you really want to continue?'))) {
return drush_user_abort();
}
try {
\Drupal::service('multiversion.manager')->disableEntityTypes($entity_types);
}
catch (Exception $e) {
drush_log($e->getMessage(), LogLevel::ERROR);
}
}
}
function _drush_multiversion_get_entity_types($entity_type_ids) {
$entity_types = [];
$manager = \Drupal::entityTypeManager();
try {
foreach ($entity_type_ids as $id) {
$entity_type = $manager->getStorage($id)->getEntityType();
if ($entity_type instanceof ContentEntityTypeInterface) {
$entity_types[$id] = $entity_type;
}
}
}
catch (Exception $e) {
drush_log($e->getMessage(), LogLevel::ERROR);
}
return $entity_types;
}