Drupal - How to add comments to content entities

Note Statistics

Note Statistics

  • Viewed 88 times
Sat, 12/19/2020 - 08:18

Content entities include node and any other custom entities. There are few ways to add comment to entities.

Option 1 - Add a comment field via the admin interface

Use the admin interface to create a comment field for you entity. Whether it is a node or a custom entity it is the same process. Use the steps here to make it happen. Follow the instruction here to add an comment to an entity using the UI

Pros

  • Easier to implement than from code.
  • You can use the feature module to store configurations as code and manage configuration updates. This the easiest to manage if it is a custom module that you own.

Cons

  • This field is not part of the base fields, so not in the base entity table. Not a big issue though.

Use case

  • This is a custom module that you or your team owns. The module is not public.
  • You use the feature module for manage and update field configurations.

Option 2 - Programmatically add this field to base field definition of your entity

A second option is to add comments as a base field of the target entity type. The example here provide a list of steps.

Pros

  • Your field is part of the entity base fields

Cons

  • Require programming.
  • You need to also write forward compatibility. Required if your module is already installed and an update is required.

Use case

  • A contrib module that require update and forward compatibility.
  • You prefer to have comments as a base field.
Authored by