2012-08-02 6 views
5

1つのフィールドしかないフォームがあります。このフィールドのタイプは 'managed_field'です。 [アップロード]ボタンをクリックすると、ファイルアップロードの進捗状況がプログレスバーに表示されます。その後、ファイルを保存するためにフォームを提出する必要があります。Drupal 7 - managed_fileタイプのファイルアップロード後のフォームの自動提出

ファイルを選択して[アップロード]ボタンではなくフォーム送信ボタンをクリックすると、進行状況バーが表示されないため、アップロード(「アップロード」ボタン経由)が完了した後、フォーム提出を開始したいと思います。

私の現在のフォームは、次のようになります。

$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'), 
); 

ファイル・モジュールは、ファイル/ AJAX/* URIにAJAXコールバックを経由してファイルを処理します。コールバックはajaxコマンドを返します。

基本的には、ファイルのアップロードが完了した後にフォーム提出をトリガーする追加のajaxコマンドを追加します。

+1

これは難しいかもしれません。別の方法として、選択時にファイルを自動アップロードして、ユーザーがボタンを1つクリックするだけで済みます。 http://drupal.stackexchange.com/questions/31121を参照してください。 – Clive

答えて

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(); 
        } 
       } 
      } 
     }); 

    } 
} 

これは他のユーザーにも役立ちます。

Thnx正しいパスに私を設定するためのクライブ。

関連する問題