View source
<?php
declare (strict_types=1);
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
function og_uninstall() {
\Drupal::queue('og_orphaned_group_content')
->deleteQueue();
\Drupal::queue('og_orphaned_group_content_cron')
->deleteQueue();
}
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.');
}
}
$membership_ids = $storage
->getQuery()
->range($sandbox['current'], $sandbox['batch_size'])
->sort('id')
->execute();
foreach ($storage
->loadMultiple($membership_ids) as $membership) {
$group = $membership
->getGroup();
if (!empty($group)) {
$membership
->set('entity_bundle', $group
->bundle());
$membership
->save();
}
else {
\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;
}
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'));
}