4

処理用のバッチ ページをセットアップしようとしていますが、例が必要です。Example モジュールで提供されている例はフォーム内にあり、バッチ リクエストを処理するフォームから独立して実行できるページが必要です。

例えば:

function mymodule_batch_2() {
 $operations[] = array('mymodule_onefunction','mymodule_anotherfunction') 
 $batch = array(
  'operations' => $operations, 
  'finished' => 'mymodule_finished',
  // We can define custom messages instead of the default ones. 
  'title' => t('Processing batch 2'), 
  'init_message' => t('Batch 2 is starting.'), 
  'progress_message' => t('Processed @current out of @total.'), 
  'error_message' => t('Batch 2 has encountered an error.'),
);
  batch_set($batch);
  batch_process('');
}

バッチ関数が $operations の形式で他の関数を呼び出す場所。

4

3 に答える 3

2

ここでは、バッチを呼び出すフォームを使用したバッチ再化のサンプルを確認できます。

    function my_module_menu() {
      $items['admin/commerce/import'] = array(
        'title' => t('Import'),
        'page callback' => 'drupal_get_form',
        'page arguments' => array('my_module_settings_form'),
        'access arguments' => array('administer site settings'),
        'type' => MENU_NORMAL_ITEM,
      );
      return $items;
    }

    /**
     * Import form
     */
    function my_module_settings_form() {
      $form = array();
      $form['import'] = array(
        '#type' => 'fieldset',
        '#title' => t('Import'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
      );

      $form['import']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Import'),
      );
      return $form;
    }

    function my_module_settings_form_submit($form, &$form_state) {
      batch_my_module_import_start();
    }


    /**
     * Batch start function
     */
    function batch_my_module_import_start() {

      $batch = array(
        'title' => t('Import products'),
        'operations' => array(
          array('_batch_my_module_import', array()),
        ),
        'progress_message' => t('Import. Operation @current out of @total.'),
        'error_message' => t('Error!'),
        'finished' => 'my_module_batch_finished',
      );

      batch_set($batch);
    }

    /**
     * Import from 1C operation. Deletes Products
     */
    function _batch_my_module_import(&$context) {
      // Your iterms. In my case select all products
      $pids = db_select('commerce_product', 'p')
          ->fields('p', array('sku', 'product_id', 'title'))
          ->condition('p.type', 'product')
          ->execute()
          ->fetchAll();

      // Get Count of products
      if (empty($context['sandbox']['progress'])) {
        $context['sandbox']['progress'] = 0;
        $context['sandbox']['max'] = count($pids);
        watchdog('import', 'import products');
      }
      // Create Iteration variable
      if (empty($context['sandbox']['iteration'])) {
        $context['sandbox']['iteration'] = 0;
      }
      // Check for the end of cycle
      if ($context['sandbox']['iteration'] < $context['sandbox']['max']) {
        // Count of operation in one batch step
        $limit = 10;
        // Counts completed operations in one batch step
        $counter = 0;
        if ($context['sandbox']['progress'] != 0) {
          $context['sandbox']['iteration'] = $context['sandbox']['iteration'] + $limit;
        }
        // Loop all Product items in xml
        for ($i = $context['sandbox']['iteration']; $i < $context['sandbox']['max'] && $counter < $limit; $i++) {

          /* Loop items here */
          /* ... */
          /* ... */
          $context['results']['added']['products'][] = $product_item->title;


          // Update Progress
          $context['sandbox']['progress']++;
          $counter++;
          // Messages
          $context['message'] = t('Now processing product %name. Product %progress of %count', array('%name' => $product_item->title, '%progress' => $context['sandbox']['progress'], '%count' => $context['sandbox']['max']));
          $context['results']['processed'] = $context['sandbox']['progress'];
        }
      }

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


/**
 * Finish of batch. Messagess
 */
function my_module_batch_finished($success, $results, $operations) {
  if ($success) {
    drupal_set_message(t('@count products added.', array('@count' => isset($results['added']) ? count($results['added']) : 0)));
  }
  else {
    $error_operation = reset($operations);
    drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE))));
  }
  watchdog('import', 'import finished');
}
于 2012-10-31T08:58:23.967 に答える
2

バッチ プロセスに作業元の ID を指定する必要があります。batch_process('mybatch') そうでなければ、あなたの例は正しいです。この戦略に特に問題がありますか?

于 2012-10-31T02:45:37.647 に答える