You are here

function workflow_state_formatter in Workflow 8

Same name and namespace in other branches
  1. 7.2 workflow.module \workflow_state_formatter()

Creates a form element to show the current value of a Workflow state.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity this field is on.

string $field_name: The field_name.

string $current_sid: The current State Id.

Return value

array Form element, resembling the formatter of List module. If state 0 is given, return an empty form element.

2 calls to workflow_state_formatter()
WorkflowDefaultFormatter::viewElements in src/Plugin/Field/FieldFormatter/WorkflowDefaultFormatter.php
N.B. A large part of this function is taken from CommentDefaultFormatter.
WorkflowTransitionElement::transitionElement in src/Element/WorkflowTransitionElement.php
Generate an element.

File

./workflow.field.inc, line 95
Defines a Workflow field, widget and formatter. (copied from list field).

Code

function workflow_state_formatter(EntityInterface $entity, $field_name, $current_sid = '') {
  $element = [];
  if (!$current_sid) {
    $current_sid = workflow_node_current_state($entity, $field_name);
  }

  // If user creates a node, and only 1 option is available, the formatter
  // is shown with key, not value, because creation state does not count.
  // In this case, hide the formatter.

  /** @var \Drupal\workflow\Entity\WorkflowState $state */
  $state = WorkflowState::load($current_sid);
  if ($state && $state
    ->isCreationState()) {
    return $element;
  }

  // Clone the entity and restore old value, in case you want to show an
  // executed transition.
  if ($entity->{$field_name}->value != $current_sid) {
    $entity = clone $entity;
    $entity->{$field_name}->value = $current_sid;
  }

  // Generate a renderable array for the field. Use default language determination ($langcode = NULL).
  // First, add the 'current value' formatter for this field.
  // $list_display = $instance['display']['default'];
  $list_display['type'] = 'list_default';
  $element = $entity->{$field_name}
    ->view($list_display);

  // @todo D8: Make weight better (even better: hook_field_extra_fields).
  // Make sure the current value is before the form. (which has weight = 0.005)
  // $element['#weight'] = 0;
  return $element;
}