user3425625
user3425625

Reputation: 17

How to embed Comment on Custom Template in Drupal 8 Twig

I have a content type named Project and I created a template for it page--project.html.twig I am new with Drupal and I want to include a comment box and comment list on this page but not sure what to do. I already added a comment field on my content type. I tried rendering it using {{ node.field_comments }} but I am getting error. How can I include a comment on a

Upvotes: 0

Views: 599

Answers (1)

Kien Nguyen
Kien Nguyen

Reputation: 2691

Because variable node is not available in template page so you have to get field comment, render it and then append it to your template by implementing hook_preprocess_page:

<your_theme>.theme

/**
 * Implements hook_preprocess_HOOK().
 */
function <your_theme>_preprocess_page(array &$variables) {
  $node = \Drupal::routeMatch()->getParameter('node');
  if (!empty($node)) {
    $variables['field_comment'] = $node->comment->view('full');
  }
}

page--project.html.twig

{{ field_comment }}

Upvotes: 1

Related Questions