You are here

protected function ForumManager::getLastPost in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/forum/src/ForumManager.php \Drupal\forum\ForumManager::getLastPost()
  2. 10 core/modules/forum/src/ForumManager.php \Drupal\forum\ForumManager::getLastPost()

Provides the last post information for the given forum tid.

Parameters

int $tid: The forum tid.

Return value

object The last post for the given forum.

1 call to ForumManager::getLastPost()
ForumManager::getChildren in core/modules/forum/src/ForumManager.php
Utility method to fetch the child forums for a given forum.

File

core/modules/forum/src/ForumManager.php, line 353

Class

ForumManager
Provides forum manager service.

Namespace

Drupal\forum

Code

protected function getLastPost($tid) {
  if (!empty($this->lastPostData[$tid])) {
    return $this->lastPostData[$tid];
  }

  // Query "Last Post" information for this forum.
  $query = $this->connection
    ->select('node_field_data', 'n');
  $query
    ->join('forum', 'f', 'n.vid = f.vid AND f.tid = :tid', [
    ':tid' => $tid,
  ]);
  $query
    ->join('comment_entity_statistics', 'ces', "n.nid = ces.entity_id AND ces.field_name = 'comment_forum' AND ces.entity_type = 'node'");
  $query
    ->join('users_field_data', 'u', 'ces.last_comment_uid = u.uid AND u.default_langcode = 1');
  $query
    ->addExpression('CASE ces.last_comment_uid WHEN 0 THEN ces.last_comment_name ELSE u.name END', 'last_comment_name');
  $topic = $query
    ->fields('ces', [
    'last_comment_timestamp',
    'last_comment_uid',
  ])
    ->condition('n.status', 1)
    ->orderBy('last_comment_timestamp', 'DESC')
    ->range(0, 1)
    ->addTag('node_access')
    ->execute()
    ->fetchObject();

  // Build the last post information.
  $last_post = new \stdClass();
  if (!empty($topic->last_comment_timestamp)) {
    $last_post->created = $topic->last_comment_timestamp;
    $last_post->name = $topic->last_comment_name;
    $last_post->uid = $topic->last_comment_uid;
  }
  $this->lastPostData[$tid] = $last_post;
  return $last_post;
}