ForumManager::getLastPost

protected ForumManager::getLastPost($tid)

Provides the last post information for the given forum tid.

Parameters

int $tid: The forum tid.

Return value

\stdClass The last post for the given forum.

File

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

Class

ForumManager
Provides forum manager service.

Namespace

Drupal\forum

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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', array(':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', array('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;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.