You are here

function opigno_ilt_entity_delete in Opigno Instructor-led Trainings 3.x

Same name and namespace in other branches
  1. 8 opigno_ilt.module \opigno_ilt_entity_delete()

Implements hook_entity_delete().

File

./opigno_ilt.module, line 322
Contains opigno_ilt.module.

Code

function opigno_ilt_entity_delete(EntityInterface $entity) {
  if ($entity
    ->bundle() == 'user') {
    try {

      // Get user Opigno ILT results ids.
      $iltr_ids = \Drupal::entityQuery('opigno_ilt_result')
        ->condition('user_id', $entity
        ->id())
        ->execute();
    } catch (\Exception $e) {
      \Drupal::logger('opigno_ilt')
        ->error($e
        ->getMessage());
      \Drupal::messenger()
        ->addMessage($e
        ->getMessage(), 'error');
    }
    if (!empty($iltr_ids)) {

      // Remove user Opigno ILT results.
      foreach ($iltr_ids as $id) {
        if ($iltr = ILTResult::load($id)) {
          $iltr
            ->delete();
        }
      }
    }
  }
}