2011-11-24 62 views
4

试图在自己的模块中创建评论。以编程方式在Drupal 7中添加评论

$comment = new stdClass(); 
$comment->nid = 555; // Node Id the comment will attached to 
$comment->cid = 0; 
$comment->pid = 0; 
$comment->uid = 1; 
$comment->mail = '[email protected]'; 
$comment->name = 'admin'; 
$comment->is_anonymous = 0; 
$comment->homepage = ''; 
$comment->status = COMMENT_PUBLISHED; 
$comment->language = LANGUAGE_NONE; 
$comment->subject = 'Comment subject'; 
$comment->comment_body[$comment->language][0]['value'] = 'Comment body text'; 
$comment->comment_body[$comment->language][0]['format'] = 'filtered_html'; 
comment_submit($comment); 
comment_save($comment); 

该代码使得以下错误:

Fatal error: Call to undefined function node_load() in BLA/BLA/comment.module on line 1455

node_load()函数是在节点模块其中,当然,启用。

如何解决?

谢谢!

+0

奇怪......感动模块文件夹里去了客场制模块目录和错误。解决了! – ymakux

+1

你在哪里放置该代码?我已经在这里测试过了,它工作正常,但是如果node_load未定义,那么您的代码必须在节点模块之前执行。您不应将自定义或社区模块放置在核心模块文件夹中。 – vgardner

回答

2

尝试这样的:

$comment = (object) array(
    'nid' => $node_id, 
    'cid' => 0, 
    'pid' => 0, 
    'uid' => 1, 
    'mail' => '', 
    'is_anonymous' => 0, 
    'homepage' => '', 
    'status' => COMMENT_PUBLISHED, 
    'subject' => 'dsk subject', 
    'language' => LANGUAGE_NONE, 
    'comment_body' => array(
     LANGUAGE_NONE => array(
     0 => array (
      'value' => 'aaa', 
      'format' => 'filtered_html' 
     ) 
    ) 
    ), 
); 

    comment_submit($comment); 
    comment_save($comment); 
相关问题