Drupal add comments as base field to an entity

Note Statistics

Note Statistics

  • Viewed 117 times
Sat, 12/12/2020 - 08:39

To add comment support to a content entity, you need to create a comment field for the entity.

Before you start: Before we add the new field, we should define a new comment type (e.g. my_custom_comment_type) that is related to our target entity. Go to /admin/structure/comment to create a new comment type. In the Target entity type select your target entity.

Let's define the structure of our entity in a function. This would allow better reuse.

use Drupal\comment\CommentInterface;
use Drupal\comment\CommentManagerInterface;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityTypeInterface;

function getCommentFieldDefinition() {
  return BaseFieldDefinition::create('comment')
          'comment_type' => 'my_custom_comment_type',
          'locked' => false,
          'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
          'per_page' => 50,
          'form_location' => CommentItemInterface::FORM_BELOW,
          'anonymous' => CommentInterface::ANONYMOUS_MAYNOT_CONTACT,
          'preview' => DRUPAL_OPTIONAL,
          'status' => CommentItemInterface::OPEN, // Comment status
          'cid' => 0,  // Last comment ID
          'last_comment_timestamp' => 0,
          'last_comment_name' => null,
          'last_comment_uid' => 0,
          'comment_count' => 0,
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

Now that we defined the structure of the comment field we need to add it to the target entity. There are two options

  1. If the entity in question is part of the same module, update the baseFieldDefinitions method of the entity class.
  2. However, if you entity is from another module you can use a hook hook_entity_base_field_info_alter

Update the baseFieldDefinitions method

  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);
		// ...
		$fields['comments'] = getCommentFieldDefinition();
		// ...

Updating with hook_entity_base_field_info_alter

Here is a quick example on how to add a new comment field to an existing entity using hook_entity_base_field_info_alter.

// mymodule.module

 * Implements hook_entity_base_field_info_alter().
function mymodule_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type)
  if ($entity_type->id() == "my_entity") {
    $fields['comments'] = getCommentFieldDefinition();

Optional - Add this field using hook_update_N

This is required when you module is already installed and you want to provide an update to add the wanted fields. This is well documented here. This allows forward compatibility.

The naming convention is for the version is <drupal core version><module major version> <update number (last 2 digits)>. In the example below

  • We are targeting Drupal 8 (8).
  • The version that brings this feature is 1. This means that projects that have a version a major version lower that 1 would run this update when they upgraded our module.
  • This is the first update of that version hence 01
// mymodule.install

function mymodule_update_8101 {
  $field_storage_definition = getCommentFieldDefinition();
    ->installFieldStorageDefinition('comments', 'my_entity', 'mymodule', $field_storage_definition);

Hope this helps. ;-)

You might also like alternative options here.

Authored by