Drupal 批处理表单 - 过程值?

发布于 2024-12-17 01:28:57 字数 43 浏览 0 评论 0原文

有没有办法让表单将表单值发送到批处理过程中并在每个批处理操作中使用它们?

Is there a way to have a form send form values into a batch process and use them on each batch operation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

⒈起吃苦の倖褔 2024-12-24 01:28:57

是的,您可以将它们传递给声明批处理的函数,然后从那里将它们传递给每个操作:

function mymodule_myform_submit(&$form, &$form_state) {
  $var1 = $form_state['values']['var1'];
  $var2 = $form_state['values']['var2'];
  batch_set(mymodule_mybatch($var1, $var2));
}

function mymodule_mybatch($var1, $var2) {
  $nid = db_result(db_query_range("SELECT nid FROM {node} ORDER BY nid ASC", 0, 1));

  $operations = array();
  for ($i = 0; $i < 100; $i++) {
    $operations[] = array('mymodule_mybatch_op', array($nid, $var1, $var2));
  }
  $batch = array(
    'operations' => $operations,
    'finished' => 'mymodule_mybatch_finished',
  );
  return $batch;
}

function mymodule_mybatch_op($nid, $var1, $var2) {
  // Perform the operation
}

function mymodule_mybatch_finished($success, $results, $operations) {
  if ($success) {
    $message = count($results) . ' processed.';
  }
  else {
    $error_operation = reset($operations);
    $message = 'An error occurred while processing ' . $error_operation[0] . ' with arguments :' . print_r($error_operation[0], TRUE);
  }
  drupal_set_message($message);
}

显然,您需要更改 mymodule_mybatch 中数据的来源以满足您的需求: )

Yes you can just pass them through to the function that declares the batch, and from there pass them to each operation:

function mymodule_myform_submit(&$form, &$form_state) {
  $var1 = $form_state['values']['var1'];
  $var2 = $form_state['values']['var2'];
  batch_set(mymodule_mybatch($var1, $var2));
}

function mymodule_mybatch($var1, $var2) {
  $nid = db_result(db_query_range("SELECT nid FROM {node} ORDER BY nid ASC", 0, 1));

  $operations = array();
  for ($i = 0; $i < 100; $i++) {
    $operations[] = array('mymodule_mybatch_op', array($nid, $var1, $var2));
  }
  $batch = array(
    'operations' => $operations,
    'finished' => 'mymodule_mybatch_finished',
  );
  return $batch;
}

function mymodule_mybatch_op($nid, $var1, $var2) {
  // Perform the operation
}

function mymodule_mybatch_finished($success, $results, $operations) {
  if ($success) {
    $message = count($results) . ' processed.';
  }
  else {
    $error_operation = reset($operations);
    $message = 'An error occurred while processing ' . $error_operation[0] . ' with arguments :' . print_r($error_operation[0], TRUE);
  }
  drupal_set_message($message);
}

Obviously you'd need to change where the data comes from in mymodule_mybatch to suit your needs :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文