You are here

protected function WorkflowStateActionBase::getTransitionForExecution in Workflow 8

Parameters

\Drupal\Core\Entity\EntityInterface $entity:

Return value

\Drupal\workflow\Entity\WorkflowTransitionInterface

2 calls to WorkflowStateActionBase::getTransitionForExecution()
WorkflowNodeGivenStateAction::execute in src/Plugin/Action/WorkflowNodeGivenStateAction.php
Executes the plugin.
WorkflowNodeNextStateAction::execute in src/Plugin/Action/WorkflowNodeNextStateAction.php
Executes the plugin.

File

src/Plugin/Action/WorkflowStateActionBase.php, line 63

Class

WorkflowStateActionBase
Sets an entity to a new, given state.

Namespace

Drupal\workflow\Plugin\Action

Code

protected function getTransitionForExecution(EntityInterface $entity) {
  $user = workflow_current_user();
  if (!$entity) {
    \Drupal::logger('workflow_action')
      ->notice('Unable to get current entity - entity is not defined.', []);
    return NULL;
  }

  // Get the entity type and numeric ID.
  $entity_id = $entity
    ->id();
  if (!$entity_id) {
    \Drupal::logger('workflow_action')
      ->notice('Unable to get current entity ID - entity is not yet saved.', []);
    return NULL;
  }

  // In 'after saving new content', the node is already saved. Avoid second insert.
  // @todo Clone?
  $entity
    ->enforceIsNew(FALSE);
  $config = $this->configuration;
  $field_name = workflow_get_field_name($entity, $config['field_name']);
  $current_sid = workflow_node_current_state($entity, $field_name);
  if (!$current_sid) {
    \Drupal::logger('workflow_action')
      ->notice('Unable to get current workflow state of entity %id.', [
      '%id' => $entity_id,
    ]);
    return NULL;
  }
  $to_sid = isset($config['to_sid']) ? $config['to_sid'] : '';

  // Get the Comment. Parse the $comment variables.
  $comment_string = $this->configuration['comment'];
  $comment = $this
    ->t($comment_string, [
    '%title' => $entity
      ->label(),
    // "@" and "%" will automatically run check_plain().
    '%state' => workflow_get_sid_name($to_sid),
    '%user' => $user
      ->getDisplayName(),
  ]);
  $force = $this->configuration['force'];
  $transition = WorkflowTransition::create([
    $current_sid,
    'field_name' => $field_name,
  ]);
  $transition
    ->setTargetEntity($entity);
  $transition
    ->setValues($to_sid, $user
    ->id(), \Drupal::time()
    ->getRequestTime(), $comment);
  $transition
    ->force($force);
  return $transition;
}