diff -u b/autosave.module b/autosave.module --- b/autosave.module +++ b/autosave.module @@ -151,51 +151,58 @@ $formid = trim($formid); } - // The form is being submitted. Let's remove the autosaved form. - if (!empty($_POST['autosave_form_path'])) { + if (($autosave_node_form || in_array($form_id, $form_ids))) { + // Remove the autosaved form when submitting it. array_unshift($form['#submit'], 'autosave_remove_autosaved_form_submit'); - } - - if (($autosave_node_form || in_array($form_id, $form_ids)) && empty($_POST['autosave_form_path'])) { - $request_path = request_path(); - // We store the drupal system paths, not the aliases. - $path = drupal_lookup_path('source', $request_path); - // For already system paths $path is FALSE. - if (!$path) { - $path = $request_path; + if (empty($_POST['autosave_form_path'])) { + $request_path = request_path(); + // We store the drupal system paths, not the aliases. + $path = drupal_lookup_path('source', $request_path); + // For already system paths $path is FALSE. + if (!$path) { + $path = $request_path; + } + // Make a note in the form of what the original path is, since when submitting + // the autosaved form to our own callback it will not be the same. + $form['autosave_form_path'] = array( + '#type' => 'hidden', + '#value' => $path, + ); + + // Store form arguments + if (!empty($form_state['build_info']['args'])) { + $form['autosave_form_args'] = array( + '#type' => 'hidden', + '#value' => serialize($form_state['build_info']['args']), + ); + } + + $settings['autosave']['formid'] = $form_id; + $settings['autosave']['url'] = url('autosave/handler'); + $settings['autosave']['period'] = variable_get('autosave_period', 10); + $settings['autosave']['q'] = $path; + $settings['autosave']['hidden'] = variable_get('autosave_hidden', 0); + $settings['autosave']['theme'] = $GLOBALS['theme']; + + // If an autosaved version of the form exists, let the user know so that + // he can restore it if desired. + $timestamp = db_query("SELECT timestamp FROM {autosaved_forms} WHERE form_id = :form_id AND path = :path AND uid = :uid", array( + ':form_id' => $form_id, + ':path' => $path, + ':uid' => $GLOBALS['user']->uid, + ))->fetchField(); + $settings['autosave']['savedTimestamp'] = $timestamp ? $timestamp : 0; + $settings['autosave']['savedDate'] = $timestamp ? format_date($timestamp) : 0; + $settings['autosave']['formToken'] = drupal_get_token($form_id); + + $form['#id'] = str_replace("_", "-", $form_id); + $form['autosave_form_path']['#attached']['library'][] = array('autosave', 'autosave'); + $form['autosave_form_path']['#attached']['js'][] = array( + 'data' => $settings, + 'type' => 'setting', + 'scope' => JS_DEFAULT, + ); } - // Make a note in the form of what the original path is, since when submitting - // the autosaved form to our own callback it will not be the same. - $form['autosave_form_path'] = array( - '#type' => 'hidden', - '#value' => $path, - ); - - $settings['autosave']['formid'] = $form_id; - $settings['autosave']['url'] = url('autosave/handler'); - $settings['autosave']['period'] = variable_get('autosave_period', 10); - $settings['autosave']['q'] = $path; - $settings['autosave']['hidden'] = variable_get('autosave_hidden', 0); - $settings['autosave']['theme'] = $GLOBALS['theme']; - - // If an autosaved version of the form exists, let the user know so that - // he can restore it if desired. - $timestamp = db_query("SELECT timestamp FROM {autosaved_forms} WHERE form_id = :form_id AND path = :path AND uid = :uid", array( - ':form_id' => $form_id, - ':path' => $path, - ':uid' => $GLOBALS['user']->uid, - ))->fetchField(); - $settings['autosave']['savedTimestamp'] = $timestamp ? $timestamp : 0; - $settings['autosave']['savedDate'] = $timestamp ? format_date($timestamp) : 0; - $settings['autosave']['formToken'] = drupal_get_token($form_id); - - $form['#id'] = str_replace("_", "-", $form_id); - $form['autosave_form_path']['#attached']['library'][] = array('autosave', 'autosave'); - $form['autosave_form_path']['#attached']['js'][] = array( - 'data' => $settings, - 'type' => 'setting', - 'scope' => JS_DEFAULT, - ); } } @@ -237,17 +244,10 @@ } $form_state['input'] = unserialize($record->serialized); - // The node_form() form builder expects the first argument to be a full node - // object, even when adding a new node. The node/add/[type] menu item only - // has a string as its argument, instead of a full object, so we convert it - // here for the form. - $build_info_args = $menu_item['page_arguments']; - if (is_string($build_info_args[0])) { - $node = (object) array('type' => $build_info_args[0]); - node_object_prepare($node); - $build_info_args[0] = $node; + // Restore form arguments. + if (!empty($form_state['input']['autosave_form_args'])) { + $form_state['build_info']['args'] = unserialize($form_state['input']['autosave_form_args']); } - $form_state['build_info']['args'] = $build_info_args; // Disable the "this form has already been submitted" nonsense by making // Drupal think the form is being rebuilt as part of a multi-step form.