home
Hero image for: Getting node comments programmatically in Drupal 8

Getting node comments programmatically in Drupal 8

By Eduardo García ● CTO | January 20th, 2016

This article belongs to the tips and tricks section in Drupal 8 because I will explain to you how to get entity comments from a particular entity node.

Before to start to code, I want to clarify that in Drupal 8 comments could be inside Content type definition as a standard field.

I will handle comments here as a separate entity. To get comments I will use EntityQuery using this approach, we will avoid writing explicit SQL queries, and our code will become portable.

$cids = \Drupal::entityQuery('comment')
   ->condition('entity_id', $ticketID)
   ->condition('entity_type', 'node')
   ->sort('cid', 'DESC')
   ->execute();

$comments = [];

foreach($cids as $cid) {
 $comment = Comment::load($cid);

 $comments[] = [
     'cid' => $cid,
     'uid' => $comment->getOwnerId(),
     'subject' => $comment->get('subject')->value,
     'body' => $comment->get('field_comment_body')->value,
     'created' => $comment->get('created')->value
 ];
}

Let’s review the code above in detail; As you can see, is required to specify to what kind of entity we are trying to get comments, that tell us that in Drupal 8 is possible to link comments to any entity.

Additionally, I have specified the Entity ID required to get comments, is possible add more conditions; this is useful taking in count that comments are entities and is possible that comments have customs fields with extra information.

When we create the EntityQuery, we define what kind of entity we want to process, as the result the query execution will return primary list keys of entities matching the conditions, in this case, cid represents the primary key.

Using the list of comments ids or cids, we load the comment object. I traverse the option to create an arbitrary array, only to show how do you have to access to comment entity property, usually is required to use the method get, but there are some exceptions like comment owner ID.

I hope that do you found this article useful.

Related Posts