public static function Job::postDelete

File

src/Entity/Job.php, line 263

Class

Job
Entity class for the tmgmt_job entity.

Namespace

Drupal\tmgmt\Entity

Code

public static function postDelete(EntityStorageInterface $storage, array $entities) {
  parent::postDelete($storage, $entities);
  $entity_type_manager = \Drupal::entityTypeManager();

  // Since we are deleting one or multiple jobs here we also need to delete
  // the attached job items and messages.
  $tjiids = \Drupal::entityQuery('tmgmt_job_item')
    ->accessCheck(TRUE)
    ->condition('tjid', array_keys($entities), 'IN')
    ->execute();
  if (!empty($tjiids)) {
    $job_items = $entity_type_manager
      ->getStorage('tmgmt_job_item')
      ->loadMultiple($tjiids);
    $entity_type_manager
      ->getStorage('tmgmt_job_item')
      ->delete($job_items);
  }
  $mids = \Drupal::entityQuery('tmgmt_message')
    ->accessCheck(TRUE)
    ->condition('tjid', array_keys($entities), 'IN')
    ->execute();
  if (!empty($mids)) {
    $messages = $entity_type_manager
      ->getStorage('tmgmt_message')
      ->loadMultiple($mids);
    $entity_type_manager
      ->getStorage('tmgmt_message')
      ->delete($messages);
  }
  $trids = \Drupal::entityQuery('tmgmt_remote')
    ->accessCheck(TRUE)
    ->condition('tjid', array_keys($entities), 'IN')
    ->execute();
  if (!empty($trids)) {
    $remotes = $entity_type_manager
      ->getStorage('tmgmt_remote')
      ->loadMultiple($trids);
    $entity_type_manager
      ->getStorage('tmgmt_remote')
      ->delete($remotes);
  }
}