You are here

private function HeartbeatHashBlock::renderMessage in Heartbeat 8

1 call to HeartbeatHashBlock::renderMessage()
HeartbeatHashBlock::build in src/Plugin/Block/HeartbeatHashBlock.php

File

src/Plugin/Block/HeartbeatHashBlock.php, line 173

Class

HeartbeatHashBlock
Provides a 'HeartbeatBlock' block.

Namespace

Drupal\heartbeat\Plugin\Block

Code

private function renderMessage(array &$messages, $heartbeat) {
  $timeago = null;
  $diff = $this->timestamp - $heartbeat
    ->getCreatedTime();
  switch (true) {
    case $diff < 86400:
      $timeago = $this->dateFormatter
        ->formatInterval(REQUEST_TIME - $heartbeat
        ->getCreatedTime()) . ' ago';
      break;
    case $diff >= 86400 && $diff < 172800:
      $timeago = 'Yesterday at ' . $this->dateFormatter
        ->format($heartbeat
        ->getCreatedTime(), 'heartbeat_time');
      break;
    case $diff >= 172800:
      $timeago = $this->dateFormatter
        ->format($heartbeat
        ->getCreatedTime(), 'heartbeat_short');
      break;
  }
  $user = $heartbeat
    ->getOwner();
  $userView = user_view($user, 'compact');
  $userPic = $user
    ->get('user_picture')
    ->getValue();
  if (!empty($userPic)) {
    $profilePic = $user
      ->get('user_picture')
      ->getValue()[0]['target_id'];
  }
  if (NULL === $profilePic) {
    $profilePic = 86;
  }
  $pic = File::load($profilePic);
  if ($pic !== NULL) {
    $style = $this->entityTypeManager
      ->getStorage('image_style')
      ->load('thumbnail');
    $rendered = $style
      ->buildUrl($pic
      ->getFileUri());
  }
  $cids = \Drupal::entityQuery('comment')
    ->condition('entity_id', $heartbeat
    ->id())
    ->condition('entity_type', 'heartbeat')
    ->sort('cid', 'ASC')
    ->execute();
  $comments = [];
  foreach ($cids as $cid) {
    $url = Url::fromRoute('heartbeat.sub_comment_request', array(
      'cid' => $cid,
    ));
    $commentLink = Link::fromTextAndUrl(t('Reply'), $url);
    $commentLink = $commentLink
      ->toRenderable();
    $commentLink['#attributes'] = array(
      'class' => array(
        'button',
        'button-action',
        'use-ajax',
      ),
    );
    $comment = Comment::load($cid);
    $commentOwner = user_view($comment
      ->getOwner(), 'comment');
    $subCids = \Drupal::entityQuery('comment')
      ->condition('entity_id', $cid)
      ->condition('entity_type', 'comment')
      ->sort('cid', 'ASC')
      ->execute();
    $subComments = [];
    if (count($subCids) > 0) {
      foreach ($subCids as $subCid) {
        $subComment = Comment::load($subCid);
        $subDiff = $this->timestamp - $subComment
          ->getCreatedTime();
        switch (true) {
          case $subDiff < 86400:
            $timeago = $this->dateFormatter
              ->formatInterval(REQUEST_TIME - $subComment
              ->getCreatedTime()) . ' ago';
            break;
          case $subDiff >= 86400 && $subDiff < 172800:
            $timeago = 'Yesterday at ' . $this->dateFormatter
              ->format($subComment
              ->getCreatedTime(), 'heartbeat_time');
            break;
          case $subDiff >= 172800:
            $timeago = $this->dateFormatter
              ->format($subComment
              ->getCreatedTime(), 'heartbeat_short');
            break;
        }
        $subCommentOwner = user_view($subComment
          ->getOwner(), 'comment');
        $subCommentTime = $this->timestamp - $subComment
          ->getCreatedTime() < 172800 ? $this->dateFormatter
          ->formatInterval(REQUEST_TIME - $subComment
          ->getCreatedTime()) . ' ago' : $this->dateFormatter
          ->format($subComment
          ->getCreatedTime(), 'heartbeat_short');
        $subComments[] = [
          'id' => $subCid,
          'body' => $subComment
            ->get('comment_body')->value,
          'username' => $subComment
            ->getAuthorName(),
          'owner' => $subCommentOwner,
          'timeAgo' => $subCommentTime,
          'commentLike' => Heartbeat::flagAjaxBuilder('heartbeat_like_comment', $subComment, $this->flagService),
        ];
      }
    }
    $commentTimeDiff = $this->timestamp - $comment
      ->getCreatedTime();
    switch (true) {
      case $commentTimeDiff < 86400:
        $cTimeago = $this->dateFormatter
          ->formatInterval(REQUEST_TIME - $comment
          ->getCreatedTime()) . ' ago';
        break;
      case $commentTimeDiff >= 86400 && $commentTimeDiff < 172800:
        $cTimeago = 'Yesterday at ' . $this->dateFormatter
          ->format($comment
          ->getCreatedTime(), 'heartbeat_time');
        break;
      case $commentTimeDiff >= 172800:
        $cTimeago = $this->dateFormatter
          ->format($comment
          ->getCreatedTime(), 'heartbeat_short');
        break;
    }
    $comments[] = [
      'id' => $cid,
      'body' => $comment
        ->get('comment_body')->value,
      'username' => $comment
        ->getAuthorName(),
      'owner' => $commentOwner,
      'timeAgo' => $cTimeago,
      'commentLike' => Heartbeat::flagAjaxBuilder('heartbeat_like_comment', $comment, $this->flagService),
      'reply' => $commentLink,
      'subComments' => $subComments,
    ];
  }
  $form = \Drupal::service('form_builder')
    ->getForm('\\Drupal\\heartbeat\\Form\\HeartbeatCommentForm', $heartbeat);
  $commentCount = count($comments);
  $messages[] = array(
    'heartbeat' => $heartbeat
      ->getMessage()
      ->getValue()[0]['value'],
    'userPicture' => $rendered,
    'userId' => $user
      ->id(),
    'timeAgo' => $timeago,
    'id' => $heartbeat
      ->id(),
    'userName' => $user
      ->getAccountName(),
    'user' => $userView,
    'commentForm' => $form,
    'comments' => array_reverse($comments),
    'commentCount' => $commentCount > 0 ? $commentCount : '',
    'likeFlag' => Heartbeat::flagAjaxBuilder('heartbeat_like', $heartbeat, $this->flagService),
    'unlikeFlag' => Heartbeat::flagAjaxBuilder('heartbeat_unlike', $heartbeat, $this->flagService),
  );
}