Since flexibility’s already one of Drupal’s emblematic features, it was about time they did something about their quite rudimentary commenting system, right? Well, get ready to embrace the upgraded Drupal 8 comments, which are far more than just some basic settings in a node type.
 

So, What Makes the Drupal 8 Comments far More Tempting?
 

1. They’re set up as fields

That’s right, comments are no longer fieldable, full-featured entities. They’re still entities but set up as fields. What’s in this for you?
You get to choose where exactly on the page your comments will get displayed and you’re no longer restricted to add them to content only (now they can practically “show up” anywhere the fields can)
 

2. Comments have their own fields

Can you believe this! Not only that they’re set up as fields in Drupal 8, but you can also add a set of fields to each comment.
 

3. They’re of several types

Now, this is just a big step ahead of the standard, one-type-only comments in Drupal 7!
 
For instance, now you can add comments to taxonomy terms, to users, blocks, contact messages, content and even to… other comments.
 
Just imagine: you’re the admin of a social networking site! You can leave private notes to your users, they, as well, can leave their own private suggestions for you, while users can also leave comments on your public-facing website, too.
 
Here’s another quick example of Drupal 8 commenting system’s flexibility: if you want to add comments to a content type you simply add a field “Comments” on the “Manage Fields” page and voila: there’s your comments field!
 
Still, when it comes to this new upgrade in Drupal’s commenting system, we must be fair and point out an issue that still needs improvement: in Drupal 8 multiple types of comments don’t come with multiple individual permissions, as well. This means that anyone who’s granted permission to post a certain type of comment can practically post any type of comment on the website.
 

4. Comments have their own area now 

You get to scroll through the “Unapproved Comments” and the “Published Comments” lists. 
 

5. The “Recent Comments” block is in Views 

Remember how frustrating it was realizing that you could not edit your “recent comments in” D7?
Things have changed now: the “recent comments” block has been moved to “Views” in Drupal 8 and is available by default.
 

And Here’s How You Can Create Your Comments in Drupal 8

Basically, the whole process comes down to 2 major steps:
 

  1. You create a comment entity in code.
     
  2. Then you save it.
 
Here’s how the code will/should look like:

 
// To create a new comment entity, we'll need `use` (import) the Comment class.
use Drupal\comment\Entity\Comment;
 
// The function name doesn't matter. Just put the the function body where you need it.
function my_modules_function_or_method() {
 
  // First, we need to create an array of field values for the comment.
  $values = [
 
    // These values are for the entity that you're creating the comment for, not the comment itself.
    'entity_type' => 'node',            // required.
    'entity_id'   => 42,                // required.
    'field_name'  => 'comment',         // required.
 
    // The user id of the comment's 'author'. Use 0 for the anonymous user.
    'uid' => 0,                         // required.
 
    // These values are for the comment itself.
    'comment_type' => 'comment',        // required.
    'subject' => 'My Awesome Comment',  // required.
    'comment_body' => $body,            // optional.
 
    // Whether the comment is 'approved' or not.
    'status' => 1,                      // optional. Defaults to 0.
  ];
 
  // This will create an actual comment entity out of our field values.
  $comment = Comment::create($values);
 
  // Last, we actually need to save the comment to the database.
  $comment->save();
}

 
Now let’s have a closer look at each one of these fields:
 
  • entity_type: it’s the entity that you attach your comment to (a node, for instance)
     
  • entity_id: it’s the id that you’ll attach the comment to (it would have to be a nid in case it’s a node that you’ll attach it to)
     
  • field_name: this field is the one for the entity that you’re attaching your comment to
     
Practically, what all these 3 first fields do is let Drupal know what entity it should attach your comment to. This whole “flexibility” laid at your feet, that you get to juggle with multiple comment fields of the same entity and the fact that those comments’ fields can use multiple types of comments, themselves, is just part of the upgrades added to the commenting system.
 
  • uid: informs Drupal which user wrote a specific comment
     
  • comment_type: the type of comment you want to create (you know, who’ve already talked about how in Drupal 8 you have several types of comments to juggle with). The default comment will be just comment
     
  • subject: just like a node comes with a title field, so does a comment come with a “subject field” in Drupal 8
     
  • comment_body: is provided by default and you can remove it, just like you can remove any other one of the fields
     
  • status: if you don’t want, as admin, to be asked to approve each comment before it goes live, set it to 1
     
  • field_foodbar: although it does not show in the above example of code, we still wanted to show you that you’re free to add custom fields if you want to. Simply use the field’s machine name and give it a default value. 
 
So, what do you think about Drupal’s new commenting system?
 
Do you find the improvements made to the way you can create comments to be a big step ahead, contributing to Drupal’s overall flexibility or do you consider that there are many other possible upgrades (feel free to name them) that its developers should have focused on?
Development

We do Drupal development

Go to our Drupal page!

Visit page!

Browse cities

Recommended Stories

Drupal: Age 1-11 in a Nutshell
For over two decades, Drupal has evolved from a simple message board to one of the most powerful and versatile… (Read more)
10 minutes /
Drupal Security Best Practices: Protecting Your Website from Common Threats
As Drupal and other technologies have grown, so have the stakes for keeping websites secure. Security isn’t a… (Read more)
10 minutes /
The Future of Drupal: What’s Next in Web Technologies
The digital world never sits still, and neither should your website. As users demand faster, smarter, and more… (Read more)
10 mins /