You are here

function autosave_restore in Autosave 7.2

Menu callback; AHAH return the form, repopulated with autosaved data.

@global type $user

Parameters

string $form_id: The form_id of the form to reload.

int $timestamp: The timestamp at which the autosaved form was saved. This is used to differentiate between different people mucking with the same form.

1 string reference to 'autosave_restore'
autosave_menu in ./autosave.module
Implements hook_menu().

File

./autosave.module, line 277
Does background saves of node being edited.

Code

function autosave_restore($form_id, $timestamp) {
  global $user;

  // Fetch the saved form, if any.
  $record = autosave_get_autosaved_form($form_id, $timestamp, $user->uid);
  $commands = array();
  if ($record) {
    $form_state = array();

    // We need to extract and reuse any additional page arguments that the
    // original form may have.  That's especially true for, say, a node form,
    // which needs the node object passed in as well.
    $menu_item = autosave_menu_get_item($record->path);
    if ($menu_item['include_file']) {
      require_once DRUPAL_ROOT . '/' . $menu_item['include_file'];
      $form_state['build_info']['files'][] = $menu_item['include_file'];
    }
    $form_state['input'] = unserialize($record->serialized);

    // Restore form arguments.
    if (!empty($record->args)) {
      $args = unserialize($record->args);
      $form_state['build_info']['args'] = $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.
    $form_state['rebuild'] = TRUE;

    // Stop recording of form error messages
    $form_state['triggering_element']['#limit_validation_errors'] = array();

    // When restoring we will need to know the form token so that the user can
    // be validated.
    $form = drupal_build_form($form_id, $form_state);

    // Because the form will by default submit back to this URL, we need to
    // tell it to actually submit back to where it would have submitted to
    // originally.
    $form['#action'] = url($record->path);
    $form['autosave_form_path']['#value'] = $record->path;

    // We don't want to change the HTML ID of the form, because we're replacing
    // it in-place.  Drupal wants to give this a suffix for some reason.
    $form['#id'] = str_replace("_", "-", $form_id);
    $commands[] = ajax_command_replace('#' . $form['#id'], drupal_render($form));
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
}