You are here

function WorkflowDefaultWidget::getTransition in Workflow 7

1 call to WorkflowDefaultWidget::getTransition()
WorkflowDefaultWidget::submit in includes/Field/WorkflowDefaultWidget.php

File

includes/Field/WorkflowDefaultWidget.php, line 375
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

function getTransition($old_sid, $new_sid, array $form_data) {
  global $user;
  $entity_type = $this->entity_type;
  $entity = $this->entity;
  $entity_id = isset($entity->nid) ? $entity->nid : entity_id($entity_type, $entity);
  $comment = isset($form_data['workflow_comment']) ? $form_data['workflow_comment'] : '';
  $field_name = !empty($this->field) ? $this->field['field_name'] : '';

  // Caveat: for the #states to work in multi-node view, the name is suffixed by unique ID.
  // We check both variants, for Node API and Field API, for backwards compatibility.
  $element_name = 'workflow_scheduled' . '-' . $entity_type . '-' . $entity_id;
  $scheduled = (isset($form_data['workflow_scheduled']) ? $form_data['workflow_scheduled'] : 0) || (isset($form_data[$element_name]) ? $form_data[$element_name] : 0);
  if (!$scheduled) {
    $stamp = REQUEST_TIME;
    $transition = new WorkflowTransition($entity_type, $entity, $field_name, $old_sid, $new_sid, $user->uid, $stamp, $comment);
  }
  else {

    // Schedule the time to change the state.
    // If $form_data is passed, use plain values; if $form is passed, use fieldset 'workflow_scheduled_date_time'.
    $schedule = isset($form_data['workflow_scheduled_date_time']) ? $form_data['workflow_scheduled_date_time'] : $form_data;
    if (!isset($schedule['workflow_scheduled_hour'])) {
      $schedule['workflow_scheduled_hour'] = '00:00';
    }
    $scheduled_date_time = $schedule['workflow_scheduled_date']['year'] . substr('0' . $schedule['workflow_scheduled_date']['month'], -2, 2) . substr('0' . $schedule['workflow_scheduled_date']['day'], -2, 2) . ' ' . $schedule['workflow_scheduled_hour'] . ' ' . $schedule['workflow_scheduled_timezone'];
    if ($stamp = strtotime($scheduled_date_time)) {
      $transition = new WorkflowScheduledTransition($this->entity_type, $this->entity, $field_name, $old_sid, $new_sid, $user->uid, $stamp, $comment);
    }
    else {
      $transition = NULL;
    }
  }
  return $transition;
}