I have a view page on which I have rendered the comment form for each node in the field using the code below:

  $comment = new stdClass;
  $comment->nid = $n->nid;
  $form = drupal_get_form('comment_form', $comment);
  print render($form);

All my forms are printing fine, but the issue is that when I enter a message on message field and click submit, it redirect me to node page with comment form at browser top and my message is lost. If I type and click save here, my message saves without an issue.

I altered the form, added a custom_submit_handler as follows:

$form['#submit'][] = 'mymodule_custom_submit';

and defined it as:

function sparx_custom_submit($form, &$form_state) {
 if(isset($form_state['custom_redirect_path'])) {
   $form_state['redirect']  = $form_state['custom_redirect_path'];

Now the problem I am facing is that each time I try the comment is getting added to the top-most node, not to any other node.

Any suggestions please

1 Answer 1


Using the following code to print comment helped me to solve this issue:

module_load_include('inc', 'comment', 'comment.pages');
$comment_form = drupal_get_form('comment_node_<content_type>_form', (object) array('nid' => $nid));
print drupal_render($comment_form);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.