You are here

function cacheflush_cron_cacheflush_update in CacheFlush 8

Implements hook_ENTITY_TYPE_update().

1 call to cacheflush_cron_cacheflush_update()
cacheflush_cron_cacheflush_insert in modules/cacheflush_cron/cacheflush_cron.module
Implements hook_ENTITY_TYPE_insert().

File

modules/cacheflush_cron/cacheflush_cron.module, line 82
Cacheflush cron module.

Code

function cacheflush_cron_cacheflush_update(EntityInterface $entity) {
  $cron_job = CronJob::load('cacheflush_preset_' . $entity
    ->id());
  $has_cron_assigned = (bool) $entity->cron
    ->getValue()[0]['value'];
  if ($has_cron_assigned === TRUE) {
    if (!$cron_job) {
      $values = [
        'id' => 'cacheflush_preset_' . $entity
          ->id(),
        'callback' => 'cacheflush_cron_clear_preset',
        'module' => 'cacheflush_cron',
        'status' => TRUE,
        'title' => 'Cacheflush cron preset ' . $entity
          ->id(),
      ];
      $cron_job = CronJob::create($values);
      $cron_job
        ->save();
    }
    else {
      if ($cron_job && $cron_job
        ->status() === FALSE) {
        $cron_job
          ->setStatus(TRUE);
        $cron_job
          ->save();
      }
    }
  }
  else {
    if (!$has_cron_assigned && $cron_job && $cron_job
      ->status() === TRUE) {
      $cron_job
        ->setStatus(FALSE);
      $cron_job
        ->save();
    }
  }
}