I am very new to Drupal 7 module development and I have been working to develop a module where it parses a file after the user uploads it. I am using hook_forms, but for some reason I can't get a submit handler to be called after I click upload, or attach a submit handler.

$form['r_file'] = array(
    '#type' => 'managed_file',
    '#title' => t('R file'),
    '#upload_validators' => array('file_validate_extensions' => array('xlsx')),
    '#upload_location' => 'public://babies',
    '#default_value' => variable_get('r_file'),
    '#description' => t('For all the babies in the world'),
    '#progress_message' => t('Uploading file...'),
    '#progress_indicator' => 'bar',
    '#required' => TRUE,
);

Where can I attach a handler so that when the user clicks upload/remove, I can conduct some post processing?

What I meant is according to this section, https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#managed_file it says "The form's validate and submit handlers receive a file object ID in $form_state['values'] that represents the ID of the new file in the {file_managed} table."

Then it goes on describing what should go inside the submit handler. My question is, how should the submit handler look like?

Hope that clarifies the question.

  • You may need to provide more of the code context, it's hard to see the entire problem there. – David Thomas Jan 22 '14 at 9:09
up vote 0 down vote accepted

Seems to me that you are looking for this hook_file_insert (https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_file_insert/7) It is being called once managed file is inserted to database which is exactly what it occurs when you hit the upload button of file upload widget.

  • This method to get the file does work, but for my purposes, I ended up using hook_form_alter. I realized that because I did not clear the cache, the function wasn't being called. – Jason Jan 23 '14 at 3:53

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.