You are here

function opigno_ilt_entity_delete in Opigno Instructor-led Trainings 8

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

Implements hook_entity_delete().

File

./opigno_ilt.module, line 320
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();
        }
      }
    }
  }
}