You are here

public function WorkflowDefaultWidget::submit in Workflow 7

Same name and namespace in other branches
  1. 7.2 includes/Field/WorkflowDefaultWidget.php \WorkflowDefaultWidget::submit()

File

includes/Field/WorkflowDefaultWidget.php, line 299
Contains workflow\includes\Field\WorkflowDefaultWidget.

Class

WorkflowDefaultWidget
Plugin implementation of the 'workflow_default' widget. @todo D8: Replace "extends WorkflowD7WidgetBase" by "extends WidgetBase" or perhaps by "extends OptionsWidgetBase" from Options module.

Code

public function submit(array $form, array &$form_state, array &$items, $force = FALSE) {
  global $user;
  $entity_type = $this->entity_type;
  $entity = $this->entity;
  $entity_id = isset($entity->nid) ? $entity->nid : entity_id($entity_type, $entity);
  $field = $this->field;
  $field_name = isset($this->field['field_name']) ? $this->field['field_name'] : '';

  // Massage the items, depending on the type of widget.
  // @todo: use MassageFormValues($values, $form, $form_state).
  $old_sid = workflow_node_current_state($entity, $entity_type, $field);

  // Todo : entity support.
  $new_sid = isset($items[0]['workflow']['workflow_options']) ? $items[0]['workflow']['workflow_options'] : $items[0]['value'];
  $new_items = isset($items[0]['workflow']) ? $items[0]['workflow'] : $items;
  $transition = $this
    ->getTransition($old_sid, $new_sid, $new_items);
  if ($error = $transition
    ->isAllowed($force)) {
    drupal_set_message($error, 'error');
  }
  elseif (!$transition
    ->isScheduled()) {

    // Now the data is captured in the Transition, and before calling the Execution,
    // restore the default values for Workflow Field.
    // For instance, workflow_rules evaluates this.
    if ($field_name) {
      $items = array();
      $items[0]['value'] = $new_sid;
      $entity->{$field_name}['und'] = $items;
    }

    // It's an immediate change. Do the transition.
    // - validate option; add hook to let other modules change comment.
    // - add to history; add to watchdog
    // return the new value of the sid. (Execution may fail and return the old Sid.)
    $new_sid = $transition
      ->execute($force);

    // In case the transition is not executed, reset the old value.
    if ($field_name) {
      $items = array();
      $items[0]['value'] = $new_sid;
      $entity->{$field_name}['und'] = $items;
    }
  }
  else {

    // A scheduled transition must only be saved to the database. The entity is not changed.
    $transition
      ->save();

    // The current value is still the previous state.
    $new_sid = $old_sid;
  }

  // The entity is still to be saved, so set to a 'normal' value.
  if ($field_name) {
    $items = array();
    $items[0]['value'] = $new_sid;
    $entity->{$field_name}['und'] = $items;
  }
  return $new_sid;
}