You are here

function autosave_form_alter in Autosave 6.2

Same name and namespace in other branches
  1. 5.3 autosave.module \autosave_form_alter()
  2. 5 autosave.module \autosave_form_alter()
  3. 5.2 autosave.module \autosave_form_alter()
  4. 6 autosave.module \autosave_form_alter()
  5. 7.2 autosave.module \autosave_form_alter()
  6. 7 autosave.module \autosave_form_alter()

Implementation of hook_form_alter().

File

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

Code

function autosave_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  $path = $_GET['q'];
  if (stristr($form_id, '_node_form') && arg(0) != 'admin') {

    // check if this content_type has the autosave function enabled and make sure it's a node edit or add form
    if (variable_get('autosave_' . $form['type']['#value'], 0)) {
      drupal_add_js(AUTOSAVE_PATH . '/autosave.js');
      drupal_add_js(AUTOSAVE_PATH . '/jquery.field.js');
      drupal_add_css(AUTOSAVE_PATH . '/autosave.css');

      // if WYSIWYG module is enabled; lets let JS know this
      if (module_exists('wysiwyg')) {
        $settings['autosave']['wysiwyg'] = 1;
      }
      else {
        $settings['autosave']['wysiwyg'] = 0;
      }

      // add security token
      $token = drupal_get_token($form_id);
      $settings['autosave']['url'] = url('autosave/handler/' . $token);
      $settings['autosave']['period'] = variable_get('autosave_period', 10);
      $settings['autosave']['autosave_path'] = $path;
      $settings['autosave']['hidden'] = variable_get('autosave_hidden', 0);

      // If an autosaved version of the form exists, make it available via javascript.
      if ($autosaved_form = autosave_get_autosaved_form($form_id, $path, $user->uid)) {

        //$autosaved_form_id = $form['type']['#value'] ? $form['type']['#value'] .'_node_form' : 'node_form';
        $settings['autosave'] = array_merge($settings['autosave'], array(
          'serialized' => unserialize($autosaved_form['serialized']),
          'saved_date' => format_date($autosaved_form['timestamp'], 'medium'),
        ));
      }
      drupal_add_js($settings, 'setting');
    }
  }
}