Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here you can see my sample of batch relization with form that calls batch:</p> <pre><code> function my_module_menu() { $items['admin/commerce/import'] = array( 'title' =&gt; t('Import'), 'page callback' =&gt; 'drupal_get_form', 'page arguments' =&gt; array('my_module_settings_form'), 'access arguments' =&gt; array('administer site settings'), 'type' =&gt; MENU_NORMAL_ITEM, ); return $items; } /** * Import form */ function my_module_settings_form() { $form = array(); $form['import'] = array( '#type' =&gt; 'fieldset', '#title' =&gt; t('Import'), '#collapsible' =&gt; TRUE, '#collapsed' =&gt; FALSE, ); $form['import']['submit'] = array( '#type' =&gt; 'submit', '#value' =&gt; t('Import'), ); return $form; } function my_module_settings_form_submit($form, &amp;$form_state) { batch_my_module_import_start(); } /** * Batch start function */ function batch_my_module_import_start() { $batch = array( 'title' =&gt; t('Import products'), 'operations' =&gt; array( array('_batch_my_module_import', array()), ), 'progress_message' =&gt; t('Import. Operation @current out of @total.'), 'error_message' =&gt; t('Error!'), 'finished' =&gt; 'my_module_batch_finished', ); batch_set($batch); } /** * Import from 1C operation. Deletes Products */ function _batch_my_module_import(&amp;$context) { // Your iterms. In my case select all products $pids = db_select('commerce_product', 'p') -&gt;fields('p', array('sku', 'product_id', 'title')) -&gt;condition('p.type', 'product') -&gt;execute() -&gt;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'] &lt; $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 &lt; $context['sandbox']['max'] &amp;&amp; $counter &lt; $limit; $i++) { /* Loop items here */ /* ... */ /* ... */ $context['results']['added']['products'][] = $product_item-&gt;title; // Update Progress $context['sandbox']['progress']++; $counter++; // Messages $context['message'] = t('Now processing product %name. Product %progress of %count', array('%name' =&gt; $product_item-&gt;title, '%progress' =&gt; $context['sandbox']['progress'], '%count' =&gt; $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' =&gt; 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' =&gt; $error_operation[0], '@args' =&gt; print_r($error_operation[0], TRUE)))); } watchdog('import', 'import finished'); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload