Index: cck_select_other.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck_select_other/cck_select_other.module,v
retrieving revision 1.1.2.7
diff -u -p -r1.1.2.7 cck_select_other.module
--- cck_select_other.module	1 Mar 2010 05:35:52 -0000	1.1.2.7
+++ cck_select_other.module	1 Mar 2010 15:52:48 -0000
@@ -6,6 +6,23 @@
  * Implements a select list widget that lets a user provide an alternate option.
  */
 
+/**
+ * Implementation of hook_menu().
+ */
+function cck_select_other_menu() {
+  $items = array();
+
+  $items['cck_select_other/%'] = array(
+    'title' => 'CCK Select Other AHAH Callback',
+    'page callback' => 'cck_select_other_ahah',
+    'page arguments' => array(1),
+    //TODO: access
+    'access arguments' => array('access content'),
+  );
+
+  return $items;
+}
+
 
 /**
  * Implementation of hook_widget_info().
@@ -82,11 +99,13 @@ function cck_select_other_widget(&$form,
     '#options' => $options,
     '#default_value' => $def,
   );
-  $element['select_other_text_input'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Specify other'),
-    '#default_value' => ($def == 'other') ? check_plain($items[$delta]['value']) : '',
-  );
+  if ($def == 'other') {
+    $element['select_other_text_input'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Specify other'),
+      '#default_value' => ($def == 'other') ? check_plain($items[$delta]['value']) : '',
+    );
+  }
   return $element;
 }
 
@@ -119,6 +138,16 @@ function cck_select_other_process($eleme
     $form_state['values'][$field_name] = $element['#value'][$delta];
     $form_state['clicked_button']['#post'][$field_name] = $element['#value'][$delta];
   }
+  else {
+    $element['select_other_list']['#ahah'] = array(
+      'path' => 'cck_select_other/'.$field_name,
+      'event' => 'change',
+      'effect' => 'slide',
+      'method' => 'replace',
+      'wrapper' => $element['#id'].'-ahah-wrapper',
+    );
+  }
+  $element['#attributes']['id'] = $element['#id'].'-ahah-wrapper';
 
   if (empty($element['#element_validate'])) {
     $element['#element_validate'] = array();
@@ -192,6 +221,7 @@ function cck_select_other_pre_render($fo
     '#field_name' => $form['#field_name'],
     '#type_name' => $form['#type_name'],
     '#tree' => $form['#tree'],
+    '#attributes' => $form['#attributes'],
   );
 
   $new_form['select_other_list'] = array(
@@ -200,13 +230,95 @@ function cck_select_other_pre_render($fo
     '#options' => $options,
     '#default_value' => $select_def,
     '#weight' => -10,
+    '#ahah' => array(
+      'path' => 'cck_select_other/'.$field_name,
+      'event' => 'change',
+      'effect' => 'slide',
+      'method' => 'replace',
+      'wrapper' => $form['#field_name']['#id'].'-ahah-wrapper',
+    ),
   );
-  $new_form['select_other_text_input'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Specify other'),
-    '#default_value' => ($select_def == 'other') ? check_plain($def) : '',
-    '#value' => ($select_def == 'other') ? check_plain($def) : '',
-  );
+  if ($select_def == 'other' ) {
+    $new_form['select_other_text_input'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Specify other'),
+      '#default_value' => ($select_def == 'other') ? check_plain($def) : '',
+      '#value' => ($select_def == 'other') ? check_plain($def) : '',
+    );
+  }
 
   return $new_form;
 }
+
+/**
+ * ahah callback to add in input form
+ * @param $field_name
+ */
+function cck_select_other_ahah($field_name) {
+  $cached_form_state = array();
+
+  if (!($cached_form = form_get_cache($_POST['form_build_id'], $cached_form_state)) || !isset($field_name)) {
+    form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.'));
+    $output = theme('status_messages');
+    print drupal_to_js(array('status' => TRUE, 'data' => $output));
+    exit();
+  }
+
+  $field = content_fields($form['#field_name']);
+  $form_state = array('values' => $_POST);
+  $message = '';
+
+  $select_def = $form_state['values'][$field_name]['select_other_list'];
+  $def = $form_state['values'][$field_name]['select_other_text_input'];
+/*
+  $element = array(
+    '#type' => 'fieldset',
+    '#title' => t($field['widget']['label']),
+    '#collapsible' => TRUE,
+    '#collapsed' => FALSE,
+    '#attributes' => array('id' => 'edit-field-'.substr($field_name, 6).'-ahah-wrapper'),
+  );
+
+  $element['select_other_list'] = array(
+    '#type' => 'select',
+    '#title' => t(''),
+    '#id' => $cached_form[$field_name]['select_other_list']['#id'],
+    '#default_value' => $select_def,
+    '#value' => $select_def,
+    '#options' => $cached_form[$field_name]['select_other_list']['#options'],
+    '#ahah' => array(
+      'path' => 'cck_select_other/'.$field_name,
+      'event' => 'change',
+      'effect' => 'slide',
+      'wrapper' => 'edit-field-'.substr($field_name, 6).'-ahah-wrapper',
+    ),
+    '#weight' => -10,
+  );
+*/
+  $cached_form[$field_name]['select_other_list']['#value'] = $select_def;
+  if ($select_def == 'other') {
+    $cached_form[$field_name]['select_other_text_input'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Specify other'),
+      '#default_value' => '',
+    );
+  }
+  else {
+    if (isset($cached_form[$field_name]['select_other_text_input'])) {
+      $cached_form[$field_name]['select_other_text_input']['#type'] = 'hidden';
+    }
+  }
+
+  form_set_cache($_POST['form_build_id'], $cached_form, $form_state);
+  $form_state = array('submitted' => FALSE);
+  $form = form_builder($_POST['form_build_id'], $cached_form, $form_state);
+  $element = $form[$field_name];
+
+  //TODO: check this, from cck
+  $javascript = drupal_add_js(NULL, NULL);
+  $output_js = isset($javascript['setting']) ? '<script type="text/javascript">jQuery.extend(Drupal.settings, '. drupal_to_js(call_user_func_array('array_merge_recursive', $javascript['setting'])) .');</script>' : '';
+
+  print drupal_to_js(array('status' => TRUE, 'data' => $message . drupal_render($element) . $output_js));
+  exit();
+}
+
