You are here

public static function WorkflowManager::getCurrentStateId in Workflow 8

Gets the current state ID of a given entity.

There is no need to use a page cache. The performance is OK, and the cache gives problems when using Rules.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to check.

string $field_name: The name of the field of the entity to check. If empty, the field_name is determined on the spot. This must be avoided, since it makes having multiple workflow per entity unpredictable. The found field_name will be returned in the param.

Return value

string The ID of the current state.

Overrides WorkflowManagerInterface::getCurrentStateId

3 calls to WorkflowManager::getCurrentStateId()
Workflow::getNextSid in src/Entity/Workflow.php
Returns the next state for the current state.
WorkflowDefaultFormatter::viewElements in src/Plugin/Field/FieldFormatter/WorkflowDefaultFormatter.php
N.B. A large part of this function is taken from CommentDefaultFormatter.
workflow_node_current_state in ./workflow.module
Gets the current state ID of a given entity.

File

src/Entity/WorkflowManager.php, line 403

Class

WorkflowManager
Manages entity type plugin definitions.

Namespace

Drupal\workflow\Entity

Code

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

  // If $field_name is not known, yet, determine it.
  $field_name = $field_name ? $field_name : workflow_get_field_name($entity, $field_name);

  // If $field_name is found, get more details.
  if (!$field_name || !isset($entity->{$field_name})) {

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

  // Normal situation: get the value.
  $sid = $entity->{$field_name}->value;

  // Entity is new or in preview or there is no current state. Use previous state.
  // (E.g., content was created before adding workflow.)
  if (!$sid || !empty($entity
    ->isNew()) || !empty($entity->in_preview)) {
    $sid = self::getPreviousStateId($entity, $field_name);
  }
  return $sid;
}