cacheflush-8.x-1.x-dev/modules/cacheflush_cron/cacheflush_cron.module

modules/cacheflush_cron/cacheflush_cron.module
<?php

/**
 * @file
 * Cacheflush cron module.
 */

use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ultimate_cron\Entity\CronJob;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Link;

/**
 * Implements hook_entity_base_field_info().
 */
function cacheflush_cron_entity_base_field_info(EntityTypeInterface $entity_type) {
  if ($entity_type->id() == 'cacheflush') {
    $fields = [];
    $fields['cron'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Cron entry'))
      ->setDescription(t('Enable cron for cacheflush preset.'))
      ->setDefaultValue(0);
    return $fields;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function cacheflush_cron_form_cacheflush_add_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  _cacheflush_cron_entity_form($form, $form_state, 'add');
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function cacheflush_cron_form_cacheflush_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  _cacheflush_cron_entity_form($form, $form_state, 'edit');
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function _cacheflush_cron_entity_form(&$form, FormStateInterface $form_state, $op) {

  $entity = $form_state->getFormObject()
    ->getEntity();
  $cron_job = NULL;
  if ($op == 'edit') {
    $cron_job = CronJob::load('cacheflush_preset_' . $entity->id());
    if ($cron_job) {
      $url = Link::createFromRoute($cron_job, 'entity.ultimate_cron_job.edit_form', [
        'ultimate_cron_job' => 'cacheflush_preset_' . $entity->id(),
      ], ['attributes' => ['target' => '_blank']]);
      $url->setText('Edit');
      $renderable = $url->toRenderable();
      $link = \Drupal::service('renderer')->render($renderable);
    }
  }

  $form['cron'] = [
    '#type' => "checkbox",
    '#title' => t('Cron'),
    '#weight' => 1,
    '#default_value' => $entity->cron->getValue()[0]['value'] ? 1 : 0,
    '#description' => t('Enable cron job for this preset.') . (($cron_job && isset($link)) ? ' ' . $link : NULL),
  ];
}

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function cacheflush_cron_cacheflush_insert(EntityInterface $entity) {
  cacheflush_cron_cacheflush_update($entity);
}

/**
 * Implements hook_ENTITY_TYPE_update().
 */
function cacheflush_cron_cacheflush_update(EntityInterface $entity) {
  $cron_job = CronJob::load('cacheflush_preset_' . $entity->id());
  $has_cron_assigned = (boolean) $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();
    }
  }
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function cacheflush_cron_cacheflush_delete(EntityInterface $entity) {
  $cron_job = CronJob::load('cacheflush_preset_' . $entity->id());
  if ($cron_job) {
    $cron_job->delete();
  }
}

/**
 * Cron job callback for cacheflush.
 *
 * @param Drupal\ultimate_cron\Entity\CronJob $job
 *   CronJob entity.
 */
function cacheflush_cron_clear_preset(CronJob $job) {
  $cacheflush = \Drupal::service('cacheflush.api');
  // @Todo: if Drupal\ultimate_cron\Entity\CronJob will let it in the future in any way to send data to callback get rid of this and make it better.
  $entity = cacheflush_load(str_replace('cacheflush_preset_', '', $job->get('id')));
  $cacheflush->clearById($entity);
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc