function UserHooks::userRoleDelete

Same name and namespace in other branches
  1. 11.x core/modules/user/src/Hook/UserHooks.php \Drupal\user\Hook\UserHooks::userRoleDelete()

Implements hook_ENTITY_TYPE_delete() for user_role entities.

Attributes

#[Hook('user_role_delete')]

File

core/modules/user/src/Hook/UserHooks.php, line 289

Class

UserHooks
Hook implementations for user.

Namespace

Drupal\user\Hook

Code

public function userRoleDelete(RoleInterface $role) : void {
  // 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();
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.