You are here

public function WorkflowTransition::setTargetEntity in Workflow 8

Sets the Entity, that is added to the Transition.

Also sets all dependent fields, that will be saved in tables {workflow_transition_*}.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The Entity ID or the Entity object, to add to the Transition.

Return value

object The Entity, that is added to the Transition.

Overrides WorkflowTransitionInterface::setTargetEntity

File

src/Entity/WorkflowTransition.php, line 654

Class

WorkflowTransition
Implements an actual, executed, Transition.

Namespace

Drupal\workflow\Entity

Code

public function setTargetEntity(EntityInterface $entity) {
  if (!$entity) {
    $this->entity_type = '';
    $this->entity_id = '';
    $this->revision_id = '';
    $this->delta = 0;

    // Only single value is supported.
    $this->langcode = Language::LANGCODE_NOT_SPECIFIED;
    return $this;
  }

  // If Transition is added via CommentForm, use the Commented Entity.
  if ($entity
    ->getEntityTypeId() == 'comment') {

    /** @var \Drupal\comment\CommentInterface $entity */
    $entity = $entity
      ->getCommentedEntity();
  }
  $this->entity = $entity;

  /** @var \Drupal\Core\Entity\RevisionableContentEntityBase $entity */
  $this->entity_type = $entity
    ->getEntityTypeId();
  $this->entity_id = $entity
    ->id();
  $this->revision_id = $entity
    ->getRevisionId();
  $this->delta = 0;

  // Only single value is supported.
  $this->langcode = $entity
    ->language()
    ->getId();
  return $this;
}