I ajax enabled a Select (or other) field but the callback if not being run. The field is inside a field collection. Here is my code.
function MY_MODULE_form_field_collection_item_form_alter(&$form, &$form_state, $form_id){
if ($form['#bundle']=='field_contract_parties'){
$form['field_select_or_other']['und']['#options'] = load_select_options();
$ajax_array = array(
'callback' => 'my_callback_function',
'wrapper' => 'contacts-replace',
'event' => 'change',
);
$form['field_select_or_other']['und']['#ajax'] = $ajax_array;
$new_properties = array(
'#prefix' => '<div id="contacts-replace">',
'#suffix' => '</div>',
);
$form['field_contact']['und'] = array_merge($form['field_contact']['und'],$new_properties);
}
}
function my_callback_function($form, $form_state){
return $form['field_contact']['und'];
}
The above code worked well when i was using a List (text) field instead of a select or other field.
I have tried using 'event' => 'change' but it didn't work either.
Comments
Comment #1
cjgriffin commentedPossibly related to this, it appears that #states isn't working on a select_or_other element either. I'm also using it within a field collection.
Comment #2
danielb commentedThese aren't things I should have to handle specifically. Just attach the properties directly to the form element (which are children of select_or_other) in a #pre_render callback.
You might also be able to do it via #select_ajax and #other_ajax, etc... but that might depend on what phase of the form you are doing it.
Comment #3
cjgriffin commentedFWIW, I got #states to work by adding:
to line 104 of select_or_other.module. Might work for #ajax as well (not able to test right now).
Comment #4
jpsolero commented#3 works for #ajax too, It should be:
Comment #5
a.ross commentedI don't think using a pre-process function to add the
#ajaxstuff works. The AJAX callback system relies on it being defined during form creation, not during rendering. This is quite an annoying bug, which means having to create extremely ugly workarounds.Comment #6
danithaca commentedboth #select_ajax and #other_ajax worked. thanks!
Comment #7
jaskho commentedRe #select_ajax and #other_ajax (which I didn't immediately follow from the previous comments)...
The select_or_other module looks for any element property prefixed with "select_" or "other_" and applies it to the appropriate widget fields:
So a '#select_ajax' key on the field's element array gets turned into an '#ajax' key on the elements generated by the widget downstream.
And here, just for fun, is the code that worked for me:
Comment #8
legolasbo@Jaskho perhaps you could document this as the first documentation in select_or_other.api.php
Comment #9
sassafrass commentedI was able to use #select_ajax to get my ajax callback triggered. However, I was not able to get the #states to work. I tried both suggestions in #3 and #4 above, but it didn't work. :(
My Select or Other field:
My #states field that depends on the Select or Other field:
Comment #10
sassafrass commentedOkay, I found my mistake(s). I am now able to get #3/#4 working for #states by
1. Adding:
to the select_or_other.module file.
2. First mistake: Incorrect jquery selector. Needed to add '[select]' to the end:
name="discovery_information[reference_source_information][reference_source_type][select]"]'3. Second mistake: I was rewriting the dependent field in my ajax callback. Don't DO that! :-)
Thank-you cjgriffin and jpsolero for the suggestions.
Comment #11
legolasboYou don't have to hack the select_or_other.module file to do this. you can just add
#select_statesto your element and it will be passed along just like#select_ajax. Any#select_*element in the array will be passed to theselectform element, any#other_*array element will be passed to theotherform element.Comment #12
sassafrass commentedAs legolasbo suggested in #11, I removed the hack and replaced '#states' with '#select_states' and it works if I have a single condition. But if I have multiple conditions "AND"ed together, it doesn't get triggered the first time a state is changed, only subsequent state changes.
Comment #13
legolasboClosing this back up because the #select_states and #other_states are passed along correctly.
sassafrass, please open a new issue for the state bug you are experiencing with clear steps to reproduce the issue.