3

Is there a Javascript/PHP callback for the managed_file form element that is invoked when the file is successfully uploaded? (I mean when the "Upload" button is clicked, not when the form is submitted.)

$form['video'] = array(
    '#title' => t('Video'),
    '#type' => 'managed_file',
    '#default_value' => variable_get('image_example_image_fid', ''),
    '#upload_location' => 'public://temp_managed/',
);  

I have tried:

  • #file_value_callbacks, but this seems to be a preprocess
  • #process, but it also seems to happen during the page load
2

For the PHP side, use #upload_validators as described in Form API docs. They are fired after upload, just like you want them to be. In Drupal everything that's supposed to happen between user's input and actual form save, happens in validators. Pretty weird, but that's the way it is.

For an JavaScript part, element's description says:

The #managed_file element is expanded into a set of FAPI elements including two '#submit' buttons one for Upload one for Remove, a '#file' element and a handful of '#hidden' and '#markup' elements to handle progress indication and displaying of already uploaded files.

Thus, while it does not have any '#ajax' property, you can try to access it's internals with your own custom code. Notably, successful file upload makes delete button visible. But internals can be refactored without notice, so be careful about that.

1

PHP: hook_field_widget_form_alter() is what ImageInfo Cache uses to get the file right after it has been uploaded on the PHP side. I use this to add a #process callback which then adds a callback to the #submit button. All of this to generate image styles in a background process right after the image was uploaded.

If you want a more barbaric way file_get_contents("php://input") & $_POST will give you access to everything needed. This is not advised & a bad idea. More info about the PHP wrapper.

Your Answer

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

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