You are here

function workflow_state_formatter in Workflow 7.2

Same name and namespace in other branches
  1. 8 workflow.field.inc \workflow_state_formatter()

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

@params Like a normal Field API function.

Parameters

int $default_value: Extra param for performance and edge cases.

Return value

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

3 calls to workflow_state_formatter()
workflowfield_field_formatter_view in workflow_field/workflowfield.formatter.inc
Implements hook_field_formatter_view().
workflownode_node_view in workflow_node/workflownode.module
Implements hook_node_view().
WorkflowTransitionForm::buildForm in includes/Form/WorkflowTransitionForm.php
_state

File

./workflow.module, line 445
Support workflows made up of arbitrary states.

Code

function workflow_state_formatter($entity_type, $entity, $field = array(), $instance = array(), $default_value = NULL) {
  $list_element = array();
  $field_name = isset($field['field_name']) ? $field['field_name'] : '';
  $current_sid = workflow_node_current_state($entity, $entity_type, $field_name);
  if (!$current_sid && !$default_value) {
    $list_element = array();
  }
  elseif ($field_name) {

    // This is a Workflow Field workflow. Use the Field API field view.
    $field_name = $field['field_name'];

    // Add the 'current value' formatter for this field.
    $list_display = $instance['display']['default'];
    $list_display['type'] = 'list_default';

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

    // Generate a renderable array for the field. Use default language determination ($langcode = NULL).
    $list_element = field_view_field($entity_type, $entity, $field_name, $list_display);

    // Make sure the current value is before the form. (which has weight = 0.005)
    $list_element['#weight'] = 0;
  }
  else {

    // This is a Workflow Node workflow.
    $current_sid = $default_value == NULL ? $current_sid : $default_value;
    $current_state = workflow_state_load_single($current_sid);
    $args = array(
      'state' => $current_state ? workflow_get_sid_label($current_sid) : 'unknown state',
      'state_system_name' => $current_state ? $current_state
        ->getName() : 'unknown state',
      'sid' => $current_sid,
    );
    $list_element = array(
      '#type' => 'item',
      // '#title' => t('Current state'),
      '#markup' => theme('workflow_current_state', $args),
    );
  }
  return $list_element;
}