2011-04-12 62 views
0

任何人都可以提供這方面的一些想法?基本上,我正在構建的模塊有一個表單(按照function_email_solicitors_compose),在提交時,我們顯然會路由到form_emails_solicitors_compose_submit。在這裏我定義了批處理批處理,batch_set爲上述批處理。 drupal文檔說,如果從form_submit中調用batch_process(),則不需要運行batch_process(),但是我嘗試過使用和不使用它。所有的測試都表明,它可以定義批次,但永遠不會比這更進一步。 email_solicitors_batch_iteration從不運行。有任何想法嗎?Drupal 6批處理不執行

作爲信息的一個附加位,batch_get然後返回如下:

Array 
(
    [sets] => Array 
     (
      [0] => Array 
       (
        [sandbox] => Array 
         (
         ) 

        [results] => Array 
         (
         ) 

        [success] => 
        [title] => Emailing. 
        [operations] => Array 
         (
          [0] => Array 
           (
            [0] => 

email_solicitors_batch_iteration [1] =>數組 ( [0] => [1] => )

       ) 

         ) 

        [finished] => my_finished_callback 
        [init_message] => Initializing.<br/>&nbsp; 
        [progress_message] => Remaining 

@total的剩餘部分。 [error_message] =>發生了錯誤 。 [總] => 1 )

 ) 

) 

代碼:

function email_solicitors_compose(){ 
    $form['email_solicitors_subject'] = array(
     '#type' => 'textfield', 
     '#title' => t('Subject'), 
     '#description' => t('Enter the subject of your email'), 
     '#default_value' => 'Subject', 
     '#size' => 30 
    ); 
    $form['email_solicitors_message'] = array(
     '#type' => 'textarea', 
     '#title' => t('Message'), 
     '#description' => t('Write your message here. <strong>Please note that we will automatically add "Dear #name", which will be personalised to the solicitor.</strong>'), 
     '#default_value' => '', 
    ); 
    $form['email_solicitors_submit'] = array(
     '#type' => 'submit', 
     '#title' => t('Submit'), 
     '#description' => t('Sumbit this form.'), 
     '#default_value' => 'Submit', 
    ); 
    return $form; 
}//function email_solicitors_compose 


function email_solicitors_compose_submit($form_state) 
{ 
    $batch = array(
     'title' => t('Sending emails to solicitors'), 
     'operations' => array(
      array('email_solicitors_batch_iteration', array()) 
     ), 
     'finished' => 'email_solicitors_batch_finished', //run this when we're finished 
     'init_message' => t('Preparing to send emails'), //initialisation message 
     'progress_message' => t('Sent @current out of @total messages.'), 
     'error_message' => t('Sorry, something went wrong when sending emails.'), 
    );// create batch array 
    $info=print_r($batch,TRUE); 
    drupal_set_message($info); 
    batch_set($batch); 
    batch_process(); 
}//function email_solicitors_compose_submit 


function email_solicitors_batch_iteration(&$context) 
{ 
    // Initialize sandbox the first time through. 
    if (!isset($context['sandbox']['progress'])) { 
     $context['sandbox']['progress'] = 0; 
     $context['sandbox']['current_user_id'] = 0; 
     $context['sandbox']['max'] = db_result(db_query('SELECT COUNT(DISTINCT field_solicitor_email_value) FROM content_type_solicitor')); 
    } 
    $comment="On item ".$context['sandbox']['progress']; 
    drupal_set_message ($comment); 
}//function email_solicitors_batch_iteration 

function email_solicitors_batch_finished (&$context) 
{ 
    die ('woohoo we finished'); 
} 

回答

3

作爲Clive給出答案的補充,您可以考慮將「文件」參數添加到批處理數組中。這將告訴API函數所在的位置。

例子:

$batch = array(
     'title' => t('Sending emails to solicitors'), 
     'operations' => array(
      array('email_solicitors_batch_iteration', array()) 
     ), 
     'finished' => 'email_solicitors_batch_finished', //run this when we're finished 
     'init_message' => t('Preparing to send emails'), //initialisation message 
     'progress_message' => t('Sent @current out of @total messages.'), 
     'error_message' => t('Sorry, something went wrong when sending emails.'), 
     'file' => drupal_get_path('module', '<module_name>').'/path_to_include_file.inc', 
    ); 

它爲我:)

0

你有徵兆的開始,該批次開始或只是沒有任何反應? 您在批處理操作回調中缺少兩個信息:進度的增加,最重要的是確定批次何時結束的語句。

// Update our progress information. 
    $context['sandbox']['progress']++; 
    $context['sandbox']['current_node'] = $row['nid']; 
    $context['message'] = t('Calcul des votes pour %node', array('%node' => $row['title'])); 

    if ($context['sandbox']['progress'] != $context['sandbox']['max']) { 
    $context['finished'] = $context['sandbox']['progress']/$context['sandbox']['max']; 
    } 
3

爲了防止任何人仍在爲此付出努力,前面的兩個註釋是不正確的,您不需要顯式設置$ context ['finished']變量(請參閱示例模塊batch_example)。

它不工作的原因很簡單,因爲批處理操作函數在默認的Drupal引導程序中沒有包含的文件中。如果將批處理操作功能從包含文件移出並放入啓用的模塊的模塊文件中,它將起作用。