You are here

public static function WorkflowManager::executeScheduledTransitionsBetween in Workflow 8

Given a time frame, execute all scheduled transitions.

Implements hook_cron().

Parameters

int $start:

int $end:

Overrides WorkflowManagerInterface::executeScheduledTransitionsBetween

1 call to WorkflowManager::executeScheduledTransitionsBetween()
workflow_cron in ./workflow.module
Implements hook_cron().

File

src/Entity/WorkflowManager.php, line 89

Class

WorkflowManager
Manages entity type plugin definitions.

Namespace

Drupal\workflow\Entity

Code

public static function executeScheduledTransitionsBetween($start = 0, $end = 0) {
  $clear_cache = FALSE;

  // If the time now is greater than the time to execute a transition, do it.
  foreach (WorkflowScheduledTransition::loadBetween($start, $end) as $scheduled_transition) {
    $entity = $scheduled_transition
      ->getTargetEntity();

    // Make sure transition is still valid: the entity must still be in
    // the state it was in, when the transition was scheduled.
    if (!$entity) {
      continue;
    }
    $field_name = $scheduled_transition
      ->getFieldName();
    $from_sid = $scheduled_transition
      ->getFromSid();
    $current_sid = workflow_node_current_state($entity, $field_name);
    if (!$current_sid || $current_sid != $from_sid) {

      // Entity is not in the same state it was when the transition
      // was scheduled. Defer to the entity's current state and
      // abandon the scheduled transition.
      $message = t('Scheduled Transition is discarded, since Entity has state ID %sid1, instead of expected ID %sid2.');
      $scheduled_transition
        ->logError($message, 'error', $current_sid, $from_sid);
      $scheduled_transition
        ->delete();
      continue;
    }

    // If user didn't give a comment, create one.
    $comment = $scheduled_transition
      ->getComment();
    if (empty($comment)) {
      $scheduled_transition
        ->addDefaultComment();
    }

    // Do transition. Force it because user who scheduled was checked.
    // The scheduled transition is not scheduled anymore, and is also deleted from DB.
    // A watchdog message is created with the result.
    $scheduled_transition
      ->schedule(FALSE);
    $scheduled_transition
      ->executeAndUpdateEntity(TRUE);
    if (!$field_name) {
      $clear_cache = TRUE;
    }
  }
  if ($clear_cache) {

    // Clear the cache so that if the transition resulted in a entity
    // being published, the anonymous user can see it.
    Cache::invalidateTags([
      'rendered',
    ]);
  }
}