I have a comment form which gets rendered below the content type on content page. I have a comment form and comments being rendered as well. Now the question is that I have a custom form that takes some text and I need to save this to content type's comments.

How can I do this? I have tried comment_save($comment) and it simply breaks my redirection and also the comments are not getting saved.

Here is what I have tried.

function quiz_commenting_form_submit($form, &$form_state) {
global $user;

$data = $form_state['values']['qcomment'];
$qnid = $form_state['values']['qnid'];
$form_state['redirect'] = 'some-url'; // hardcoding this untill i get url for quiz in form

// Let's create a managed object
$comment = new stdClass(); // We create a new comment object
$comment->nid = $qnid; // 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 = $user->uid; // user's id, who left the comment
$comment->mail = $user->email; // 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->body['und'][0]['value'] = $data; // Everything here is pretty much like with a node

comment_form_submit($comment); // saving a comment
  • Could you please attach the code you have used for comment_save() unction? – Alireza Tabatabaeian Sep 21 '15 at 21:33
  • Sure please check the updated question @AlirezaTabatabaeian – Prerit Mohan Sep 21 '15 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.