Batch-API:我在这段代码中做错了啥?
Posted
技术标签:
【中文标题】Batch-API:我在这段代码中做错了啥?【英文标题】:Batch-API: what am I doing wrong in this code?Batch-API:我在这段代码中做错了什么? 【发布时间】:2013-01-11 13:49:57 【问题描述】:我正在尝试开发一个模块来一次插入很多节点,我想使用 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;
【问题讨论】:
if($node = node_submit($node))
可能评估为假。您是否尝试过实际调试它?
不幸的是,如果我注释掉该行,我会得到相同的结果。
我感觉无论如何都不叫函数'create_node'。
开始拨打watchdog()
来了解您的脚本的进度。您可以在报告 -> 最近的日志消息页面中查看使用该功能记录的消息
确认批处理过程不会随时调用“create_node”函数。我在报告中发现了这种信息:无法生成位于 public://styles/32_32/public/images/gdce/immagine_guida_gdc08_francesco_vezzoli_per_web_0.jpg 的派生图像。并且推荐人是'mysite.org/batch?op=start&id=2206'
【参考方案1】:
您必须在函数中包含 node.pages.inc 才能使 node_object_prepare() 工作。
module_load_include('inc', 'node', 'node.pages');
【讨论】:
以上是关于Batch-API:我在这段代码中做错了啥?的主要内容,如果未能解决你的问题,请参考以下文章