This tutorial is part of the “Build a Blog in Drupal 8” series:
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”.
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”.
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.
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”.
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.
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.
Scroll to the bottom and click on “Save permissions”.
Approve Comments
When an anonymous user submits a comment, they receive the following message:
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.
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.
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.
That’s a good idea. I didn’t think of event registrations.
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?
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.
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?
Hi Sven,
Look at using the Actions module to send an email when comments awaiting approval. I haven’t used it in D8, but I’ve used it in D7.
Modules like “Comment notify” (https://www.drupal.org/project/comment_notify) only send the notification once the comment has been approved.
Let me know how you go with the Actions module.
Hello,
How to display name of the user who posted comment on node details page in comment listing? is there any settings?
Thanks,
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
great work!
How to theme Comments & Comments form?
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
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
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
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
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
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.
Thanks so much for this tut! I really wanted to add Facebook comments to my ‘about’ page and this made it simple
hi Ivan,
where do i customize user detail display. I only want to show the picture and name of the user who commented with the comment.
thanks
Chime
Hi chime,
This all happens by default (or I must not understand your question). If you can’t see an image make sure you upload an image on the profile.
Cheers,
Ivan
Great Tutorial .. as always..
I just would like you to elaborate a little on how to make a private comment, so that only the author of the node can see
Thank you
Hi Salman,
I haven’t done this myself so you’ll need to test it out.
But, could you just remove the “View comments” permission for authenticated and anonymous users?
For users to post a comment they need “Post comments” permission, but see what happens if you remove the “View comments” permission.
Cheers,
Ivan
Private comments can be done trought the personal contact form.
Thanks for your input.
But the user asked for private comments on nodes. The personal contact form has nothing to do with comments.
Smart idea Ivan,
but this way I will have either private or public comments. I was hoping for a way to have both 🙂
Regards
Additionally, if the node author replied the private comment, the comment author would not be able to view it
Hi Salman,
I understand it’s not the best solution.
There’s no Drupal 8 module for handling private comments.
Cheers,
Ivan
After i adding comment section in my drupal , lots of spam commets start to come how can i prevent them ?
Just google “Drupal stop spam” and you’ll find a bunch of blog posts about recommended modules.
You’ll need to use a collection of modules like recaptcha and honeypot.
https://www.drupal.org/project/recaptcha
https://www.drupal.org/project/honeypot