0

Edit: I managed to add the image file to [#file] so then the file was in the form but with no preview. My workaround then was to use jQuery .ajaxComplete to check for form elements with .file-size and no .image-preview, and then click the upload button.

The jQuery:

    if ($('.entitytype-interactive_content_wizard-form').length > 0) {
        $(document).ajaxComplete(function(event, response, settings) {
            $('.entitytype-interactive_content_wizard-form .image-widget').
                    each(function() {
                        if ($(this).find('.file-size').length > 0 &&
                                $(this).find('.image-preview').length == 0) {
                            var clickbtn = $(this).find("[type=submit]");
                            clickbtn.mousedown();
                        }
                    });
        });
    }

Question:

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.

  • You haven't actually said what the problem is. But I've tried to change values in ajax callbacks before, and you likely need #value, not #default_value. – Jaypan Jan 15 '18 at 6:30
  • Tried that, and every other thing I could.. When I return that element, the thumbnail of the added image won't display unless I click the ajax link again. I wrote a submit handler for form['rebuild'] based on your multi-step form tutorial but still nothing. – Niall Murphy Jan 15 '18 at 7:30
  • I did have success writing my own theme function for the image, but it wasn't really that good, so I went with the solution I added at the top of this post. – Niall Murphy Jan 15 '18 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.