0

我正在尝试开发一个模块来一次插入很多节点,我想使用 Batch-API 来显示操作的进度。

我将示例读入“示例”模块并编写了此代码。

但不要做任何事。我可以看到进度条往前走,但它不保存任何节点。

谁可以帮我这个事?

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;
}
4

1 回答 1

0

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

module_load_include('inc', 'node', 'node.pages');
于 2014-01-28T09:22:14.577 回答