Hi all,

I have code for creating comment programmatically in Drupal 6 and it's works. Since i want to move into Drupal 7, now my code doesn't work and it's give error. Here are my code in Drupal 7 :

global $user;
$user = user_authenticate($account['mail'],$account['pass']);

/** POST CONTENT COMMENT */
$getcomments = array();
$getcomments['values']['author'] = $author;
$getcomments['values']['subject'] = $subject;
$getcomments['values']['comment'] = $cleanComment;
$getcomments['und'][0]['comment'] = $cleanComment;
$getcomments['und'][0]['format'] = 'full_html';
$getcomments['values']['nid'] = $node->nid;
$getcomments['values']['uid'] = $userUID;
$getcomments['values']['pid'] = 0;
$getcomments['values']['op'] = t('Save');
$getcomments['values']['timestamp'] = $commentTimestamp;
$getcomments['values']['language'] = 'und';
$getcomments['values']['status'] = COMMENT_PUBLISHED;

comment_form_submit("comment_form", $getcomments);
session_destroy();

$null = NULL;
user_module_invoke('logout', $null, $user);

// Load the anonymous user
$user = drupal_anonymous_user();

I tried with comment_form_submit("comment_node_article_form", $getcomments); but it's doesn't work.

I hope somebody can tell me how to make this work as in Drupal 6.

Thanks

Comments

yoodey’s picture

I just solved this problem in 2 hours after asking. Thanks

aschiwi’s picture

And could you post your solution?

venkatd’s picture

Could you please post your solution? Drupal is aggravating me!

ThomasKaemmerling’s picture

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

Found the solution on
comm-press.de

josh@pixael.com’s picture

Thanks mate it was useful.

Nikdhil Mdohfan’s picture

Let's create a managed object for new comment.

$comment = new stdClass();
Then add the objects for the comment.

$comment->nid = $node->nid; // nid of a node you want to attach a comment to
$comment->cid = 0; // leave it as is
$comment->pid = 0; // parent comment id, 0 if none 
$comment->uid = 1; // user's id, who left the comment
$comment->mail = 'email@example.com'; // user's email
$comment->name = 'User name'; // If user is authenticated you can omit this field, it will be auto-populated, if the user is anonymous and you want to name him somehow, input his name here
$comment->thread = '01/'; // OPTIONAL. If you need comments to be threaded you can fill this value. Otherwise omit it.
$comment->hostname = '127.0.01' // OPTIONAL. You can log poster's ip here
$comment->created = time(); // OPTIONAL. You can set any time you want here. Useful for backdated comments creation.
$comment->is_anonymous = 0; // leave it as is
$comment->homepage = ''; // you can add homepage URL here
$comment->status = COMMENT_PUBLISHED; // We auto-publish this comment
$comment->language = LANGUAGE_NONE; // The same as for a node
$comment->subject = 'Comment subject'; 
$comment->comment_body[$comment->language][0]['value'] = 'Comment body text'; // Everything here is pretty much like with a node
$comment->comment_body[$comment->language][0]['format'] = 'filtered_html'; 
$comment->field_custom_field_name[LANGUAGE_NONE][0]['value'] = ‘Some value’; // OPTIONAL. If your comment has a custom field attached it can added as simple as this // preparing a comment for a save

Call the comment_submit and comment_save function to create the comments

comment_submit($comment); // saving a comment
comment_save($comment);

Also you can read full code HERE

malcolmp’s picture

Thanks to all.

Also, I needed to do
$comment->comment_body[$comment->language][0]['format'] = 3

bobn’s picture

Thanks for that, worked perfectly apart from the setting of the 'thread' property. If you want threading, this must be '01/' for the first comment in a thread only, thereafter '', otherwise comment_save sets the thread property to '01/' for every comment.

sunwukong’s picture

Working with comments recently.

dalinian’s picture

Just wanted to note that you cannot set $comment->hostname in this manner. comment_save() adds hostname without any checks to see if it's already set.

      // Add the values which aren't passed into the function.
      $comment->thread = $thread;
      $comment->hostname = ip_address();

The only way I see around this problem is to do a db_update after comment_save(). Something like:

   ...
    comment_save($comment);
    db_update('comment')
      ->fields(array(
          'hostname' => 'CORRECT_HOSTNAME',
        ))
      ->condition('cid', $comment->cid)
      ->execute();
redjumpsuit’s picture

this was my problem before, i didn't get to think of your solution ;-) my workaround was to mass update the comment table with the correct ip address from the source table (which i know now is less elegant vs your suggestion.)

prsnjtbarman’s picture

Thank you. this code works for me

lukasss’s picture

I think it would be useful about comment date: $comment->created not working if not $comment->date
I see it: https://api.drupal.org/api/drupal/modules!comment!comment.module/functio...

For world peace!