-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathislandora_spreadsheet_ingest.module
199 lines (178 loc) · 6.7 KB
/
islandora_spreadsheet_ingest.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
<?php
/**
* @file
* General hook implementations.
*/
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\islandora_spreadsheet_ingest\RequestInterface;
/**
* Helper; add file usage for a request.
*
* @param \Drupal\islandora_spreadsheet_ingest\RequestInterface $request
* The request of which to add the usage.
*/
function _islandora_spreadsheet_ingest_add_file_usage(RequestInterface $request) : void {
$file_usage_service = \Drupal::service('file.usage');
$file_service = \Drupal::service('entity_type.manager')->getStorage('file');
$fids = $request->getSheet()['file'] ?? NULL;
$file = $fids ? $file_service->load(reset($fids)) : FALSE;
if ($file && $request->id()) {
$file_usage_service->add(
$file,
'islandora_spreadsheet_ingest',
$request->getEntityTypeId(),
$request->id()
);
}
else {
// Nothing to do.
}
}
/**
* Helper; delete file usage for a request.
*
* @param \Drupal\islandora_spreadsheet_ingest\RequestInterface $request
* The request of which to delete the usage.
*/
function _islandora_spreadsheet_ingest_delete_file_usage(RequestInterface $request) : void {
$file_usage_service = \Drupal::service('file.usage');
$file_service = \Drupal::service('entity_type.manager')->getStorage('file');
$fids = $request->getSheet()['file'];
$file = $fids ? $file_service->load(reset($fids)) : FALSE;
if ($file && $request->id()) {
$file_usage_service->delete(
$file,
'islandora_spreadsheet_ingest',
$request->getEntityTypeId(),
$request->id()
);
}
else {
// Nothing to do.
}
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function islandora_spreadsheet_ingest_isi_request_insert(RequestInterface $request) : void {
_islandora_spreadsheet_ingest_add_file_usage($request);
// If active, generate the migration group...
// XXX: Should only actually fire when importing the config... which would
// probably be broken due to referencing content (a file)... may have to
// suppress/detect related errors?
if ($request->getActive()) {
\Drupal::service('islandora_spreadsheet_ingest.migration_group_deriver')->create($request);
\Drupal::service('islandora_spreadsheet_ingest.migration_deriver')->createAll($request);
}
}
/**
* Implements hook_ENTITY_TYPE_update().
*/
function islandora_spreadsheet_ingest_isi_request_update(RequestInterface $request) : void {
_islandora_spreadsheet_ingest_delete_file_usage($request->original);
_islandora_spreadsheet_ingest_add_file_usage($request);
if ($request->getActive()) {
// (Re)derive our migration group.
\Drupal::service('islandora_spreadsheet_ingest.migration_group_deriver')->create($request);
\Drupal::service('islandora_spreadsheet_ingest.migration_deriver')->createAll($request);
}
else {
// Nuke the related migration group.
\Drupal::service('islandora_spreadsheet_ingest.migration_deriver')->deleteAll($request);
\Drupal::service('islandora_spreadsheet_ingest.migration_group_deriver')->delete($request);
}
}
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function islandora_spreadsheet_ingest_isi_request_delete(RequestInterface $request) : void {
_islandora_spreadsheet_ingest_delete_file_usage($request);
\Drupal::service('islandora_spreadsheet_ingest.migration_deriver')->deleteAll($request);
\Drupal::service('islandora_spreadsheet_ingest.migration_group_deriver')->delete($request);
}
/**
* Implements hook_entity_operation().
*/
function islandora_spreadsheet_ingest_entity_operation(EntityInterface $entity) : array {
$ops = [];
if ($entity->getEntityType()->id() == 'isi_request') {
$ops['activate'] = [
'title' => t('Process'),
'url' => $entity->toUrl('process-form'),
'weight' => 14,
];
if ($entity->getActive()) {
$ops['migration'] = [
'title' => t('Migration group'),
'url' => Url::fromRoute('entity.migration.list', [
'migration_group' => \Drupal::service('islandora_spreadsheet_ingest.migration_group_deriver')->deriveName($entity),
]),
'weight' => 16,
];
}
}
return $ops;
}
/**
* Helper for the migration access checks.
*
* @see islandora_spreadsheet_ingest_migration_group_entity_access()
* @see islandora_spreadsheet_ingest_migration_entity_access()
*/
function _islandora_spreadsheet_ingest_migration_entity_helper(EntityInterface $entity, AccountInterface $account, $uid, $tags) : AccessResultInterface {
return AccessResult::allowedIf(in_array('isi_derived_migration', $tags))
->addCacheableDependency($entity)
->andIf(
AccessResult::allowedIf($uid !== FALSE && $uid == $account->id())
->cachePerUser()
);
}
/**
* Implements hook_ENTITY_TYPE_entity_access() for migration_group entities.
*/
function islandora_spreadsheet_ingest_migration_group_entity_access(EntityInterface $entity, $operation, AccountInterface $account) : AccessResultInterface {
$shared = $entity->get('shared_configuration') ?? [];
$tags = NestedArray::getValue($shared, ['migration_tags']) ?? [];
$uid = NestedArray::getValue($shared, ['source', 'isi', 'uid']) ?? FALSE;
return _islandora_spreadsheet_ingest_migration_entity_helper($entity, $account, $uid, $tags);
}
/**
* Implements hook_ENTITY_TYPE_entity_access() for migration entities.
*/
function islandora_spreadsheet_ingest_migration_entity_access(EntityInterface $entity, $operation, AccountInterface $account) : AccessResultInterface {
$tags = $entity->get('migration_tags') ?? [];
$source = $entity->get('source');
$uid = NestedArray::getValue($source, ['isi', 'uid']) ?? FALSE;
return _islandora_spreadsheet_ingest_migration_entity_helper($entity, $account, $uid, $tags);
}
/**
* Implements hook_migration_plugins_alter().
*/
function islandora_spreadsheet_ingest_migration_plugins_alter(&$definitions) : void {
$logger = \Drupal::logger('islandora_spreadsheet_ingest');
$logger->debug('Altering...');
// XXX: The "idMap" hash is not presently passed down through the migration
// derivation process... so handle setting our couple of things indirectly.
// @see https://www.drupal.org/project/migrate_plus/issues/2944627
$tag = 'isi_manage_orphans';
foreach ($definitions as $plugin_id => &$def) {
$tags = $def['migration_tags'] ?? [];
if ($tags && in_array($tag, $tags, TRUE)) {
$logger->debug('Found tag "{tag}" in migration "{migration}."', [
'tag' => $tag,
'migration' => $plugin_id,
]);
$def['idMap'] = [
'plugin' => 'smart_sql',
'manage_orphans' => TRUE,
];
}
}
unset($def);
$logger->debug('Altered.');
}