Skip to content

Instantly share code, notes, and snippets.

@chx
Created October 22, 2012 10:27
Show Gist options
  • Save chx/3930812 to your computer and use it in GitHub Desktop.
Save chx/3930812 to your computer and use it in GitHub Desktop.
Batch proof of concept
<?php
use Symfony\Component\HttpFoundation\JsonResponse;
function btest_menu() {
$items['btest'] = array(
'type' => MENU_CALLBACK,
'access callback' => TRUE,
'page callback' => 'btest_page',
);
$items['btest'] = array(
'type' => MENU_CALLBACK,
'access callback' => TRUE,
'page callback' => 'btest_js_page',
);
return $items;
}
function btest_page() {
if ($_GET['op'] == 'finished') {
return array('#markup' => 'done');
}
variable_set('btest', 20);
$js_setting = array(
'batch' => array(
'errorMessage' => 'None',
'initMessage' => 'start',
'uri' => url('btest/js', array('query' => array('id' => 1))),
),
);
drupal_add_js($js_setting, 'setting');
drupal_add_library('system', 'drupal.batch');
$output = theme('progress_bar', array('percent' => 0, 'message' => 'testing'));
drupal_set_page_content($output);
$page = element_info('page');
$page['#show_messages'] = FALSE;
return $page;
}
function btest_js_page() {
$btest = variable_get('btest');
$percentage = 5 * (20 - $btest);
$message = "btest value: $btest";
variable_set('btest', $btest - 1);
return new JsonResponse(array('status' => TRUE, 'percentage' => (string) $percentage, 'message' => $message));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment