-
Notifications
You must be signed in to change notification settings - Fork 0
/
og.install
95 lines (81 loc) · 3.11 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* @file
* Install, update, and uninstall functions for the Organic groups module.
*/
declare(strict_types = 1);
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* 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(new TranslatableMarkup('Group bundle ID'))
->setDescription(new TranslatableMarkup('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 new TranslatableMarkup('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 = new TranslatableMarkup('Processed @current of @total memberships (@percentage% complete)', [
'@current' => $sandbox['current'],
'@total' => $sandbox['total'],
'@percentage' => number_format($sandbox['#finished'] * 100, 2),
]);
return $message;
}
/**
* Add uuid field to OgMembership.
*/
function og_update_8002() {
$manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $manager->getEntityType('og_membership');
$entity_keys = $entity_type->getKeys();
$entity_keys['uuid'] = 'uuid';
$entity_type->set('entity_keys', $entity_keys);
$manager->updateEntityType($entity_type);
$manager->updateFieldStorageDefinition($manager->getFieldStorageDefinition('uuid', 'og_membership'));
}