简体   繁体   English

如何在Drupal中的Cron作业中创建节点?

[英]How do I create a node from a cron job in drupal?

In a custom module for drupal 4.7 I hacked together a node object and passed it to node_save($node) to create nodes. 在drupal 4.7的自定义模块中,我将一个节点对象砍在一起,并将其传递给node_save($ node)来创建节点。 This hack appears to no longer work in drupal 6. While I'm sure this hack could be fixed I'm curious if there is a standard solution to create nodes without a form. 该hack似乎在drupal 6中不再起作用。虽然我确定可以修复此hack,但我很好奇是否存在标准的解决方案来创建不带表单的节点。 In this case the data is pulled in from a custom feed on another website. 在这种情况下,数据是从另一个网站上的自定义提要中提取的。

The best practices method of making this happen is to utilize drupal_execute. 实现此目标的最佳实践方法是利用drupal_execute。 drupal_execute will run standard validation and basic node operations so that things behave the way the system expects. drupal_execute将运行标准验证和基本节点操作,从而使事情按系统预期的方式运行。 drupal_execute has its quirks and is slightly less intuitive than simply a node_save, but, in Drupal 6, you can utilize drupal_execute in the following fashion. drupal_execute具有其怪癖,并且比简单的node_save直观性要差一些,但是,在Drupal 6中,您可以按以下方式使用drupal_execute。


$form_id = 'xxxx_node_form'; // where xxxx is the node type
$form_state = array();
$form_state['values']['type'] = 'xxxx'; // same as above
$form_state['values']['title'] = 'My Node Title';
// ... repeat for all fields that you need to save
// this is required to get node form submits to work correctly
$form_state['submit_handlers'] = array('node_form_submit');

$node = new stdClass();
// I don't believe anything is required here, though 
// fields did seem to be required in D5

drupal_execute($form_id, $form_state, $node);

node_save() still works fine in Drupal 6; node_save()在Drupal 6中仍然可以正常工作; you'll need a couple of specific pieces of data in place to make it work. 您需要准备一些特定的数据才能使其正常工作。

$node = new stdClass();
$node->type = 'story';
$node->title = 'This is a title';
$node->body = 'This is the body.';
$node->teaser = 'This is the teaser.';
$node->uid = 1;
$node->status = 1;
$node->promote = 1;

node_save($node);

'Status' and 'Promote' are easy to overlook -- if you don't set those, the node will remain unpublished and unpromoted, and you'll only see if you go to the content administration screen. “状态”和“升级”很容易被忽略-如果您未设置它们,则该节点将保持未发布和未升级状态,并且仅在转到内容管理屏幕时才能看到。

I don't know of a standard API for creating a node pragmatically. 我不知道用于实用地创建节点的标准API。 But this is what I've gleaned from building a module that does what you're trying to do. 但这就是我从构建一个可以执行您要执行的操作的模块中收集到的。

  1. Make sure the important fields are set: uid, name, type, language, title, body, filter (see node_add() and node_form() ) 确保设置了重要字段:uid,名称,类型,语言,标题,正文,过滤器(请参阅node_add()node_form()
  2. Pass the node through node_object_prepare() so other modules can add to the $node object. 通过node_object_prepare()传递节点,以便其他模块可以添加到$ node对象。

One more answer I discovered was to use the example from the blogapi module in drupal core. 我发现的另一个答案是使用drupal核心中blogapi模块中的示例 The fact that it is in core gives me a bit more confidence that it will continue to work in future versions. 它处于核心地位这一事实使我更有信心,它将在将来的版本中继续使用。

There are some good answers above, but in the specific example of turning an ingested feed item into a node, you could also take the approach of using the simplefeed module ( http://wwww.drupal.org/project/simplefeed ). 上面有一些不错的答案,但是在将摄取的提要项变成节点的特定示例中,您还可以采用使用simplefeed模块的方法( http://wwww.drupal.org/project/simplefeed )。 This module uses the simplepie engine to ingest feeds and turns individual items from each feed into nodes. 该模块使用simplepie引擎来提取提要,并将每个提要中的单个项目转换为节点。 I realize that this doesn't specifically address the issue of creating nodes from cron, but it might be an easier solution to your problem overall. 我意识到这并没有专门解决从cron创建节点的问题,但它可能是解决您总体问题的一种更简单的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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