--- workflow.module	Thu Jan 01 16:09:16 2009
+++ workflow.module	Thu Mar 12 20:18:23 2009
@@ -1465,21 +1466,56 @@
 
       if ($row = db_fetch_object($result)) {
         $account = user_load(array('uid' => $row->uid));
+      }
+
+      if (isset($node->workflow) && !isset($node->workflow_stamp)) {
+        // The node is being submitted but the form data has not been saved to the database yet,
+        // so we set the token values from the workflow form fields.
+        $sid = $node->workflow;
+        $old_sid = $row->sid ? $row->sid : _workflow_creation_state($wid);
+        $date = time();
+        $user_name = $node->uid ? $node->name : variable_get('anonymous', 'Anonymous');
+        $uid = $node->uid;
+        $mail = $node->uid ? $node->user_mail : '';
+        $comment = $node->workflow_comment;
+      }
+      else if (!isset($node->workflow) && empty($row->sid)) {
+        // If the state is not specified and the node has no workflow history,
+        // the node is being inserted and will soon be transitioned to the first valid state.
+        // We find this state using the same logic as workflow_nodeapi.
+        $choices = workflow_field_choices($node);
+        $keys = array_keys($choices);
+        $sid = array_shift($keys);
+        $old_sid = _workflow_creation_state($wid);
+        $date = time();
+        $user_name = $node->uid ? $node->name : variable_get('anonymous', 'Anonymous');
+        $uid = $node->uid;
+        $mail = $node->uid ? $node->user_mail : '';
+        $comment = $node->workflow_comment;
+      }
+      else {
+        //Default to the most recent transition data in the workflow history table
+        $sid = $row->sid;
+        $old_sid = $row->old_sid;
+        $date = $row->stamp;
+        $user_name = $account->uid ? $account->name : variable_get('anonymous', 'Anonymous');
+        $uid = $account->uid;
+        $mail = $account->uid ? $account->mail : '';
         $comment = $row->comment;
       }
 
-      $values['workflow-current-state-name']                =  $states[$row->sid];
-      $values['workflow-old-state-name']                    =  $states[$row->old_sid];
+      $values['workflow-current-state-name']                =  $states[$sid];
+      $values['workflow-old-state-name']                    =  $states[$old_sid];
 
-      $values['workflow-current-state-date-iso']            =  date('Ymdhis',$row->stamp);
-      $values['workflow-current-state-date-tstamp']         =  $row->stamp;
-      $values['workflow-current-state-date-formatted']      =  date('M d, Y h:i:s', $row->stamp);
+      $values['workflow-current-state-date-iso']            =  date('Ymdhis', $date);
+      $values['workflow-current-state-date-tstamp']         =  $date;
+      $values['workflow-current-state-date-formatted']      =  date('M d, Y h:i:s', $date);

-      $values['workflow-current-state-updating-user-name']  = $account->uid ? $account->name : variable_get('anonymous', 'Anonymous');
-      $values['workflow-current-state-updating-user-uid']   = $account->uid;
-      $values['workflow-current-state-updating-user-mail']  = $account->uid ? $account->mail : '';
+      $values['workflow-current-state-updating-user-name']  = $user_name;
+      $values['workflow-current-state-updating-user-uid']   = $uid;
+      $values['workflow-current-state-updating-user-mail']  = $mail;
 
-      $values['workflow-current-state-log-entry']           = $row->comment;
+      $values['workflow-current-state-log-entry']           = $comment;
       break;
   }
 
