You are here

function workbench_access_user_delete in Workbench Access 8

Same name and namespace in other branches
  1. 7 workbench_access.module \workbench_access_user_delete()

Implements hook_ENTITY_TYPE_delete().

File

./workbench_access.module, line 210
Contains workbench_access.module.

Code

function workbench_access_user_delete($account) {
  $uid = $account
    ->id();
  $storage_handler = \Drupal::entityTypeManager()
    ->getStorage('section_association');

  // Retrieve all the section associations referencing this user.
  $section_user_associations = $storage_handler
    ->loadByProperties([
    'user_id' => $uid,
  ]);
  foreach ($section_user_associations as $section_association) {

    // For each retrived association, remove this user.
    if ($user_ids = $section_association
      ->getCurrentUserIds()) {
      $remaining_user_ids = array_diff($user_ids, [
        $uid,
      ]);
      $section_association
        ->set('user_id', $remaining_user_ids);
      $section_association
        ->save();
    }
  }
}