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',
);
}
}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');
}
}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
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.
----------------
Regards,
ARDAS group - Web site development, Drupal services, Software development, IT outsourcing.
Same Problem
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
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
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
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