Adding #ahah to a CCK field

andreiashu - March 4, 2009 - 04:22

I'm trying to add AHAH functionality to a select list. The problem is that this select list is a CCK content type and injecting a "#ahah" array (during hook_form_alter) does not work. I don't know the reason.
1.

function test_ahah_form_alter(&$form, $form_state, $form_id) {
  // i created a test_ah node type to test this
  if($form_id == 'test_ah_node_form') {
    $form['field_select']['#ahah'] = array(
      'path' => 'test_ahah',
      'wrapper' => 'edit-field-select-value-wrapper',
      'method' => 'replace',
      'effect' => 'fade',
      'event' => 'click',
    );
  }
}
This is not working at all: the js setting #ahah callback is not even added into the header.

Another form of the code that i tried that works partially is this (more info here: http://www.lullabot.com/blog/ahah-js-in-core):
2.

function test_ahah_form_alter(&$form, $form_state, $form_id) {
    $ahah_binding = array(
      'url'   => url('test_ahah'),
      'event' => 'change',
      'wrapper' => 'edit-field-select-value-wrapper',
      'selector' => '#edit-field-select-value',
      'effect'   => 'slide',
      'method'   => 'replace',
      'progress' => array('type' => 'throbber'),
    );
   
    drupal_add_js('misc/jquery.form.js');
    drupal_add_js('misc/ahah.js');
    drupal_add_js(array('ahah' => array('edit-field-select-value-wrapper' => $ahah_binding)), 'setting');
  }
}
But this form is working only partially: in the callback of the 'test_ahah' path the form_get_cache($form_build_id, $form_state) return NULL for no obvious reason.

I managed to find a workaround:
3.

function test_ahah_form_alter(&$form, $form_state, $form_id) {
  //dpm($form_id);
  if($form_id == 'test_ah_node_form') {
    // without this here the ahah for #edit-field-select-value wont work (form_get_cache returns null from the ahah callback)
    $form['title']['#ahah'] = array(
      'path' => 'test_ahah',
      //'wrapper' => 'edit-field-select-value-wrapper',
      //'method' => 'replace',
      //'effect' => 'fade',
      'event' => 'click',
    );
   
    $ahah_binding = array(
      "button" => false,
      "keypress" => null,
      'url'   => url('test_ahah'),
      'event' => 'change',
      'wrapper' => 'edit-field-select-value-wrapper',
      'selector' => '#edit-field-select-value',
      'effect'   => 'slide',
      'method'   => 'replace',
      'progress' => array('type' => 'throbber'),
    );
   
    drupal_add_js('misc/jquery.form.js');
    drupal_add_js('misc/ahah.js');
    drupal_add_js(array('ahah' => array('edit-field-select-value-wrapper' => $ahah_binding)), 'setting');
  }
}

This is really really annoying... So if anyone knows why 1. not works (or why 3. works but 2. not) please have mercy and tell me :)

I have the same issue!!! We

ardas - May 15, 2009 - 15:36

I have the same issue!!! We added a CCK Select field and now we need to develop custom feature which will populate one CCK Select based on the choice of another CCK Select. (I know about Hierarchical select - it won't work in my case). We added all #ahah stuff for CCK select on form_alter but this didn't work.

This only works if we change its type to SELECT instead of OPTIONWIDGETS_SELECT but in this case saving doesn't work.

This is very frustrating. We spent the whole day fighting with this in D6... We did this easily for D5.

Same Problem

GrimSage - May 28, 2009 - 03:36

I have been looking for the exact same thing. My ideal solution would be to fix Hierarchical select. But in the mean time I am trying to do this. Main reason be a country ->province/state selection. I found out some information in case someone else is working on this.

Form alter cant be used for cck fields because the fields have not been created yet.
http://drupal.org/node/357328

If anyone gets the solution to this, please please please share.

I fought with this for a few

pjhanson3 - June 7, 2009 - 17:54

I fought with this for a few days. It turned out that I had to adjust the module weight so my custom module was evaluated after the CCK processing to create the form.

Install the utility module and then enable module weights and make sure your custom module weight is greater than the CCK weights.

Pete

It's a can of worms

seattleturtle - August 31, 2009 - 17:40

For a number of reasons, hook_form_alter-based AHAH manipulation + CCK fields = big mess. For an informative walk through the problems, I recommend Roger Saner's thread here:
http://drupal.org/node/331941#comment-1503260

Even without the AHAH wrinkle, hook_form_alterations are tricky with CCK, per the explanation cited above and here:
http://drupal.org/node/339730

In a nutshell: CCK fields have meta types (which don't take #ahah bindings) that are only populated with standard FAPI fields after hook_form_alter gets its turn. I think the solution is a second alteration step using #pre_render, like this:

function hook_form_alter(&$form, &$form_state, $form_id) {
  ...
  $form['field_my_cck_field']['#pre_render'][] = 'my_cck_field_pre_render';
}

function my_cck_field_pre_render($element) {
  // now the real form elements are in here as children
  $element['cck-generated-fapi-element']['#ahah'] = array(
    ...
  );

  // this part may be naughty
  form_expand_ahah($element['cck-generated-fapi-element']);

  // $element doesn't pass by reference, so don't forget to return it
  return $element;
}

A second problem is that ahah bindings won't be processed for #ahah parameters added at the #pre_render stage, therefore our new #ahah parameters need to be processed manually. My possibly-naughty solution above (calling form_expland_ahah() directly) seems to work, but a FAPI expert should tell us whether that could have unintended consequences.

This still seems doesn't work

andrea.vivaldi - September 11, 2009 - 08:49

This still seems doesn't work for me... can anyone has another solution... No way to bind ahah property to a cck field.
heeeeeeeelp

=============================================================================
Andrea Vivaldi
Institute for Informatics and Telematics (IIT)
Italian National Research Council (CNR)
Via G. Moruzzi, 1 - 56124 Pisa, Italy

 
 

Drupal is a registered trademark of Dries Buytaert.