Build a Blog in Drupal 8: Adding Comments

This tutorial is part of the “Build a Blog in Drupal 8” series:

  1. Content types and Fields
  2. Adding Comments
  3. Using Views
  4. Managing Blocks
  5. Create and Manage Menus
  6. Custom Contact Forms

If you want to communicate with readers on a blog, the best way to achieve this is by setting up comments. If you’re happy to deal with spam and the occasional troll, then comments are great for communicating with readers and fostering a community.

The functionality has changed a lot in Drupal 8. In Drupal 7, the comments system was fairly rigid. You could only use them on content types and only have a single comment type. If you needed to support multiple comment types like review or feedback, you were out of luck.

In Drupal 8, the comments system has been rebuilt. They can be attached to any entity type by adding a “Comments” field. You can also now have different comment types. This allows you to do all sorts of things like having public and private comments on a single content type.

In the last tutorial, we created a Blog content type and added fields to it. Let’s continue working on it by adding support for comments. We’ll configure the user permissions so that comments can be submitted anonymously, but will need to be approved before they’re published.

Add Comment Field

As mentioned in the introduction, the comment system has changed quite considerably in Drupal 8. Instead of configuring the comments from the vertical tabs on a content type edit form, everything is done through a field.

1. Click on Structure in the toolbar, “Content types” and click on “Manage fields” on the Blog row. Now click on “Add field”.

Fig 1.0

2. In Drupal’s Standard install profile, it creates a comment field and attaches it to the Article content type. We’ll re-use this field instead of creating another one.

3. Select “Comments: comment” from “Re-use an existing field” and click on “Save and continue”.

Fig 1.1

4. From the field edit page, you can configure the default value; if the comments should be open, closed or hidden and display settings. If you’ve used Drupal 7, these settings will look familiar.

Fig 1.2

Once you’re happy click on “Save settings”.

Comment Types

New in Drupal 8 is the ability to have different comment types. This is the same concept as content types. The Standard installation profile creates a “Default comments” comment type, which is used for general comments.

To create and manage comment types, just go to Structure and click on “Comment types”.

Fig 1.3

Being able to create different types is powerful and this makes the comment module very flexible. Let’s say for example you want to allow users to leave a review, you could then create a comment type called Review with its own fields.

Anonymous Comments

If you now go to your Drupal site as an anonymous user you’ll see that you can’t comment without registering or logging in. Requiring users to register creates a big barrier to entry and sometimes it make sense to allow anonymous comments, but that’s a choice you need to make.

Fig 1.4

If you do decide to allow anonymous comments make sure you install Honeypot and Mollom. The modules will help to combat spam, it won’t eliminate them totally, but it’ll get most spam comments. Both modules have Drupal 8 versions, but I haven’t tested them so let me know how you go in the comments below.

Let’s now allow anonymous comments.

1. Go to People and click on the Permissions tab.

2. Scroll to the Comment section. Check “Post comments” for anonymous users and make sure “Skip comment approval” stays unchecked. This will allow anonymous users to submit comments but they’ll need to be approved.

Fig 1.5

Scroll to the bottom and click on “Save permissions”.

Approve Comments

When an anonymous user submits a comment, they receive the following message:

Fig 1.6

To approve comments, simply go to Content, Comments and click on the “Unapproved comments” tab. Check the comment you want to approve, select “Publish the selected comments” and click on Update.

Fig 1.7

Now the comment will be published and visible on the “Published comments” page. If you want to give another user access to moderate the comments then assign them the “Administer comments and comment settings” permission.

Summary

The new comments system in Drupal 8 is very good. Now that comments can be added on any entity type and you can have different comment types, I’ll be curious to see in what innovative way they’ll be used.

In the next tutorial, you’ll learn how to create a listing of blog posts using Views.

How would you use the new comments system in Drupal 8? Tell us in the comments below.

Ivan Zugec

About Ivan Zugec

Ivan is the founder of Web Wash and spends most of his time consulting and writing about Drupal. He's been working with Drupal for 10 years and has successfully completed several large Drupal projects in Australia.

15 thoughts on “Build a Blog in Drupal 8: Adding Comments”

  1. Hello Ivan, nice D8 article series! Following.
    My first thought on other use cases after “Reviews” is “Attend to something”. I believe this is also called “RSVP” for events. For instance: Offer limited seats or tickets. Set and display available total tickets. Maybe calculate available seats with total# in parent content type and submitted comments etc.

  2. Hello Ivan, thanks for your examples.
    When I publish a blog, the subject for all comments is clear to me, namely the subject of my blog. So I prefer to hide this subject-field for all visitors. But I can’t find a way to do this. It seems to me that Drupal has decided that this field is always necesarry. Is there a solution from the back-end of Drupal? At this moment I use rc4. Or do I have to make some modifications in css or php?

    1. Hi Harro,

      The Subject text field can now be hidden by disabling it from the “Manage form display” page. Just go to “/admin/structure/comment/manage/comment/form-display” and move the field to the Disabled area. This will remove the Subject field from the comment form.

      However, you’ll still need to hide the Subject field when the comment is displayed. You can do this by modifying the comment template or by using CSS.

  3. Hi Ivan,

    Thanks for the great tutorial. I recently finished upgrading to D8. Is there yet any way of receiving email notifications of comments awaiting approval?

  4. Hello,

    How to display name of the user who posted comment on node details page in comment listing? is there any settings?

    Thanks,

    1. Hi Hiraman,

      Unless I’ve misunderstood your question, Drupal already does this.
      If a user is logged in, their username will be displayed on the comment. If not logged in, they’ll see a “Your name” field.

      Cheers,
      Ivan

    1. Hi monelgq,

      How to theme Comments & Comments form?

      Really depends on what you want to do. A lot can be achieved through CSS.

      Just style the form like you would style any other HTML form.

      Cheers,
      Ivan

  5. Hi Ivan,

    I add a blog but I did not see any comment field prebuilt when I tried to add this field. Could you please show me how we can add this field from the beginning?

    Thank you very much!

    Silvia

    1. Hi Silvia,

      The prebuilt field appears if you installed Drupal using the Standard installation profile. If it’s not there then just create another comment field.

      Cheers,
      Ivan

      1. Hi Ivan,
        So I was able to add comment field to my blog. However, I don’t see any submit button and the text format of the comment field is very weird but I don’t know ho to reformat it. Could you please help me with this? Thank you very much!
        Silvia

        1. Hi Silvia,

          Have you done any customisation to the site?

          Make sure the following has been configured:

          1. Make sure the Comments formatter is being displayed on the “Manage display” on the content type
          2. Make sure the “Form display” on the Comment type (/admin/structure/comment) has all the fields enabled

          Hope this helps.

          Cheers,
          Ivan

          1. Thanks Ivan! Sorry about my late response. I don’t receive any notification when you respond so just have to check back later. I will check those.

Leave a Comment

You have to agree to the comment policy.