Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By yoodey on
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
Nevermind
I just solved this problem in 2 hours after asking. Thanks
And could you post your
And could you post your solution?
Could you please post your
Could you please post your solution? Drupal is aggravating me!
Solution: comments programmatically in Drupal 7
Found the solution on
comm-press.de
Thanks mate it was useful.
Thanks mate it was useful.
How to programmatically create a comment in drupal 7
Let's create a managed object for new comment.
$comment = new stdClass();
Then add the objects for the comment.
Call the comment_submit and comment_save function to create the comments
Also you can read full code HERE
Thanks to all. Also, I needed
Thanks to all.
Also, I needed to do
$comment->comment_body[$comment->language][0]['format'] = 3
$comment->thread
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.
Thanks. I need it.
Working with comments recently.
Can't set $comment->hostname
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.
The only way I see around this problem is to do a db_update after comment_save(). Something like:
had the same issue
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.)
It works for me
Thank you. this code works for me
$comment->date
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!