function CommentHooks::entityView

Implements hook_entity_view().

Attributes

#[Hook('entity_view')]

File

core/modules/comment/src/Hook/CommentHooks.php, line 195

Class

CommentHooks
Hook implementations for comment.

Namespace

Drupal\comment\Hook

Code

public function entityView(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) : void {
  if ($entity instanceof FieldableEntityInterface && $view_mode == 'rss' && $display->getComponent('links')) {
    /** @var \Drupal\comment\CommentManagerInterface $comment_manager */
    $comment_manager = \Drupal::service('comment.manager');
    $fields = $comment_manager->getFields($entity->getEntityTypeId());
    foreach ($fields as $field_name => $detail) {
      if ($entity->hasField($field_name) && $entity->get($field_name)->status != CommentItemInterface::HIDDEN) {
        // Add a comments RSS element which is a URL to the comments of this
        // entity.
        $options = [
          'fragment' => 'comments',
          'absolute' => TRUE,
        ];
        $entity->rss_elements[] = [
          'key' => 'comments',
          'value' => $entity->toUrl('canonical', $options)
            ->toString(),
        ];
      }
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.