Drupal 7 - 使用managed_file类型上传文件后自动提交表单

J S*_*hut 5 drupal file-upload drupal-7

我有一个只有一个字段的表单.该字段的类型为"managed_field".单击"上载"按钮时,进度条将显示文件上载的进度.之后,您需要提交表单以保存文件.

由于当您选择文件然后单击表单提交按钮而不是"上载"按钮时,进度条将不会显示.我想在上传(通过"上传"按钮)完成后触发表单提交.

我目前的表格如下:

$form['#attributes'] = array('enctype' => "multipart/form-data");

$form['pdf_upload'] = array(
    '#title' => t('Upload PDF'),
    '#type' => 'managed_file',
    '#required' => TRUE,
    '#progress_message' => t('Please wait...'),
    '#progress_indicator' => 'bar',
    '#upload_validators' => array(
        'file_validate_extensions' => array('pdf'),
    )

);

$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
);
Run Code Online (Sandbox Code Playgroud)

文件模块通过ajax回调文件/ ajax/*uri来处理文件.回调返回ajax命令.

基本上我想添加一个额外的ajax命令,在上传文件完成后触发表单提交.

J S*_*hut 2

@Clive 这对我来说不是一个选择,因为我希望用户自己开始上传。你的回答给了我一些想法,我想出了以下解决方案。

Drupal.behaviors.fileUpload = {
    attach: function(context, settings) {
        jQuery("body").ajaxComplete(function(event,request, settings){
            // Only do something when on the orders page of a user
            // This is where I use the upload functionality
            if(window.location.pathname.match(/user\/\d+\/orders/)) {
                // Check if the AjaxComplete was triggered by the managed file upload
                // pdf_upload_XXX is my form name
                // Get the form-build-id from the URL
                if (form_build_id = settings.url.match(/file\/ajax\/pdf_upload_\d*\/(.*)$/)) {
                    // Check if the upload has completed by checking if there is a Delete button in the form that has the form-build-id
                    if(jQuery('[value="'+form_build_id[1]+'"]').closest('form').find('[id$=remove-button]').length) {
                        // Click the submit button
                        jQuery('[value="'+form_build_id[1]+'"]').closest('form').find('[id^=edit-submit]').click();
                    }
                }
            }   
        });

    }
}
Run Code Online (Sandbox Code Playgroud)

希望这对其他用户也有用。

感谢克莱夫让我走上了正确的道路。