I have what I think is a pretty useful demo module for understanding how ahah works in Drupal 6.x. It is the module code
with screencast. I couldn't find anything like it and so it took me quite some time to figure out how ahah works with the 6.x form API. All the bits and pieces are out there, but not in one place.

Is there a place to post such a thing? Or is this the place?

Thanks,
pre

Comments

tincorad’s picture

Hi pre!
I'm doing some research in order to understand how AHAH works in Drupal and how to use that functionality in my custom modules, and I certanly see the lack of examples so I will be very interested in watching your screencast whenever you have it online.
So, to answer your question I don't really know if this is the best place to put it but I found this after I searched for 'AHAH' in Drupal.org and I think it could be a good place to start.
Looking forward for your examples/screencasts..

thanks,
tincorad

pre911mindset’s picture

I don't know if this is the right spot either, but I'll post it now. Here is a sample (or demo) drupal 6.x module ahah with comments. I may follow with a screencast later, although this is simple and basic enough I'm not sure if one is needed.

<?php
// Minimal AHAH Form Implementation for 6.x
// Place these 2 files into /sites/all/modules/test
// test.module - this file.
/* test.info - should contain this:
name = "Test"
description = "For Testing."
core = "6.x"
version = "6.x-1.1"
 * * * */

define(TEST_WRAPPER,'ahah-will-replace-this-stuff');
define(TEST_PATH,'test/ahah');
define(TEST_TEXT_FORM_FIELD,'Test-Text-Body');

/** Implementation of menu_hook.  **/
function test_menu() {

  $items = array();

  // Setup the initial menu option.
  // yoursite/?q=test
  // if you use clear URLs  
  // yoursite/test  
  $items['test'] = array(
    'title' => 'Test setup',
    'description' => 'A simple test form.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array ('test_cb'),
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM
  );

  /**
   * This is the path in your site to the ajax/ahah callback function.
   */
  $items[TEST_PATH] = array(
    'title' => 'Test setup',
    'description' => 'A simple test AHAH callback.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array (
           // first argument is the callback function.
            'test_ahah_cb'),
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM
  );

  return $items;
}

/**
 * Here is our menu callback function (see menu_hook).
 * Just create a simple form with a single button.
 * @return drupal form array
 */
function test_cb(&$form_state) {
    $form['test'] = array (
    '#prefix' => '<div id="'.TEST_WRAPPER.'">',
    '#suffix' => '</div>',
    '#type' => 'fieldset',
    '#title' => 'Test Module...', 
    '#tree' => TRUE,
    );

    $form['test']['button'] = array (
    '#type' => 'submit',
    '#name' => 'Test Submit Button',
    '#value' => 'Push Me',
    '#id' => 'unique-test-id',
    '#ahah' => array(
        'path' => TEST_PATH,
        'event' => 'click',
        'wrapper' => TEST_WRAPPER,
        'progress' => array('type' => 'bar', 'message' => t('Grinding...')),
      ),    
    );
        
    $form['test']['check'] = array (
    '#prefix' => '<small>',
    '#suffix' => '</small>',
    '#type' => 'checkbox',
    '#name' => 'Display_Debugging',
    '#title' => 'Display Debug Fields',
    '#default_value' => FALSE,);
  
    return $form;
}

/**
 * This is the callback function for the ajax part.  
 * Return the original form, with some updated stuff on 
 * it.
 */
function test_ahah_cb() {
    
    // Unlike with normal 'submit' cb, $form and $form_state don't come
    // with ahah callbacks.  We need to get them from the cache.
    $form_state = array();
    $form_build_id = $_POST['form_build_id'];
    $form = form_get_cache($form_build_id, $form_state);
    
    // Now, set the fields in the old form the way we want them.
    // For this example we are adding a new field.
    $form['test']['body'] = array(
      '#type' => 'textfield',
      '#name' => TEST_TEXT_FORM_FIELD,
      '#title' => t('New Button Text'),
      '#size' => 40,
      '#maxlength' => 40,
      '#default_value' => 'You\'re Welcome',
      );        
    // If this is ahah cb, $_POST php global contains
    // info about the form that was posted.
    $value = 'Thank You';
    if (isset($_POST[TEST_TEXT_FORM_FIELD])) {
        $value = $_POST[TEST_TEXT_FORM_FIELD]; 
    }

    $form['test']['button']['#value']= $value;
      
        
    /**
     * Some debugging fields, we put our debug info into
     * fields since we can't use drupal_set_message.
     */
    $debug = isset($_POST['Display_Debugging']) &&
        ($_POST['Display_Debugging']== '1');
    $form['test']['check']['#default_value'] = $debug;
    _test_set_debugging($form,$form_state,$debug);
   
    // Now we need to save the form in the cache, so that future
    // submits will work with it.  Note that we could set stuff
    // in form_state['storage'] if we wanted to.
    form_set_cache($form_build_id, $form, $form_state);
    
    // Rebuild the form and display.
    $form = form_builder($_POST['form_id'], $form, $form_state);
    $output = drupal_render($form);     
    print drupal_to_js(array('data' => $output, 'status' => true));
    exit();         
}

/**
 * Set some debugging fields for ahah debugging.
 *
 * @param drupal form $form
 * @param drupal form_state $form_state
 * @param boolean $debug
 */
function _test_set_debugging(&$form,&$form_state,$debug) {
    if ($debug) {
        ob_start();
        var_dump($form);
        $debug_text = ob_get_clean();
        $form['test']['debugform'] = array(
          '#type' => 'textarea',
          '#name' => 'Debug-Text-Body',
          '#title' => t('Stuff in the form'),
          '#default_value' => $debug_text,
          );    
        ob_start();
        var_dump($_POST);
        $debug_text = ob_get_clean();
        $form['test']['debugpost'] = array(
          '#type' => 'textarea',
          '#name' => 'Debug-Post-Body',
          '#title' => t('Stuff in $_POST'),
          '#default_value' => $debug_text,
          );    
        ob_start();
        var_dump($form_state);
        $debug_text = ob_get_clean();
          $form['test']['debugstate'] = array(
          '#type' => 'textarea',
          '#name' => 'Debug-Post-Body',
          '#title' => t('Stuff in $form_state'),
          '#default_value' => $debug_text,
          );    
    } else {
        unset($form['test']['debugstate']);    
        unset($form['test']['debugform']);    
        unset($form['test']['debugpost']);    
    }            
}

?>