George Milonas
George Milonas

Reputation: 563

Drupal 7 Render a Comment Object

So this is the problem I am running into. If I have a comment object, I want to create a renderable array that is using the display settings of that comment. As of now this is what I have:

$commentNew = comment_load($var);
$reply[] = field_view_value('comment', $commentNew, 'comment_body', $commentNew->comment_body['und'][0]);

Which works fine because I dont have any specific settings setup for the body. But I also have image fields and video embed fields that I need to have rendered the way they are setup in the system. How would I go about doing that?

Upvotes: 1

Views: 669

Answers (1)

Clive
Clive

Reputation: 36957

Drupal core does it with the comment_view() function:

$comment = comment_load($var);
$node = node_load($comment->nid);
$view_mode = 'full'; // Or whatever view mode is appropriate
$build = comment_view($comment, $node, $view_mode);

If you need to change a particular field from the default, use hook_comment_view():

function MYMODULE_comment_view($comment, $view_mode, $langcode) {
  $comment->content['body'] = array('#markup' => 'something');
}

or just edit the $build array received from comment_view() as you need to if implementing the hook won't work for your use case.

Upvotes: 3

Related Questions