You are here

public function TransitionManager::process in Lightning Workflow 8.2

Same name and namespace in other branches
  1. 8.3 modules/lightning_scheduler/src/TransitionManager.php \Drupal\lightning_scheduler\TransitionManager::process()

Executes all scheduled transitions for a particular entity type.

Parameters

string $entity_type_id: The entity type ID.

DrupalDateTime $now: The time that processing began.

File

modules/lightning_scheduler/src/TransitionManager.php, line 170

Class

TransitionManager

Namespace

Drupal\lightning_scheduler

Code

public function process($entity_type_id, DrupalDateTime $now) {

  /** @var ContentEntityInterface $entity */
  foreach ($this
    ->getTransitionable($entity_type_id, $now) as $entity) {
    $error_context = [
      'entity_type' => (string) $entity
        ->getEntityType()
        ->getSingularLabel(),
      'entity' => $entity
        ->label(),
    ];
    $workflow = $this->moderationInformation
      ->getWorkflowForEntity($entity);

    // If the entity hasn't got a workflow, what are we doing here?
    if (empty($workflow)) {
      $message = $this
        ->t('Could not execute scheduled transition(s) for {entity_type} "{entity}" because no workflow is assigned to it.');
      $this->logger
        ->error($message, $error_context);
      continue;
    }
    $transition_set = new TransitionSet($entity
      ->get('scheduled_transition_date'), $entity
      ->get('scheduled_transition_state'));
    $to_state = $transition_set
      ->getExpectedState($now);

    // If no workflow state is targeted, there's nothing to transition to.
    if (empty($to_state)) {
      continue;
    }
    $from_state = $entity->moderation_state->value;
    $plugin = $workflow
      ->getTypePlugin();
    if ($plugin
      ->hasTransitionFromStateToState($from_state, $to_state)) {
      $entity
        ->set('moderation_state', $to_state);
    }
    else {
      $error_context += [
        'from_state' => $plugin
          ->getState($from_state)
          ->label(),
        'to_state' => $plugin
          ->getState($to_state)
          ->label(),
        'workflow' => $workflow
          ->label(),
      ];
      $message = $this
        ->t('Could not transition {entity_type} "{entity}" from {from_state} to {to_state} because no such transition exists in the "{workflow}" workflow.');
      $this->logger
        ->warning($message, $error_context);
    }
    $transition_set
      ->trim($now);
    $entity
      ->save();
  }
}