function lightning_scheduler_cron in Lightning Workflow 8
Same name and namespace in other branches
- 8.3 modules/lightning_scheduler/lightning_scheduler.module \lightning_scheduler_cron()
- 8.2 modules/lightning_scheduler/lightning_scheduler.module \lightning_scheduler_cron()
Implements hook_cron().
File
- modules/
lightning_scheduler/ lightning_scheduler.module, line 54
Code
function lightning_scheduler_cron() {
$filter = function (array $fields) {
return isset($fields['scheduled_publication']) && isset($fields['scheduled_moderation_state']);
};
$field_map = array_filter(\Drupal::service('entity_field.manager')
->getFieldMap(), $filter);
$now = (new DateTime('now', 'UTC'))
->format('c');
/** @var \Drupal\content_moderation\ModerationInformationInterface $moderation */
$moderation = \Drupal::service('content_moderation.moderation_information');
foreach ($field_map as $entity_type_id => $fields) {
$result = \Drupal::entityTypeManager()
->getStorage($entity_type_id)
->getQuery()
->condition('scheduled_publication', $now, '<=')
->execute();
foreach ($result as $entity_id) {
$entity = $moderation
->getLatestRevision($entity_type_id, $entity_id);
$entity->moderation_state->value = $entity->scheduled_moderation_state->value;
$entity->scheduled_moderation_state->value = NULL;
$entity->scheduled_publication->value = NULL;
$entity
->save();
}
}
}