1

I am trying to write a Batch import to create nodes based on a uploaded file. The form works and I receive the file, and can read it in my submit callback which should initiate the Batch import. Here is the code:

function mymodule_import_submit(&$form, $form_state) {
  $xml = file_get_contents($form_state['values']['xml']->uri);

  $ads = xml2ary($xml);

  $batch = array(
    'title' => t('Importing Content ...'),
    'operations' => array(),
    'init_message' => t('Commencing'),
    'progress_message' => t('Processed @current out of @total.'),
    'error_message' => t('An error occurred during processing'),
    'finished' => 'mymodule_import_finished',
  );

  foreach ($nodes['nodes']['_c']['node'] as $new_node) {
    $batch['operations'][] = array('mymodule_import_create_node', array($new_node['_c'])); 
  }

  batch_set($batch);
}

While it seems to know the amount of operations needed (how many need to be created) my callback doesn't seem to get called. Here is its code:

function mymodule_import_create_node($new_node) {
  $clean_body = make_safe($new_node['CONTENT_BODY']['_v']);

  $node = new stdClass();
  $node->type = 'article';
  node_object_prepare($node);

  $node->title = substr(strip_tags($clean_body, 0, 20)) . '...';
  $node->body['und'][0]['value'] = $clean_body;
  $node->status = 1;
  $node->field_article_category = array();
  $node->field_article_location = '';
  $node->field_email_address = '';
  $node->uid = 1;

  node_save($node);

  $context['results']['articles_imported']++;
}

I put a few dumps at the start of the batch callback and it never exits. Watchdog indicates nothing, and no new nodes appear.

3 Answers 3

3

According to the Batch operations page on api.drupal.org, the processing function needs to have $context passed by reference.

Thus your import create node function should read function mymodule_import_create_node($node, &$context) and that should do it

Edit: sample code

0

You have to include node.pages.inc in your function to make node_object_prepare() work.

module_load_include('inc', 'node', 'node.pages');
0

This Problem happens when we put our file in different folder like as includes/form_test.inc.In this scenario batch function can't call.So to handle this we put 'file' => 'path_to_file_containing_myfunctions' in our submit function.

$batch = array(
  'title' => t('Custom File uploader'),
  'operations' => array(
    array('test_feeds_import_form_batch', array($uri)),
  ),
  'progress_message' => t('Uploading Multiple Products...'),
  'error_message' => t('Error!'),
  'finished' => 'test_feeds_import_form_batch_finish',
  'file' => drupal_get_path('module', 'test') . '/includes/test_form.inc',
);

batch_set($batch);
batch_process('admin/import-data'); //Work like as drupal goto

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.