以编程方式在Drupal 7中添加评论

时间:2011-11-24 01:34:54

标签: drupal 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@example.com';
$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);

代码导致以下错误:

  

致命错误:调用未定义的函数 node_load()   第1455行的BLA / BLA / comment.module

node_load()函数位于节点模块中,当然已启用。

如何解决?

谢谢!

1 个答案:

答案 0 :(得分:3)

试试这样:

  $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);