user_cancel_entity_queue-1.0.x-dev/user_cancel_entity_queue.module
user_cancel_entity_queue.module
<?php
use Drupal\user\UserInterface;
/**
* @file
* Primary module hooks for User cancel entity queue module.
*/
/**
* Implements hook_user_cancel_methods_alter().
*/
function user_cancel_entity_queue_user_cancel_methods_alter(&$methods) {
$plugin_definitions = \Drupal::service('plugin.manager.user_cancel_entity_queue_method')->getDefinitions();
foreach ($plugin_definitions as $name => $plugin_definition) {
$methods[$plugin_definition['id']] = [
'title' => $plugin_definition['label'],
'description' => $plugin_definition['description'],
];
}
}
/**
* Implements hook_user_cancel().
*
* @throws \Drupal\Core\Entity\EntityStorageException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
function user_cancel_entity_queue_user_cancel($edit, UserInterface $account, $method) {
$batch_size = 10;
$logger = \Drupal::logger('user_cancel_entity_queue');
$config = \Drupal::config('user_cancel_entity_queue.settings');
// Make sure the user cancellation is handled by any of the queue plugins.
$plugin_definitions = \Drupal::service('plugin.manager.user_cancel_entity_queue_method')->getDefinitions();
if (!array_key_exists($method, $plugin_definitions)) {
// If no queue plugin is responsible for this cancellation method, do nothing.
return null;
}
// For each entity type fill up the queue.
foreach ($config->get('enabled_entity_types') as $entity_type_id) {
// Get definition of entity_type.
/** @var \Drupal\Core\Entity\EntityTypeInterface $definition */
$definition = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
// Load reassignment user setting.
$reassignment_uid = $config->get('reassignment_uid') ?: 0;
// Entity owner key is defined by EntityOwnerTrait.
if ($owner_key = $definition->getKey('owner')) {
// Pre-fill $entity_ids so it is not empty.
$entity_ids = [1];
// Fill queue with stacks of 10 ($batch_size) entities until none are left.
for ($i = 0; !empty($entity_ids); $i++) {
$query = \Drupal::entityTypeManager()
->getStorage($entity_type_id)
->getQuery();
$query->condition($owner_key, $account->id());
// Don't care for access, all content of this user should be queried.
$query->accessCheck(FALSE);
$query->range($i * $batch_size, $batch_size);
$entity_ids = $query->execute();
if (!empty($entity_ids)) {
\Drupal::queue('user_cancel_entity_queue')->createItem([
'entity_type_id' => $entity_type_id,
'entity_ids' => $entity_ids,
'method' => $method,
'reassignment_uid' => $reassignment_uid,
]);
}
}
}
$logger->notice('Entities of type %type from user: %name %email were added to the %method queue.', [
'%type' => $entity_type_id,
'%name' => $account->getAccountName(),
'%email' => '<' . $account->getEmail() . '>',
'%method' => $method
]);
}
// Finally add deletion of the user itself to the queue.
\Drupal::queue('user_cancel_entity_queue')->createItem([
'entity_type_id' => 'user',
'entity_ids' => [$account->id()],
'method' => 'user_cancel_entity_queue_delete',
'reassignment_uid' => 0,
]);
}
