Add comment programmatically in Drupal 7

trying to create a comment in its own module.

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

The code causes the following error:

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

The node_load () function is located in the node module, which, of course, is included.

How to fix it?

Thanks!

+4
source share
1 answer

Try it like this:

  $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); 
+2
source

Source: https://habr.com/ru/post/1382802/


All Articles