I'm trying to port a theme from Drupal 6 to Drupal 8, and I find code like the following.

$edit = array();
$edit['nid'] = $node->nid;
$form = drupal_get_form('comment_form', $edit));

How do I load the form in Drupal 8? I get the following error:

Fatal error: Call to a member function getEntityTypeId() on null in /www/site/core/lib/Drupal/Core/Entity/EntityForm.php on line 77

The line causing the error is the following one:


I can't work out how to pass the node object to the form builder. How can I do it?


FormBuilder::getForm() doesn't accept just an argument; after the first, the other arguments are:

Any additional arguments are passed on to the functions called by \Drupal::formBuilder()->getForm(), including the unique form constructor function. For example, the node_edit form requires that a node object is passed in here when it is called. These are available to implementations of hook_form_alter() and hook_form_FORM_ID_alter() as the array $form_state->getBuildInfo()['args'].

So, \Drupal::formBuilder()->getForm('Drupal\comment\CommentForm', $node); would be perfectly fine. It's up to the form builder, using those arguments.

  • Thanks! I figured it was something like that. I looked in the source code for CommentForm but don't see any clues about the expected parameters. How do I found out?
    – Nic
    Jun 13 '16 at 13:23
  • 2
    In fact passing $node this gives me the same exception on the same line.
    – Nic
    Jun 13 '16 at 16:33

kiamlaluno's solution didn't work for me. This did:

$values = array(
    'entity_type'  => 'node',
    'entity_id'    => $node->id(),
    'field_name'   => 'comment',
    'comment_type' => 'comment',
    'pid' => NULL,

$comment = \Drupal::entityTypeManager()->getStorage('comment')->create($values);
$formHTML  = \Drupal::service('entity.form_builder')->getForm($comment);

Example in core using lazy builder approach to make it work properly with caching per permission


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.