简体   繁体   中英

Batch-API: what am I doing wrong in this code?

I am trying to develop a module to insert a lot of node at one time and I would like to use Batch-API to display the progress of operations.

I read the example into 'Example' module and I wrote this code.

But don't made anything. I can see the progress bar go head but it don't save any node.

Can anyone help me on this?

function custom_content_archive_import_contents_submit($form, &$form_state) {
    $file=$form_state['values']['file'];
    unset($form_state['values']['file']);
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);
    drupal_set_message(t('The file @filename was uploaded successfully.', array('@filename' => $file->filename)));

    ini_set('auto_detect_line_endings',TRUE);
    $path = drupal_realpath($file->uri);
    $importer = new CsvImporter($path, true, variable_get('custom_content_archive_file_delimiter'), variable_get('custom_content_archive_file_enclosure'));
    $data = $importer->get();
    ini_set('auto_detect_line_endings',FALSE);

    $_SESSION['http_request_count'] = 0; // reset counter for debug information.
    $all_data = array(
        'ctype' => $form_state['values']['list'],
        'data' => $data,
        'user' => $form_state['values']['user'],
        'lang' => $form_state['values']['languages'],
    );
    batch_set(start_batch_creation_nodes($all_data));
}

function start_batch_creation_nodes($all_data) {
    $num_operations = count($all_data['data']);

    $operations = array();
    $i = 0;
    // leggo il file uploadato e creo i nodi
    foreach ($all_data['data'] as $node) {
         $operations[] = array('create_node', 
                          array($node,
                                $all_data['ctype'],
                                $all_data['user'],
                                $all_data['lang'],
                                t('(Operation @operation)', array('@operation' => $i))
                          )
         );
         $i++;
    }

    $batch = array(
        'operations' => $operations,
        'finished' => 'custom_content_archive_import_contents_finished',
    );
    return $batch;
}

function create_node($arrnode, $ctype, $user, $lang, $operation_details, &$context) {
    $node = new stdClass(); // Create a new node object
    $node->type = $ctype; // Or page, or whatever content type you like
    node_object_prepare($node); // Set some default values
    $node->uid = $user; // UID of the author of the node; or use $node->name
    $node->language = $lang; // Or e.g. 'en' if locale is enabled
    $node->promote = 0;

    foreach ($arrnode as $field => $value){
        switch ($field) {
            case 'title':
                $node->title = $arrnode['title'];
                break;
            case'body':
                $node->body[LANGUAGE_NONE][0]['value'] = nl2br($arrnode['body']);
                $node->body[LANGUAGE_NONE][0]['summary'] = text_summary($bodytext);
                $node->body[LANGUAGE_NONE][0]['format'] = 'filtered_html';
                break;
            default:
                $arrfield = field_info_field($field);
                switch ($arrfield['type']) {
                    case 'datetime':
                         $my_date = new DateTime($value);
                         $node->{$field}[LANGUAGE_NONE][0][value] = date_format($my_date, 'Y-m-d H:i:s');
                         break;
                    case 'text':
                         $node->{$field}[LANGUAGE_NONE][0][value] = $value;
                         break;
                    case 'email':
                         $node->{$field}[LANGUAGE_NONE][0][email] = $value;
                         break;
                    case 'link_field':
                         $node->{$field}[LANGUAGE_NONE][0][url] = $value;
                         break;
                }
        }
    }

    if($node = node_submit($node)) { // Prepare node for saving
        node_save($node);
        workflow_execute_transition($node, 3, $comment = NULL, $force = TRUE);
        // Store some results for post-processing in the 'finished' callback.
        // The contents of 'results' will be available as $results in the
        // 'finished' function (in this example, batch_example_finished()).
        $context['results'][] = $node->nid . ' : ' . check_plain($node->title);

        // Optional message displayed under the progressbar.
        $context['message'] = t('Saving node "@title"', array('@title' => $node->title)) . ' ' . $operation_details;

        _custom_content_archive_import_contents_update_http_requests();
    }

}

function custom_content_archive_import_contents_finished($success, $results, $operations) {
    if ($success) {
        // Here we could do something meaningful with the results.
        // We just display the number of nodes we processed...
        drupal_set_message(t('@count results processed in @requests HTTP requests.', array('@count' => count($results), '@requests' => _custom_content_archive_import_contents_get_http_requests())));
        drupal_set_message(t('The final result was "%final"', array('%final' => end($results))));
    } else {
        // An error occurred.
        // $operations contains the operations that remained unprocessed.
        $error_operation = reset($operations);
        drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE))));
    }
}

function _custom_content_archive_import_contents_update_http_requests() {
    $_SESSION['http_request_count']++;
}

function _custom_content_archive_import_contents_get_http_requests() {
    return !empty($_SESSION['http_request_count']) ? $_SESSION['http_request_count'] : 0;
}

您必须在函数中包含node.pages.inc才能使node_object_prepare()工作。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM