forked from mradcliffe/og
-
Notifications
You must be signed in to change notification settings - Fork 0
/
og.install
79 lines (67 loc) · 2.5 KB
/
og.install
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
<?php
/**
* @file
* Install, update, and uninstall functions for the Organic groups module.
*/
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Implements hook_uninstall().
*/
function og_uninstall() {
\Drupal::queue('og_orphaned_group_content')->deleteQueue();
\Drupal::queue('og_orphaned_group_content_cron')->deleteQueue();
}
/**
* Add a base field to store the group bundle in memberships.
*/
function og_update_8001(&$sandbox) {
$storage = \Drupal::entityTypeManager()->getStorage('og_membership');
if (!isset($sandbox['total'])) {
$storage_definition = BaseFieldDefinition::create('string')
->setLabel(t('Group bundle ID'))
->setDescription(t('The bundle ID of the group.'));
\Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition('entity_bundle', 'og_membership', 'og', $storage_definition);
$sandbox['#finished'] = 0;
$sandbox['batch_size'] = 500;
$sandbox['current'] = 0;
$sandbox['total'] = $storage->getQuery()->count()->execute();
if (!$sandbox['total']) {
$sandbox['#finished'] = 1;
return t('No OG memberships found.');
}
}
// Update the existing memberships to include the group bundle ID.
$membership_ids = $storage->getQuery()
->range($sandbox['current'], $sandbox['batch_size'])
->sort('id')
->execute();
/** @var \Drupal\og\Entity\OgMembership $membership */
foreach ($storage->loadMultiple($membership_ids) as $membership) {
$group = $membership->getGroup();
if (!empty($group)) {
$membership->set('entity_bundle', $group->bundle());
$membership->save();
}
else {
// The membership is for a group that no longer exists. We cannot no
// longer retrieve the group bundle ID so the membership cannot be
// updated. Delete the membership since it is invalid, and inform the
// user.
\Drupal::logger('og')->warning('Deleted orphaned membership with ID @id since the group it refers to no longer exists.', [
'@id' => $membership->id(),
]);
$membership->delete();
}
}
$sandbox['current'] += $sandbox['batch_size'];
if ($sandbox['current'] >= $sandbox['total']) {
$sandbox['current'] = $sandbox['total'];
}
$sandbox['#finished'] = $sandbox['current'] / $sandbox['total'];
$message = t('Processed @current of @total memberships (@percentage% complete)', [
'@current' => $sandbox['current'],
'@total' => $sandbox['total'],
'@percentage' => number_format($sandbox['#finished'] * 100, 2),
]);
return $message;
}