Search code examples
drupaltwigconventionstheming

Drupal theme naming conventions for comment types


I set up a Drupal page, which has two different content-types. One is "articles", one is "reviews".

In my theme folder, I set up two node templates for these content types. node.html.twig (standard template - articles), and node--reviews.html.twig. When I add an article, node.html.twig is used, when I add a review, node--reviews.html.twig is used.

Now, it's possilbe to have different comments types too. I want to add the standard comment (comment.html.twig) for articles, and a custom comment type to reviews.

In Drupals theme naming documentation it's written, to name a template override like this: comment--node-type.html.twig. So I named my comment template comment--node-reviews.html.twig.

Is there anything else I have to do - apart from clearing the cache? Because I simply cant get it to run.


Solution

  • Enable Twig Debugging, then you'll get template suggestions printed as HTML comments directly into the markup.

    I did that and got the following output:

    <!-- THEME DEBUG -->
    <!-- THEME HOOK: 'comment__field_my_comment_field__article' -->
    <!-- FILE NAME SUGGESTIONS:
       * comment--field-my-comment-field--article.html.twig
       * comment--field-my-comment-field.html.twig
       x comment.html.twig
    -->
    <!-- BEGIN OUTPUT from 'core/themes/classy/templates/content/comment.html.twig' -->
    

    So, the templates are supposed to be named like the following pattern:

    comment--field-name-of-the-comment-field-in-the-content-type--content-type.html.twig
    comment--field-name-of-the-comment-field-in-the-content-type.html.twig