user_delete_reassign-8.x-1.x-dev/user_delete_reassign.module
user_delete_reassign.module
<?php
/**
* @file
* Contains user_delete_reassign.module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\user\Entity\User;
/**
* Implements hook_help().
*/
function user_delete_reassign_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the user_delete_reassign module.
case 'help.page.user_delete_reassign':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Alters the user deletion form allowing users to reassign their content to some other existing users.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_user_cancel_methods_alter().
*/
function user_delete_reassign_user_cancel_methods_alter(&$methods) {
$account = \Drupal::currentUser();
$user_delete_reassign = [
'title' => t('Disable the account and make its content belong to another user.'),
'description' => t('All the content will be assigned to another user.'),
'access' => $account
->hasPermission('access delete and reassign user deletion'),
];
$delete_title_with_extra_warning = [
'#type' => 'html_tag',
'#tag' => 'span',
'#attributes' => ['class' => 'color-warning'],
'#value' => $methods['user_cancel_delete']['title'] . ' ' . t('USE WITH CARE, THIS COULD LEAD TO MISSING CONTENTS!'),
];
$methods['user_cancel_delete']['title'] = \Drupal::service('renderer')->render($delete_title_with_extra_warning);
$methods = array_slice($methods, 0, 2, TRUE) +
['user_delete_reassign' => $user_delete_reassign] +
array_slice($methods, 2, NULL, TRUE);
}
/**
* Implements hook_form_alter().
*/
function user_delete_reassign_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$allowed_roles = \Drupal::config('user_delete_reassign.settings')->get('role_filter');
$forms = ["user_multiple_cancel_confirm", "user_cancel_form"];
if (in_array($form_id, $forms)) {
$form['user_cancel_reassign_to'] = [
'#type' => 'entity_autocomplete',
'#title' => t("User to assign this user's content to"),
'#target_type' => 'user',
'#selection_handler' => 'default:user_advanced',
'#selection_settings' => [
'include_anonymous' => FALSE,
'exclude_users' => $form_id == 'user_cancel_form' ? [$form['uid']['#value']] : array_keys($form['account']['names']['#items']),
'filter' => [
'type' => 'role',
'role' => $allowed_roles,
],
],
'#states' => [
'visible' => [
'[name="user_cancel_method"]' => [
'value' => 'user_delete_reassign',
],
],
'required' => [
'[name="user_cancel_method"]' => [
'value' => 'user_delete_reassign',
],
],
],
// Validation is done in static::validateConfigurationForm().
'#size' => '60',
'#maxlength' => '60',
"#description" => t("Select a user to reassign content to."),
];
// Rearrange things in the form: user_cancel_reassign_to must be in between
// other fields.
$form['account']['#weight'] = 0;
$form['user_cancel_method']['#weight'] = 10;
$form['user_cancel_reassign_to']['#weight'] = 20;
$form['user_cancel_confirm']['#weight'] = 30;
$form['description']['#weight'] = 40;
// Make this option default.
$form['user_cancel_method']['#default_value'] = "user_delete_reassign";
}
}
/**
* Implements hook_user_cancel().
*/
function user_delete_reassign_user_cancel($edit, $account, $method) {
switch ($method) {
case 'user_delete_reassign':
if (!isset($edit['user_cancel_reassign_to'])) {
$message = t('Please select a user to reassign the content to.');
\Drupal::messenger()->addError($message, TRUE);
return FALSE;
}
elseif ($account->id() === $edit['user_cancel_reassign_to']) {
$message = t('Can not reassign content to same user.');
\Drupal::messenger()->addError($message, TRUE);
return FALSE;
}
$new_user = User::load($edit['user_cancel_reassign_to']);
Drupal::moduleHandler()->loadInclude('node', 'inc', 'node.admin');
$nodes = \Drupal::entityQuery('node')
->condition('uid', $account->id())
->execute();
node_mass_update($nodes, ['uid' => $new_user->id()], NULL, TRUE);
// Assign old revisions to the same user.
\Drupal::database()->update('node_field_revision')
->fields(['uid' => $new_user->id()])
->condition('uid', $account->id())
->execute();
$message = t('@count nodes have been assigned to @user.', [
'@count' => count($nodes),
'@user' => $new_user->getDisplayName(),
]);
\Drupal::messenger()->addStatus($message, TRUE);
break;
}
}
