5

I've created a views from comments. I want to have add comment form at the button of my views (in footer). so I installed "views_php" module and add "Global: PHP" to footer and write this code to print add comment form in footer.

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

but the problem is that I don't have comment_body or any other custom field in the printed form. actually just author, subject and save button.

I tried this code too:

print drupal_render(drupal_get_form("comment_form", (object) array('nid' => $data->nid)));

or

print drupal_render(drupal_get_form("comment_node_news_form", (object) array('nid' => $results[0]->nid)));

but none of them worked properly!

5
<?php
$comment = new stdClass;
$comment->nid = $row->nid;
$form = drupal_get_form('comment_form', $comment);
print render($form);
?>

Works for me. Make sure that your Content: Nid field is ABOVE the Global: PHP field, otherwise (when it's below it) $row->nid is not available as a variable.

enter image description here

Update

After reading your Q more carefully, since you want it in the footer then you need to specify the node id of the form. (Under what node this comment will be stored) For Example: /node/1

<?php
$comment = new stdClass;
$comment->nid = 1;
$form = drupal_get_form('comment_form', $comment);
print render($form);
?>

If you want /node/5 you change this line to $comment->nid = 5;

OR

If you want this to be dynamic, the nid to be based on the page you're viewing. Then use

<?php
if ($node = menu_get_object()) {
$comment = new stdClass;
$comment->nid = $node->nid;
$form = drupal_get_form('comment_form', $comment);
print render($form);
}
?>

Note: The form will not render/show on views preview, but it will show up when you visit the node.

| improve this answer | |
  • Updated my answer. – No Sssweat Feb 14 '16 at 8:13
  • 2
    that's true appearently but does not work properly. actually when I post the comment redirect to comment/reply/[nid] page and the comment is not posted too. – shekoufeh Feb 14 '16 at 12:04
  • I am facing the same issue. drupal.stackexchange.com/questions/204663/… – Dev Jun 20 '16 at 12:34

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