Greetings,

Is it possible to render a comment form in a view? I have not been able to find anything that works, yet it seems pretty basic. I appreciate any advice. Thanks.

Comments

bshaddad’s picture

Hi,

You can add the below code to views custom template:

<?php $data_to_render = drupal_get_form("comment_node_{$node->type}_form", (object) array('nid' => $node->nid)); ?>
<?php print drupal_render($data_to_render);?>
pyxio’s picture

Hi

Thanks so much for this! Unfortunately, I get the following error though.. is there a solution? Cheers Kevin

Notice: Undefined variable: node in include() (line 62 of /var/www/html/panelize/sites/dixit.panelize.it/themes/dixit/templates/view/views-view--product-detail--view-product-reviews.tpl.php).
Notice: Trying to get property of non-object in include() (line 62 of /var/www/html/panelize/sites/dixit.panelize.it/themes/dixit/templates/view/views-view--product-detail--view-product-reviews.tpl.php).
Notice: Undefined variable: node in include() (line 62 of /var/www/html/panelize/sites/dixit.panelize.it/themes/dixit/templates/view/views-view--product-detail--view-product-reviews.tpl.php).
Notice: Trying to get property of non-object in include() (line 62 of /var/www/html/panelize/sites/dixit.panelize.it/themes/dixit/templates/view/views-view--product-detail--view-product-reviews.tpl.php).
Notice: Undefined index: comment_node__form in drupal_retrieve_form() (line 763 of /var/www/html/panelize/includes/form.inc).
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'comment_node__form' not found or invalid function name in drupal_retrieve_form() (line 798 of /var/www/html/panelize/includes/form.inc).

bshaddad’s picture

Hi,

You need to load the node before:
<?php $node = node_load(arg(1));?>

Where arg(1) is the node id in the url:
example.com/node/1 (if you are trying to print the form in a node's page)

pyxio’s picture

I see. But the problem is there are many nodes. For example, a product page and each one has a comment form like standard drupal page. so the node id will change per page. is there a variable that can be used instead of a fixed node id?

bshaddad’s picture

arg(1) will get the node id from the url, it is not static.

pyxio’s picture

Most excellent! Huge thanks for taking the time to share the code with me. Works perfectly!

mustanggb’s picture

Issue summary: View changes
Status: Active » Closed (outdated)