ForumManager::getTopics

public ForumManager::getTopics($tid, AccountInterface $account)

Gets list of forum topics.

Parameters

int $tid: Term ID.

\Drupal\Core\Session\AccountInterface $account: Account to fetch topics for.

Return value

array Array with keys 'topics' and 'header'.

Overrides ForumManagerInterface::getTopics

File

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

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
public function getTopics($tid, AccountInterface $account) {
  $config = $this->configFactory->get('forum.settings');
  $forum_per_page = $config->get('topics.page_limit');
  $sortby = $config->get('topics.order');
 
  $header = array(
    array('data' => $this->t('Topic'), 'field' => 'f.title'),
    array('data' => $this->t('Replies'), 'field' => 'f.comment_count'),
    array('data' => $this->t('Last reply'), 'field' => 'f.last_comment_timestamp'),
  );
 
  $order = $this->getTopicOrder($sortby);
  for ($i = 0; $i < count($header); $i++) {
    if ($header[$i]['field'] == $order['field']) {
      $header[$i]['sort'] = $order['sort'];
    }
  }
 
  $query = $this->connection->select('forum_index', 'f')
    ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
    ->extend('Drupal\Core\Database\Query\TableSortExtender');
  $query->fields('f');
  $query
  ->condition('f.tid', $tid)
    ->addTag('node_access')
    ->addMetaData('base_table', 'forum_index')
    ->orderBy('f.sticky', 'DESC')
    ->orderByHeader($header)
    ->limit($forum_per_page);
 
  $count_query = $this->connection->select('forum_index', 'f');
  $count_query->condition('f.tid', $tid);
  $count_query->addExpression('COUNT(*)');
  $count_query->addTag('node_access');
  $count_query->addMetaData('base_table', 'forum_index');
 
  $query->setCountQuery($count_query);
  $result = $query->execute();
  $nids = array();
  foreach ($result as $record) {
    $nids[] = $record->nid;
  }
  if ($nids) {
    $nodes = $this->entityManager->getStorage('node')->loadMultiple($nids);
 
    $query = $this->connection->select('node_field_data', 'n')
      ->extend('Drupal\Core\Database\Query\TableSortExtender');
    $query->fields('n', array('nid'));
 
    $query->join('comment_entity_statistics', 'ces', "n.nid = ces.entity_id AND ces.field_name = 'comment_forum' AND ces.entity_type = 'node'");
    $query->fields('ces', array(
      'cid',
      'last_comment_uid',
      'last_comment_timestamp',
      'comment_count'
    ));
 
    $query->join('forum_index', 'f', 'f.nid = n.nid');
    $query->addField('f', 'tid', 'forum_tid');
 
    $query->join('users_field_data', 'u', 'n.uid = u.uid AND u.default_langcode = 1');
    $query->addField('u', 'name');
 
    $query->join('users_field_data', 'u2', 'ces.last_comment_uid = u2.uid AND u.default_langcode = 1');
 
    $query->addExpression('CASE ces.last_comment_uid WHEN 0 THEN ces.last_comment_name ELSE u2.name END', 'last_comment_name');
 
    $query
    ->orderBy('f.sticky', 'DESC')
      ->orderByHeader($header)
      ->condition('n.nid', $nids, 'IN')
      // @todo This should be actually filtering on the desired node language
      //   and just fall back to the default language.
      ->condition('n.default_langcode', 1);
 
    $result = array();
    foreach ($query->execute() as $row) {
      $topic = $nodes[$row->nid];
      $topic->comment_mode = $topic->comment_forum->status;
 
      foreach ($row as $key => $value) {
        $topic->{$key} = $value;
      }
      $result[] = $topic;
    }
  }
  else {
    $result = array();
  }
 
  $topics = array();
  $first_new_found = FALSE;
  foreach ($result as $topic) {
    if ($account->isAuthenticated()) {
      // A forum is new if the topic is new, or if there are new comments since
      // the user's last visit.
      if ($topic->forum_tid != $tid) {
        $topic->new = 0;
      }
      else {
        $history = $this->lastVisit($topic->id(), $account);
        $topic->new_replies = $this->commentManager->getCountNewComments($topic, 'comment_forum', $history);
        $topic->new = $topic->new_replies || ($topic->last_comment_timestamp > $history);
      }
    }
    else {
      // Do not track "new replies" status for topics if the user is anonymous.
      $topic->new_replies = 0;
      $topic->new = 0;
    }
 
    // Make sure only one topic is indicated as the first new topic.
    $topic->first_new = FALSE;
    if ($topic->new != 0 && !$first_new_found) {
      $topic->first_new = TRUE;
      $first_new_found = TRUE;
    }
 
    if ($topic->comment_count > 0) {
      $last_reply = new \stdClass();
      $last_reply->created = $topic->last_comment_timestamp;
      $last_reply->name = $topic->last_comment_name;
      $last_reply->uid = $topic->last_comment_uid;
      $topic->last_reply = $last_reply;
    }
    $topics[$topic->id()] = $topic;
  }
 
  return array('topics' => $topics, 'header' => $header);
 
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.