You are here

function social_comment_comment_view in Open Social 8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_comment/social_comment.module \social_comment_comment_view()
  2. 8.2 modules/social_features/social_comment/social_comment.module \social_comment_comment_view()
  3. 8.3 modules/social_features/social_comment/social_comment.module \social_comment_comment_view()
  4. 8.4 modules/social_features/social_comment/social_comment.module \social_comment_comment_view()
  5. 8.5 modules/social_features/social_comment/social_comment.module \social_comment_comment_view()
  6. 8.6 modules/social_features/social_comment/social_comment.module \social_comment_comment_view()
  7. 8.7 modules/social_features/social_comment/social_comment.module \social_comment_comment_view()
  8. 8.8 modules/social_features/social_comment/social_comment.module \social_comment_comment_view()
  9. 10.3.x modules/social_features/social_comment/social_comment.module \social_comment_comment_view()
  10. 10.0.x modules/social_features/social_comment/social_comment.module \social_comment_comment_view()
  11. 10.1.x modules/social_features/social_comment/social_comment.module \social_comment_comment_view()
  12. 10.2.x modules/social_features/social_comment/social_comment.module \social_comment_comment_view()

Implements hook_ENTITY_TYPE_view().

File

modules/social_features/social_comment/social_comment.module, line 189
The Social comment module.

Code

function social_comment_comment_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  if ($view_mode === 'activity') {
    $commented_entity = $entity
      ->getCommentedEntity();
    if (is_object($commented_entity)) {
      $commented_entity_type = $commented_entity
        ->getEntityTypeId();

      // Teaser of commented entity.
      $build['commented_entity'] = \Drupal::entityTypeManager()
        ->getViewBuilder($commented_entity_type)
        ->view($commented_entity, 'activity_comment');

      // Prepare the "Show all comments" link.
      if ($commented_entity_type === 'post') {
        $comment_field_name = 'field_post_comments';
      }
      elseif ($commented_entity_type === 'node') {
        $comment_field_name = 'field_' . $commented_entity
          ->bundle() . '_comments';

        // Prepare the "Comments" link.
        $link_options = [
          'fragment' => 'comment-form',
          'attributes' => [
            'class' => [
              'btn btn-block btn-link brand-text-primary',
            ],
          ],
        ];
        $commented_entity_url = $commented_entity
          ->toUrl('canonical', $link_options);
        $build['comment_link'] = Link::fromTextAndUrl(t('Comment'), $commented_entity_url)
          ->toRenderable();
      }
      $comment_count = $commented_entity->{$comment_field_name}->comment_count;
      $t_args = [
        ':num_comments' => $comment_count,
      ];
      if ($comment_count > 1) {
        $more_link = t('Show all :num_comments comments', $t_args);

        // Set link classes to be added to the button.
        $more_link_options = [
          'attributes' => [
            'class' => [
              'btn',
              'btn-flat',
              'brand-text-primary',
            ],
          ],
        ];

        // Set path to post node.
        $link_url = $commented_entity
          ->urlInfo('canonical');

        // Attach the attributes.
        $link_url
          ->setOptions($more_link_options);
        $more_button = Link::fromTextAndUrl($more_link, $link_url)
          ->toRenderable();
        $build['more_link'] = $more_button;
      }
    }

    // Comment.
    $build['comment'] = \Drupal::entityTypeManager()
      ->getViewBuilder($entity
      ->getEntityTypeId())
      ->view($entity, 'activity_comment');
  }
}