You are here

function user_user_role_delete in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/user/user.module \user_user_role_delete()

Implements hook_ENTITY_TYPE_delete() for user_role entities.

File

core/modules/user/user.module, line 881
Enables the user registration and login system.

Code

function user_user_role_delete(RoleInterface $role) {

  // Delete role references for all users.
  $user_storage = \Drupal::entityTypeManager()
    ->getStorage('user');
  $user_storage
    ->deleteRoleReferences([
    $role
      ->id(),
  ]);

  // Ignore the authenticated and anonymous roles or the role is being synced.
  if (in_array($role
    ->id(), [
    RoleInterface::AUTHENTICATED_ID,
    RoleInterface::ANONYMOUS_ID,
  ]) || $role
    ->isSyncing()) {
    return;
  }
  $actions = Action::loadMultiple([
    'user_add_role_action.' . $role
      ->id(),
    'user_remove_role_action.' . $role
      ->id(),
  ]);
  foreach ($actions as $action) {
    $action
      ->delete();
  }
}