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 comments in Drupal 8, which are far more than just some basic settings in a node type.

So, what makes comments in Drupal 8 far more tempting?
 

1. They’re set up as fields

That’s right, in Drupal 8 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 not 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 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” Drupal 7?
Things have changed now: the “recent comments” block has been moved to “Views” in Drupal 8 and are 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 of 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 Drupal 8 commenting system’s upgrades.
 
  • 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 life, 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 comments in Drupal 8 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?

Recommended Stories

How to Integrate Alexa with Your Drupal 8 Website: A Step-by-Step Guide
Just imagine: a user asks Amazon Alexa to read out loud to him/her the headline of your latest blog post! Or maybe… (Read more)
RADU SIMILEANU / May 18 '2018
When Should You Not Consider Using Node.js? 3 Unsuitable Use Cases
It's undebatable: Node.js has practically laid the foundation of the real-time web! The real-time, two-way… (Read more)
RADU SIMILEANU / May 17 '2018
What Is Node.js Used for? What Projects Can You Build Using It? 7 Best Use Cases
Not exactly the “jumping on the latest trend" type? Therefore, you're still a bit hesitant to get on the Node.js… (Read more)
RADU SIMILEANU / May 10 '2018