You are here

public function WorkflowTransition::executeAndUpdateEntity in Workflow 8

Executes a transition (change state of an entity), from OUTSIDE the entity.

Use $transition->executeAndUpdateEntity() to start a State Change from outside an entity, e.g., workflow_cron(). Use $transition->execute() to start a State Change from within an entity.

A Scheduled Transition ($transition->isScheduled() == TRUE) will be un-scheduled and saved in the history table. The entity will not be updated. If $transition->isScheduled() == FALSE, the Transition will be removed from the {workflow_transition_scheduled} table (if necessary), and added to {workflow_transition_history} table. Then the entity wil be updated to reflect the new status.

@usage $to_sid = $transition->->executeAndUpdateEntity($force);

Parameters

bool $force: If set to TRUE, workflow permissions will be ignored.

Return value

string The resulting WorkflowState id.

Overrides WorkflowTransitionInterface::executeAndUpdateEntity

See also

workflow_execute_transition()

File

src/Entity/WorkflowTransition.php, line 589

Class

WorkflowTransition
Implements an actual, executed, Transition.

Namespace

Drupal\workflow\Entity

Code

public function executeAndUpdateEntity($force = FALSE) {
  $to_sid = $this
    ->getToSid();

  // Generate error and stop if transition has no new State.
  if (!$to_sid) {
    $t_args = [
      '%sid2' => $this
        ->getToState()
        ->label(),
      '%entity_label' => $this
        ->getTargetEntity()
        ->label(),
    ];
    $message = "Transition is not executed for %entity_label, since 'To' state %sid2 is invalid.";
    $this
      ->logError($message);
    $this
      ->messenger()
      ->addError($this
      ->t($message, $t_args));
    return $this
      ->getFromSid();
  }

  // Save the (scheduled) transition.
  $do_update_entity = !$this
    ->isScheduled() && !$this
    ->isExecuted();
  if ($do_update_entity) {
    $this
      ->_updateEntity();
  }
  else {

    // We create a new transition, or update an existing one.
    // Do not update the entity itself.
    // Validate transition, save in history table and delete from schedule table.
    $to_sid = $this
      ->execute($force);
  }
  return $to_sid;
}