Skip to main content
Category:

How to get node comments programmatically in drupal 8 & 9

 

Example How to get node comments programmatically in drupal 8 & 9 :

$entity_manager = \Drupal::entityTypeManager();
$cids = $entity_manager
    ->getStorage('comment')
    ->getQuery('AND')
    ->condition('entity_id', 'NODE_ID')
    ->condition('entity_type', 'node')
    ->condition('comment_type', 'post_trp_comments')
    ->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
 ];
}

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook