You are here

function ajaxsubmit_form_alter in Javascript Tools 5

Implementation of hook_form_alter().

File

ajaxsubmit/ajaxsubmit.module, line 33
Make designated forms submit via AJAX.

Code

function ajaxsubmit_form_alter($form_id, &$form) {
  if (in_array($form_id, array_filter(variable_get('ajaxsubmit_forms', array())))) {
    $form['#ajaxsubmit'] = TRUE;
  }
  if ($form['#ajaxsubmit']) {

    // If we're using ajaxsubmit and the form's already been submitted, redirect.
    if (!empty($_POST) && !empty($_POST['ajaxsubmit']) && $_POST['form_id'] == $form_id) {

      // Redirect on successful submit, i.e., if there are no errors.
      $form['#redirect'] = 'ajaxsubmit/dispatch';
      if (is_array($form['#pre_render'])) {
        $form['#pre_render'][] = 'ajaxsubmit_dispatch';
      }
      else {
        $form['#pre_render'] = array(
          'ajaxsubmit_dispatch',
        );
      }
    }
    else {

      // In case a destination was set by a previous transaction, unset it
      // so that this one can set its own as needed.
      unset($_SESSION['ajaxsubmit_destination']);
      ajaxsubmit_load();
      $form['#attributes']['class'] .= ' ajaxsubmit';
      if ($form['#ajaxsubmit_target']) {
        $form['ajaxsubmit_target'] = array(
          '#type' => 'hidden',
          '#value' => $form['#ajaxsubmit_target'],
        );
      }
      if ($form['#ajaxsubmit_progress']) {

        // Value is the path to the progress monitoring.
        $form['ajaxsubmit_progress'] = array(
          '#type' => 'hidden',
          '#value' => url(drupal_get_path('module', 'ajaxsubmit') . '/ajaxsubmit_update'),
        );
      }
      if ($form['#ajaxsubmit_error_message']) {
        $form['ajaxsubmit_error_message'] = array(
          '#type' => 'hidden',
          '#value' => $form['#ajaxsubmit_error_message'],
        );
      }
      if ($form['#ajaxsubmit_error_redirect']) {
        $form['ajaxsubmit_error_redirect'] = array(
          '#type' => 'hidden',
          '#value' => $form['#ajaxsubmit_error_redirect'],
        );
      }
    }
  }
}