Drupal 8: How to add Comments to a Custom Entity Type

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

7 Thoughts

Discussion

Sylvain
October 30, 2018

Thank you for this tip, this could be helpful…
The question is : how to do this fully programmatically ?
I’ve created a custom entity :

/**
* Définition
* @ingroup Cercle
* @ContentEntityType(
* id = “circle”,
* label = @Translation(“Circle”),
* base_table = “bgm_circle”,
* entity_keys = {
* “id” = “cid”,
* “label” = “name”,
* “uuid” = “uuid”,
* },
* )
*/
class Circle extends ContentEntityBase implements ContentEntityInterface {

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

$fields = [
‘cid’ =>BaseFieldDefinition::create(‘integer’)
->setLabel(t(‘ID’))
->setDescription(t(‘The ID of the circle entity.’))
->setReadOnly(TRUE),

‘name’ =>BaseFieldDefinition::create(‘string’)
->setLabel(t(“Circle name”))
->setRequired(TRUE)
->setDescription(t(‘The name of the circle.’))
->setSettings([
‘default_value’ => ”,
‘max_length’ => 255,
‘text_processing’ => 0,
]),
‘uuid’ =>BaseFieldDefinition::create(‘uuid’)
->setLabel(t(‘UUID’))
->setDescription(t(‘The UUID of the circle entity.’))
->setReadOnly(TRUE),
];
return $fields;
}
}

What’s the next step ?

Thanks for your interest

vensires
April 6, 2020

I recently required this functionality. I used Drupal Console to create my content entity and modified the “src/Entity/CLASS.php” file to add a “comment” field.

In order to achieve what was required, after installing the module, I created the comment’s bundle targeting the new CLASS entity type using Drupal’s UI. After I added the required fields to my new comment bundle, I used “drush cex” to export the configuration.

I then copied the new files to my module’s “config/install” folder (though core’s book module uses “config/optional”) and deleted their uuids. The next steps were to:
– Delete the comment type I had just created using the UI.
– Delete any CLASS entities I had created in the meantime.
– Uninstall my module.
– Try to install my module again.

After that last step, the entity and the comment type should have been created correctly.

Habiba Mansouri
December 19, 2018

Good morning,
How can’i create comments programatically in drupal 8 project using a custom theme?
Thank’you :)

David
January 1, 2020

Very useful content for drupal development. Thanks for the tips.

Quality Solution
February 24, 2021

Thanks for your nice post I really like it and appreciate it.

Harry
July 10, 2021

You have added the value into your stuff, Awesome work! Keep up the good work.

Ayan
July 28, 2021

I simply want to mention that I am just all new to weblog and certainly enjoyed your web page. I very likely to bookmark your blog. You definitely have perfect posts. Bless you for sharing with us your web site.

Stefan
May 18, 2022

Enjoyed a lot reading this article. Thanks for sharing such content.

Leave a Reply

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