You are here

public static function WorkflowManager::getPreviousStateId in Workflow 8

Gets the previous state ID of a given entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity:

string $field_name:

Return value

string The ID of the previous state.

Overrides WorkflowManagerInterface::getPreviousStateId

3 calls to WorkflowManager::getPreviousStateId()
WorkflowManager::executeTransitionsOfEntity in src/Entity/WorkflowManager.php
Execute a single transition for the given entity.
WorkflowManager::getCurrentStateId in src/Entity/WorkflowManager.php
Gets the current state ID of a given entity.
workflow_node_previous_state in ./workflow.module
Gets the previous state ID of a given entity.

File

src/Entity/WorkflowManager.php, line 216

Class

WorkflowManager
Manages entity type plugin definitions.

Namespace

Drupal\workflow\Entity

Code

public static function getPreviousStateId(EntityInterface $entity, $field_name = '') {
  $sid = '';
  if (!$entity) {
    return $sid;
  }

  // Determine $field_name if not known, yet.
  $field_name = $field_name ? $field_name : workflow_get_field_name($entity, $field_name);
  if (!$field_name) {

    // Return the initial value.
    return $sid;
  }

  // Retrieve previous state from the original.
  if (isset($entity->original) && !empty($entity->original->{$field_name}->value)) {
    return $entity->original->{$field_name}->value;
  }

  // A node may not have a Workflow attached.
  if ($entity
    ->isNew()) {
    return self::getCreationStateId($entity, $field_name);
  }

  // @todo Read history with an explicit langcode(?).
  $langcode = '';

  // $entity->language()->getId();
  // @todo D8: #2373383 Add integration with older revisions via Revisioning module.
  $entity_type = $entity
    ->getEntityTypeId();
  $last_transition = WorkflowTransition::loadByProperties($entity_type, $entity
    ->id(), [], $field_name, $langcode, 'DESC');
  if ($last_transition) {
    $sid = $last_transition
      ->getToSid();

    // @see #2637092, #2612702
  }
  if (!$sid) {

    // No history found on an existing entity.
    $sid = self::getCreationStateId($entity, $field_name);
  }
  return $sid;
}