2011-04-12 63 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引导程序中没有包含的文件中。如果将批处理操作功能从包含文件移出并放入启用的模块的模块文件中,它将起作用。