1

What are some ways of rendering a comment form without also displaying the comments?

What about creating a block contains the form? If that's possible, what are some links to documentation touching on that?

1 Answer 1

3

To display comment form without comments list, you need to override field--comment.html.twig template, and remove the following lines:

  {% if comments and not label_hidden %}
    {{ title_prefix }}
    <h2{{ title_attributes.addClass(title_classes) }}>{{ label }}</h2>
    {{ title_suffix }}
  {% endif %}

  {{ comments }}

to override it:

  1. From your theme, just create one of the following twig file in you templates folder, and put in it field--comment.html.twig content without the above section.

   * field--node--comment--article.html.twig
   * field--node--comment.html.twig
   * field--node--article.html.twig
   x field--comment.html.twig
   x field--comment.html.twig
   * field.html.twig

Note: the above suggestions for article content type, you may have different suggestions.

  1. From your module, implement hook_theme for one of the suggestions like the following:

/**
 * Implements hook_theme().
 */
function MODULENAME_theme($existing, $type, $theme, $path) {
  return [
    'field__node__comment__article' => [
      'base hook' => 'field'
    ],
  ];
}

And make your changes in MODULENAME/templates/field--node--comment--article.html.twig

2
  • 1
    Thanks, @berramou! This worked after I changed "hook" to "base hook" in hook_theme().
    – htoip
    Commented May 1, 2020 at 1:11
  • welcome, glad that helped 👍
    – berramou
    Commented May 1, 2020 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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