You are here

function form_expand_ahah in Drupal 6

Add AHAH information about a form element to the page to communicate with javascript. If #ahah[path] is set on an element, this additional javascript is added to the page header to attach the AHAH behaviors. See ahah.js for more information.

Parameters

$element: An associative array containing the properties of the element. Properties used: ahah_event, ahah_path, ahah_wrapper, ahah_parameters, ahah_effect.

Return value

None. Additional code is added to the header of the page using drupal_add_js.

Related topics

1 string reference to 'form_expand_ahah'
system_elements in modules/system/system.module
Implementation of hook_elements().

File

includes/form.inc, line 1878

Code

function form_expand_ahah($element) {
  global $user;
  static $js_added = array();

  // Add a reasonable default event handler if none specified.
  if (isset($element['#ahah']['path']) && !isset($element['#ahah']['event'])) {
    switch ($element['#type']) {
      case 'submit':
      case 'button':
      case 'image_button':

        // Use the mousedown instead of the click event because form
        // submission via pressing the enter key triggers a click event on
        // submit inputs, inappropriately triggering AHAH behaviors.
        $element['#ahah']['event'] = 'mousedown';

        // Attach an additional event handler so that AHAH behaviours
        // can be triggered still via keyboard input.
        $element['#ahah']['keypress'] = TRUE;
        break;
      case 'password':
      case 'textfield':
      case 'textarea':
        $element['#ahah']['event'] = 'blur';
        break;
      case 'radio':
      case 'checkbox':
      case 'select':
        $element['#ahah']['event'] = 'change';
        break;
    }
  }

  // Adding the same javascript settings twice will cause a recursion error,
  // we avoid the problem by checking if the javascript has already been added.
  if (isset($element['#ahah']['path']) && isset($element['#ahah']['event']) && !isset($js_added[$element['#id']])) {
    drupal_add_js('misc/jquery.form.js');
    drupal_add_js('misc/ahah.js');
    $ahah_binding = array(
      'url' => url($element['#ahah']['path']),
      'event' => $element['#ahah']['event'],
      'keypress' => empty($element['#ahah']['keypress']) ? NULL : $element['#ahah']['keypress'],
      'wrapper' => empty($element['#ahah']['wrapper']) ? NULL : $element['#ahah']['wrapper'],
      'selector' => empty($element['#ahah']['selector']) ? '#' . $element['#id'] : $element['#ahah']['selector'],
      'effect' => empty($element['#ahah']['effect']) ? 'none' : $element['#ahah']['effect'],
      'method' => empty($element['#ahah']['method']) ? 'replace' : $element['#ahah']['method'],
      'progress' => empty($element['#ahah']['progress']) ? array(
        'type' => 'throbber',
      ) : $element['#ahah']['progress'],
      'button' => isset($element['#executes_submit_callback']) ? array(
        $element['#name'] => $element['#value'],
      ) : FALSE,
    );

    // If page caching is active, indicate that this form is immutable.
    if (variable_get('cache', CACHE_DISABLED) != CACHE_DISABLED && !$user->uid && $_SERVER['REQUEST_METHOD'] == 'GET' && page_get_cache(TRUE)) {
      $ahah_binding['immutable'] = TRUE;
    }

    // Convert a simple #ahah[progress] type string into an array.
    if (is_string($ahah_binding['progress'])) {
      $ahah_binding['progress'] = array(
        'type' => $ahah_binding['progress'],
      );
    }

    // Change progress path to a full URL.
    if (isset($ahah_binding['progress']['path'])) {
      $ahah_binding['progress']['url'] = url($ahah_binding['progress']['path']);
    }

    // Add progress.js if we're doing a bar display.
    if ($ahah_binding['progress']['type'] == 'bar') {
      drupal_add_js('misc/progress.js');
    }
    drupal_add_js(array(
      'ahah' => array(
        $element['#id'] => $ahah_binding,
      ),
    ), 'setting');
    $js_added[$element['#id']] = TRUE;
    $element['#cache'] = TRUE;
  }
  return $element;
}