In Drupal 7, comment form can be rendered using the following code.

drupal_get_form("comment_node_{$node->type}_form", (object) array('nid' => $node->nid));

How can I achieve the same in Drupal 8?

1 Answer 1


See https://api.drupal.org/api/drupal/core%21modules%21comment%21src%21CommentLazyBuilders.php/function/CommentLazyBuilders%3A%3ArenderForm/8.2.x.

You create a comment entity and then get its form.

$values = array(
    'entity_type' => $commented_entity_type_id,
    'entity_id' => $commented_entity_id,
    'field_name' => $field_name,
    'comment_type' => $comment_type_id,
    'pid' => NULL,
$comment = \Drupal::entityTypeManager()->getStorage('comment')->create($values);
$form = \Drupal::service('entity.form_builder')->getForm($comment);

Your Answer

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

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