When looking to achieve the goal of adding comments to a custom entity type, I found that in Drupal 8 comments are added to entities with a special field. In order to use this new field, you must first setup a new Entity Type, Bundle, and Comment Type. Here are the steps to do just that.

In this example, I’m using the Entity Construction Kit to create the custom entity type and bundle. I don’t have too much experience with this module at the moment, but so far it seems great!

Summary:

  1. Create Entity Type
  2. Create Bundle
  3. Create Comment Type
  4. Add Comment field to new Bundle

#1 Create a new Entity Type

Using the ECK module, you can do this by browsing to /admin/structure/eck/entity_type. I’ve named mine “Example Custom Entity”.

ECK Create Entity screenshot

#2 Create a new Bundle for that Entity Type

Using ECK, add a new bundle to this entity type by selecting “Bundle list” from the dropdown beside your newly created entity type. I’ve named mine “Example Custom Bundle”

ECK Create bundle on entity screenshot

#3 Create a new Comment Type

Next, we need to add a new Comment Type that will be used with our new Entity Type. Be sure to enable the Comment module, then browse to /admin/structure/comment and click “Add comment type”. I’ve named my new comment type “Example Comment Type”.

Create comment type screenshot

#4 Add a Comments field to the new Bundle

Now we’re ready to hook them up! Visit your new bundle and click “Manage Fields”. Then select the “Comments” field, and click next to configure it.

Add comment field to bundle screenshot

#5 Select your new Comment Type & Configure the field

On the next screen you will select the new comment type you created earlier and “Save field settings”.

Configure comment field screenshot

Then, you will be presented with the familiar comments configuration options which in previous versions of Drupal were stored along with the bundle settings.

Configure comment settings screenshot

#6 Voila! All Done

We now have a custom entity type, custom bundle, and custom comment type all linked up and ready to go.

Example of the custom comment type on a new entity screenshot


Read more about Comments in Drupal 8

About the Author

Jonathan Daggerhart

Long time Drupal and WordPress developer. I like to write modules and plugins, and I dabble in frontend and design.

Leave a Reply

Your email address will not be published. Required fields are marked *