You are here

function userprotect_user_role_delete in User protect 8

Implements hook_user_role_delete().

When an role is deleted, delete all associated protection rules as well.

File

./userprotect.module, line 192
Allows admins to protect users from being edited or cancelled.

Code

function userprotect_user_role_delete($role) {

  // Lookup all protection rules for this user.
  $entity_ids = \Drupal::entityQuery('userprotect_rule')
    ->condition('protectedEntityTypeId', 'user_role')
    ->condition('protectedEntityId', $role
    ->id())
    ->execute();

  // Delete protection rules.
  if (!empty($entity_ids)) {
    $storage_handler = \Drupal::entityTypeManager()
      ->getStorage('userprotect_rule');
    $entities = $storage_handler
      ->loadMultiple($entity_ids);
    $storage_handler
      ->delete($entities);
  }
}