Batch API

Créé le 22/02/2014

Dernière mise à jour le 05/09/2014

Étiquettes

Batch sans paramètre

Dans la batch API, lors de l'appel à des opérations de process, si le process n'a pas besoin d'arguments, il faut mettre un array vide. C'est signalé dans le commentaire, mais quand on découvre pour la première fois, on n'y fait pas tellement attention.

function batch_example($options1, $options2, $options3, $options4) {
  $batch = array(
    'operations' => array(
      array('batch_example_process', array($options1, $options2)),
      array('batch_example_process', array()),
      ),
    'finished' => 'batch_example_finished',
    'title' => t('Processing Example Batch'),
    'init_message' => t('Example Batch is starting.'),
    'progress_message' => t('Processed @current out of @total.'),
    'error_message' => t('Example Batch has encountered an error.'),
    'file' => drupal_get_path('module', 'batch_example') . '/batch_example.inc',
  );
  batch_set($batch);
  batch_process('node/1');
}

Batch API et Drush

Une fois que vous avez un process Batch de prêt, si jamais vous souhaitez l'utiliser sous forme d'une commande Drush, quelques modifications sont nécessaires. Source du code : site de Julien Dubreuil

Les deux première fonctions doivent être déclarée dans un fichier sandbox.drush.inc, il manque le fonction d'opérations de batch, mais celle-ci ne contient aucune différence avec un batch "classique".

function sandbox_drush_command() {
  $items = array();
  $items['my-import'] = array(
    'callback'    => 'sandbox_setup_batch',
    'description' => dt('Import'),
  );
  return $items;
}
function sandbox_drush_help($section) {
  switch ($section) {
    case 'drush:myimport':
    return dt("Traitement des utilisateurs.");
  }
}

function sandbox_setup_batch() {
  $operations = array();
  $operations[] = array('sandbox_batch_process', array());
  $batch = array(
    'operations' => $operations,
    'title' => t('Import batch'),
    'init_message' => t('Initializing'),
    'error_message' => t('An error occurred'),
    'finished' => 'sandbox_finished_method'
  );
  $batch['progressive'] = FALSE; 
  batch_set($batch);
  drush_backend_batch_process();
}

function sandbox_finished_method($success, $results, $operations) {
  drush_print('Finished importing!');
}

Ajouter un commentaire