drupal 8 comment:load code example

Example: drupal 8 load a comment programmatically

$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
 ];
}

Tags:

Misc Example