2012-08-02 14 views
5

Ho ottenuto un modulo con un solo campo. Questo campo è del tipo "managed_field". Quando fai clic sul pulsante "Carica", una barra di avanzamento ti mostrerà l'avanzamento del caricamento del file. Dopodiché dovrai inviare il modulo per salvare il file.Drupal 7 - modulo di invio automatico dopo il caricamento del file con tipo managed_file

Poiché la barra di avanzamento non viene visualizzata quando si seleziona un file, quindi fare clic sul pulsante di invio modulo anziché sul pulsante "Carica". Vorrei attivare un modulo di invio dopo il caricamento (tramite il pulsante "Carica") completato.

mia forma attuale si presenta così:

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

Il modulo di file gestisce i file tramite un callback ajax al file/Ajax/* uri. Il callback restituisce comandi ajax.

In pratica, voglio aggiungere un comando ajax in più che attiva il modulo di invio dopo il completamento del caricamento del file.

+1

Potrebbe essere difficile. Un'alternativa è quella di caricare automaticamente il file alla selezione in modo che l'utente debba solo fare clic su un pulsante. Vedi http://drupal.stackexchange.com/questions/31121 – Clive

risposta

2

@Clive Questa non era un'opzione per me poiché volevo che gli utenti iniziassero il caricamento da soli. La tua risposta mi ha dato alcune idee e mi è venuta in mente la seguente soluzione.

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

    } 
} 

Spero che questo sia utile anche per altri utenti.

Thnx Clive per avermi messo sulla strada giusta.

Problemi correlati