7

I'm trying to print a comment form with the following code.

<?php 
    print drupal_get_form('comment_form', array('nid' => $row->nid), t('Post new comment')); 
?>

All I get is the following error repeated 40 times:

Warning: Attempt to assign property of non-object in comment_form() (line 1845 of /home/admin/public_html/website.com/modules/comment/comment.module).`

The comment form is called from inside a PHP Views field. What is the proper way to add a comment for to a node in Drupal 7?

There are actually a lot more errors, most of which repeat at least once:

EntityMalformedException: Missing bundle property on entity of type comment. in entity_extract_ids() (line 7501 of /home/admin/public_html/website.com/includes/common.inc). Notice: Trying to get property of non-object in comment_form() (line 1854 of /home/admin/public_html/website.com/modules/comment/comment.module).

1 Answer 1

12

comment_form() is expecting the parameter to be an object rather than an array so you should be doing something like this:

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

Note the lack of a 3rd parameter passed to drupal_get_form(), comment_form() only accepts the comment as a parameter, if you want to add a title you'll have to print that separately.

Finally drupal_get_form() returns a render array rather than a string so you need to pass it through the render() function:

print render($form);

The extra errors you're getting are just side-effects of passing an array rather than an object to the comment_form() function.

1
  • Thanks man... some of this documentation is rather cryptic
    – emc
    Commented Feb 5, 2012 at 1:10

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