You are here

public function WorkflowTransition::save in Workflow 8

Saves the entity.

Mostly, you'd better use WorkflowTransitionInterface::execute().

Overrides EntityBase::save

2 calls to WorkflowTransition::save()
WorkflowScheduledTransition::save in src/Entity/WorkflowScheduledTransition.php
Saves a scheduled transition. If the transition is executed, save in history.
WorkflowTransition::execute in src/Entity/WorkflowTransition.php
Execute a transition (change state of an entity).
1 method overrides WorkflowTransition::save()
WorkflowScheduledTransition::save in src/Entity/WorkflowScheduledTransition.php
Saves a scheduled transition. If the transition is executed, save in history.

File

src/Entity/WorkflowTransition.php, line 220

Class

WorkflowTransition
Implements an actual, executed, Transition.

Namespace

Drupal\workflow\Entity

Code

public function save() {

  // Set Target Entity, to be used by Rules.

  /** @var \Drupal\workflow\Entity\WorkflowTransitionInterface $reference */
  $reference = $this
    ->get('entity_id')
    ->first();
  if ($reference) {
    $reference
      ->set('entity', $this
      ->getTargetEntity());
  }

  // Avoid custom actions for subclass WorkflowScheduledTransition.
  if ($this
    ->isScheduled()) {
    return parent::save();
  }
  if ($this
    ->getEntityTypeId() != 'workflow_transition') {
    return parent::save();
  }
  $transition = $this;
  $entity_type = $transition
    ->getTargetEntityTypeId();
  $entity_id = $transition
    ->getTargetEntityId();
  $field_name = $transition
    ->getFieldName();

  // Remove any scheduled state transitions.
  foreach (WorkflowScheduledTransition::loadMultipleByProperties($entity_type, [
    $entity_id,
  ], [], $field_name) as $scheduled_transition) {

    /** @var \Drupal\workflow\Entity\WorkflowTransitionInterface $scheduled_transition */
    $scheduled_transition
      ->delete();
  }

  // Check for no transition.
  if ($this
    ->isEmpty()) {
    return SAVED_UPDATED;
  }

  // Update the transition, if already exists.
  if ($this
    ->id()) {
    return parent::save();
  }

  // Insert the transition. Make sure it hasn't already been inserted.
  // @todo Allow a scheduled transition per revision.
  // @todo Allow a state per language version (langcode).
  $found_transition = self::loadByProperties($entity_type, $entity_id, [], $field_name);
  if ($found_transition && $found_transition
    ->getTimestamp() == \Drupal::time()
    ->getRequestTime() && $found_transition
    ->getToSid() == $this
    ->getToSid()) {
    return SAVED_UPDATED;
  }
  return parent::save();
}