hook_form_alter with Ajax works for texts fields on first go, but only with images on second go.. I'm sure it's a simple fix

by Niall Murphy   Last Updated January 14, 2018 20:07 PM

I've added an "update" submit button to each row in a field collection.

$form['field_fc_wiz_word']['und'][$delta]['submit'] = array(
                        '#type' => 'submit',
                        '#value' => t('Update'),
                        '#weight' => 2,
                        '#ajax' => array(       
                            'wrapper' => 'words-form-update-' . $delta,
                            'callback' => 'custom_h5p_wizard_callback',
                            'clear' => TRUE,
                            'effect' => 'fade'

In that callback, I set various values but the image is causing issues. I've removed the fields that work and put in a hard-coded value that works on the second refresh.

function custom_h5p_wizard_callback($form, &$form_state) {
    $delta = $form_state['clicked_button']['#parents'][2];
    $form['field_fc_wiz_word']['und'][$delta]['field_wiz_image']['und'][0]['#default_value'] = 17984;
    return $form['field_fc_wiz_word']['und'][$delta];

I've tested all combinations of trying to set that image field but it won't work on the first update.

The other text fields are fine but adding the fid doesn't do anything for me. If I click the submit button twice, all is well.

What am I doing wrong? Thanks.

Related Questions

Ajax on Add another item of field collection

Updated January 14, 2018 11:07 AM

Add a custom property to an existing field

Updated February 06, 2018 10:07 AM