tracker.pages.inc
4.86 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* @file
* User page callbacks for tracker.module.
*/
use Drupal\Core\Cache\Cache;
use Drupal\node\Entity\Node;
/**
* Page callback: Generates a page of tracked nodes for the site.
*
* Queries the database for info, adds RDFa info if applicable, and generates
* the render array that will be used to render the page.
*
* @return array
* A renderable array.
*
* @see tracker_menu()
*/
function tracker_page($account = NULL) {
if ($account) {
$query = db_select('tracker_user', 't')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->addMetaData('base_table', 'tracker_user')
->condition('t.uid', $account->id());
}
else {
$query = db_select('tracker_node', 't', array('target' => 'replica'))
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->addMetaData('base_table', 'tracker_node');
}
// This array acts as a placeholder for the data selected later
// while keeping the correct order.
$tracker_data = $query
->addTag('node_access')
->fields('t', array('nid', 'changed'))
->condition('t.published', 1)
->orderBy('t.changed', 'DESC')
->limit(25)
->execute()
->fetchAllAssoc('nid');
$cache_tags = [];
$rows = [];
if (!empty($tracker_data)) {
// Load nodes into an array with the same order as $tracker_data.
$nodes = Node::loadMultiple(array_keys($tracker_data));
// Enrich the node data.
$result = \Drupal::service('comment.statistics')->read($nodes, 'node', FALSE);
foreach ($result as $statistics) {
// The node ID may not be unique; there can be multiple comment fields.
// Make comment_count the total of all comments.
$nid = $statistics->entity_id;
if (empty($nodes[$nid]->comment_count)
|| !is_numeric($nodes[$nid]->comment_count)) {
$nodes[$nid]->comment_count = $statistics->comment_count;
}
else {
$nodes[$nid]->comment_count += $statistics->comment_count;
}
// Make the last comment timestamp reflect the latest comment.
if (!isset($nodes[$nid]->last_comment_timestamp)) {
$nodes[$nid]->last_comment_timestamp = $statistics->last_comment_timestamp;
}
else {
$nodes[$nid]->last_comment_timestamp = max($nodes[$nid]->last_comment_timestamp, $statistics->last_comment_timestamp);
}
}
// Display the data.
foreach ($nodes as $node) {
// Set the last activity time from tracker data. This also takes into
// account comment activity, so getChangedTime() is not used.
$node->last_activity = $tracker_data[$node->id()]->changed;
// Determine the number of comments.
$comments = 0;
if ($node->comment_count) {
$comments = $node->comment_count;
}
$row = array(
'type' => node_get_type_label($node),
'title' => array(
'data' => array(
'#type' => 'link',
'#url' => $node->urlInfo(),
'#title' => $node->getTitle(),
),
'data-history-node-id' => $node->id(),
'data-history-node-timestamp' => $node->getChangedTime(),
),
'author' => array(
'data' => array(
'#theme' => 'username',
'#account' => $node->getOwner(),
),
),
'comments' => array(
'class' => array('comments'),
'data' => $comments,
'data-history-node-last-comment-timestamp' => $node->last_comment_timestamp,
),
'last updated' => array(
'data' => t('@time ago', array(
'@time' => \Drupal::service('date.formatter')->formatTimeDiffSince($node->last_activity),
)),
),
);
$rows[] = $row;
// Add node and node owner to cache tags.
$cache_tags = Cache::mergeTags($cache_tags, $node->getCacheTags());
if ($node->getOwner()) {
$cache_tags = Cache::mergeTags($cache_tags, $node->getOwner()->getCacheTags());
}
}
}
// Add the list cache tag for nodes.
$cache_tags = Cache::mergeTags($cache_tags, \Drupal::entityManager()->getDefinition('node')->getListCacheTags());
$page['tracker'] = array(
'#rows' => $rows,
'#header' => array(t('Type'), t('Title'), t('Author'), t('Comments'), t('Last updated')),
'#type' => 'table',
'#empty' => t('No content available.'),
);
$page['pager'] = array(
'#type' => 'pager',
'#weight' => 10,
);
$page['#sorted'] = TRUE;
$page['#cache']['tags'] = $cache_tags;
$page['#cache']['contexts'][] = 'user.node_grants:view';
// Display the reading history if that module is enabled.
if (\Drupal::moduleHandler()->moduleExists('history')) {
// Reading history is tracked for authenticated users only.
if (\Drupal::currentUser()->isAuthenticated()) {
$page['#attached']['library'][] = 'tracker/history';
}
$page['#cache']['contexts'][] = 'user.roles:authenticated';
}
return $page;
}