-1

I can't seem to pass any info to the 'finished' function of this batch operation. Everything works in terms of data getting saved correctly but I get the following error message due to the 'finished' function looking for things that don't exist.

Notice: Undefined index: created in my_csv_import_finished() (line 280 of /var/www/html/mysite.com/sites/all/modules/custom/my_csv/my_csv.module).

Notice: Undefined index: updated in my_csv_import_finished() (line 281 of /var/www/html/mysite.com/sites/all/modules/custom/my_csv/my_csv.module).

And that's because there is no $success, $results, or $operations being passed to my_csv_import_finished

The $context variable is indeed being passed to the $operations function but from what I can tell, there is nothing there that would give me access to any sort of 'created' and/or 'updated' key of this $contexts array. What am I doing wrong?

function my_csv_import_form_submit($form, $form_state) {
  // Check to make sure that the file was uploaded to the server properly
  $uri = db_query("SELECT uri FROM {file_managed} WHERE fid =:fid", array(
                   ':fid' => $form_state['input']['import']['fid'],
                  ))->fetchField();
 if(!empty($uri)) {
   if(file_exists(drupal_realpath($uri))) { 
     // Open the csv
     $handle = fopen(drupal_realpath($uri), "r");
     $fp = file($uri);
     $total_rows = count($fp);


     // this is somewhat of a hack to skip the first line (headers)
     fgetcsv($handle, 0, ',', '"');

     while (($data = fgetcsv($handle, 0, ',', '"')) !== FALSE) {
     $operations[] = array('my_csv_import_batch_processing', array($data), 
    );
  }

    $batch = array(
      'title' => t('Importing CSV...'),
      'operations' => $operations, array($data),  
      'finished' => 'my_csv_import_finished', 
      'error_message' => t('The installation has encountered an error.'),
      'progress_message' => t('Processed @current of @total rows.'),
    );
    batch_set($batch);

    fclose($handle);    
  }
 }

function my_csv_import_batch_processing($data, &$context) {
  watchdog('$context', print_r($context,TRUE));

  .....
}

What am I supposed to use here?

$context looks like this:

[0] => Array (
            [sandbox] => Array (
                )
            [results] => Array (
                )
            [finished] => 1
            [message] => ''
        )

If I've indeed successfully updated a node, where is this info? And how do I get/find it?

Is it something to do with the structure of what's being passed to the operations function?

  • Frank, you can add an answer to your own questions. That is preferred over editing the answer into the question itself. – Neograph734 Sep 21 '17 at 14:20
0

Soon after posting this I figured out that one can simply pass things into the $context['results'] array like so:

$context['results'][].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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