1

I want to prepopulate some node fields with image metadata (using the EXIF module) right after an image is uploaded/inserted in a node image field with media browser widget and before the node is being saved.

By default, the EXIF module updates the node fields with image metadata after the node is being saved. But, ideally, I would like to hook into the file upload process and update the node fields instantly with ajax, so that users can preview and even edit the metadata related fields.

1 Answer 1

0

If I understand your question correctly, I believe you can do this with hook_form_alter. You can create a new form submit handler to do anything you want to do the data before it's saved. I'm not sure why you would need ajax.

my_module_form_alter(&$form, &$form_state, $form_id){
    $form['#submit'] = array();
    $form['#submit'][] = 'my_module_submit_handler';
//this makes sure your submit handler is run first
     array_unshift($form['#submit'], 'my_module_submit_handler');
}

Then create a function to make your changes and call the original form submit function...

function my_module_submit_handler($form_id, &$form_state){
     /* 
       changes to the values submitted to your form
     */
   }

This is off the top of my head but should put you on the correct path. I believe that your handler will execute and then the default handler will execute.

3
  • But as I mentioned in my question, I want the data before the form submission, just after file upload.
    – user20765
    Commented Mar 1, 2018 at 10:47
  • then you can use any jQuery event you want to trigger the ajax... put the form alter in the ajax... call it using an on change event... for the file field... Commented Mar 1, 2018 at 12:32
  • The problem is that I do not know how to implement what you describe. If you could provide a more specific code sample for that (may be in another answer) it would be nice.
    – user20765
    Commented Mar 28, 2018 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.