name = example description = "example batch" core = 7.x version = "7.x-0.1"
<?php function example_menu() { $items["example"] = array( "title" => "Example", "type" => MENU_NORMAL_ITEM, "page callback" => "drupal_get_form", // , "page arguments" => array('example_form'), // 'access arguments' => array('administer site configuration'), ); return $items; } // , example_form function example_form($form, &$form_state) { $form["submit"] = array( '#type' => 'submit', '#value' => t('Submit') ); $form["#action"] = url("example"); return $form; }
// function example_form_submit() { $batch = array( 'title' => t('import'), 'operations' => array( array('example_batch_0', array($_SERVER['REQUEST_TIME'])), // array('example_batch_1', array($_SERVER['REQUEST_TIME'])), // ... , ), 'finished' => 'example_batch_finished', ); batch_set($batch);// batch_process();// }
// , , , &$context function example_batch_0($time, &$context) { if (empty($context['sandbox'])) { // , example_batch_0 $context['sandbox']['progress'] = 0; $context['sandbox']['max'] = 100500; // , } // - // , - $context["result"] $context['message'] = $context['sandbox']['progress']. "/" . $context['sandbox']['max'] ." - ". ($_SERVER['REQUEST_TIME'] - $time) . " sec"; $context['sandbox']['progress']++; if ($context['sandbox']['progress'] < $context['sandbox']['max']) { $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; } }
function example_batch_finished($success, $results, $operations) { if ($success) { drupal_set_message('yep'); } else { drupal_set_message('nope', 'error'); } }
Source: https://habr.com/ru/post/144424/
All Articles