You are here

function webform_ajax_form_webform_client_form_alter in Webform Ajax 7

Same name and namespace in other branches
  1. 7.2 webform_ajax.module \webform_ajax_form_webform_client_form_alter()

Implements hook_form_FORM_ID_alter().

With FORM_ID = webform_client_form. Add AJAX logic to Webform form.

File

./webform_ajax.module, line 90
Webform AJAX module file.

Code

function webform_ajax_form_webform_client_form_alter(&$form, $form_state, $form_id) {
  $webform = $form['#node']->webform;
  if ($webform['webform_ajax'] != WEBFORM_AJAX_NO_AJAX) {

    // The wrapper ID will have to follow the webform all the time, to be unique,
    // and allow AJAX commands to perform correctly.
    // We have many ways to get it back depending on the situation.
    $form['webform_ajax_wrapper_id'] = array(
      '#type' => 'hidden',
    );
    if (isset($form_state['values']['webform_ajax_wrapper_id'])) {
      $form['webform_ajax_wrapper_id']['#value'] = $form_state['values']['webform_ajax_wrapper_id'];
    }
    elseif (isset($form['#node']->webform['webform_ajax_wrapper_id'])) {
      $form['webform_ajax_wrapper_id']['#value'] = $form['#node']->webform['webform_ajax_wrapper_id'];
    }
    else {

      // At last, generate a unique ID.
      $form['webform_ajax_wrapper_id']['#value'] = drupal_html_id('webform-ajax-wrapper-' . $form['#node']->nid);
    }
    $form['#prefix'] = '<div id="' . $form['webform_ajax_wrapper_id']['#value'] . '">' . (isset($form['#prefix']) ? $form['#prefix'] : '');
    $form['#suffix'] = (isset($form['#suffix']) ? $form['#suffix'] : '') . '</div>';
    foreach (array(
      'previous',
      'next',
      'submit',
      'draft',
    ) as $button) {
      if (isset($form['actions'][$button])) {
        $form['actions'][$button]['#ajax'] = array(
          'callback' => 'webform_ajax_callback',
          'wrapper' => $form['webform_ajax_wrapper_id']['#value'],
          'progress' => array(
            'message' => '',
            'type' => 'throbber',
          ),
        );
        if (in_array($button, array(
          'next',
          'submit',
        ))) {
          $form['actions'][$button]['#ajax']['event'] = 'click';
        }

        // Workaround for Drupal core bug http://drupal.org/node/1548976.
        // As long as buttons HTML id are causing problems, and it has bad
        // consequences on pages where Webform AJAX is active, we'll force
        // custom ids on AJAXed Webform's buttons.
        $submit_id = drupal_html_id('edit-webform-ajax-' . $button . '-' . $form['#node']->nid);
        $form['actions'][$button]['#attributes']['id'] = $submit_id;
        $form['actions'][$button]['#id'] = $submit_id;
      }
    }
  }
}