function hook_comment_view_alter

Alter the results of comment_view().

This hook is called after the content has been assembled in a structured array and may be used for doing processing which requires that the complete comment content structure has been built.

If the module wishes to act on the rendered HTML of the comment rather than the structured content array, it may use this hook to add a #post_render callback. Alternatively, it could also implement hook_preprocess_HOOK() for comment.html.twig. See drupal_render() documentation for details.

Parameters

$build: A renderable array representing the comment.

\Drupal\comment\Plugin\Core\Entity\Comment $comment: The comment being rendered.

\Drupal\entity\Plugin\Core\Entity\EntityDisplay $display: The entity_display object holding the display options configured for the comment components.

See also

comment_view()

hook_entity_view_alter()

Related topics

File

drupal/core/modules/comment/comment.api.php, line 129
Hooks provided by the Comment module.

Code

function hook_comment_view_alter(&$build, \Drupal\comment\Plugin\Core\Entity\Comment $comment, \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display) {

  // Check for the existence of a field added by another module.
  if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {

    // Change its weight.
    $build['an_additional_field']['#weight'] = -10;
  }

  // Add a #post_render callback to act on the rendered HTML of the comment.
  $build['#post_render'][] = 'my_module_comment_post_render';
}