Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * User page callbacks for tracker.module.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 use Drupal\Core\Cache\Cache;
|
Chris@0
|
9 use Drupal\node\Entity\Node;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Page callback: Generates a page of tracked nodes for the site.
|
Chris@0
|
13 *
|
Chris@0
|
14 * Queries the database for info, adds RDFa info if applicable, and generates
|
Chris@0
|
15 * the render array that will be used to render the page.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @param \Drupal\user\UserInterface $account
|
Chris@0
|
18 * (optional) The user account to track.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @return array
|
Chris@0
|
21 * A renderable array.
|
Chris@0
|
22 */
|
Chris@0
|
23 function tracker_page($account = NULL) {
|
Chris@0
|
24 if ($account) {
|
Chris@18
|
25 $query = \Drupal::database()->select('tracker_user', 't')
|
Chris@0
|
26 ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
Chris@0
|
27 ->addMetaData('base_table', 'tracker_user')
|
Chris@0
|
28 ->condition('t.uid', $account->id());
|
Chris@0
|
29 }
|
Chris@0
|
30 else {
|
Chris@18
|
31 $query = \Drupal::service('database.replica')->select('tracker_node', 't')
|
Chris@0
|
32 ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
Chris@0
|
33 ->addMetaData('base_table', 'tracker_node');
|
Chris@0
|
34 }
|
Chris@0
|
35
|
Chris@0
|
36 // This array acts as a placeholder for the data selected later
|
Chris@0
|
37 // while keeping the correct order.
|
Chris@0
|
38 $tracker_data = $query
|
Chris@0
|
39 ->addTag('node_access')
|
Chris@0
|
40 ->fields('t', ['nid', 'changed'])
|
Chris@0
|
41 ->condition('t.published', 1)
|
Chris@0
|
42 ->orderBy('t.changed', 'DESC')
|
Chris@0
|
43 ->limit(25)
|
Chris@0
|
44 ->execute()
|
Chris@0
|
45 ->fetchAllAssoc('nid');
|
Chris@0
|
46
|
Chris@0
|
47 $cache_tags = [];
|
Chris@0
|
48 $rows = [];
|
Chris@0
|
49 if (!empty($tracker_data)) {
|
Chris@0
|
50 // Load nodes into an array with the same order as $tracker_data.
|
Chris@0
|
51 $nodes = Node::loadMultiple(array_keys($tracker_data));
|
Chris@0
|
52
|
Chris@0
|
53 // Enrich the node data.
|
Chris@0
|
54 $result = \Drupal::service('comment.statistics')->read($nodes, 'node', FALSE);
|
Chris@0
|
55 foreach ($result as $statistics) {
|
Chris@0
|
56 // The node ID may not be unique; there can be multiple comment fields.
|
Chris@0
|
57 // Make comment_count the total of all comments.
|
Chris@0
|
58 $nid = $statistics->entity_id;
|
Chris@0
|
59 if (empty($nodes[$nid]->comment_count)
|
Chris@0
|
60 || !is_numeric($nodes[$nid]->comment_count)) {
|
Chris@0
|
61 $nodes[$nid]->comment_count = $statistics->comment_count;
|
Chris@0
|
62 }
|
Chris@0
|
63 else {
|
Chris@0
|
64 $nodes[$nid]->comment_count += $statistics->comment_count;
|
Chris@0
|
65 }
|
Chris@0
|
66 // Make the last comment timestamp reflect the latest comment.
|
Chris@0
|
67 if (!isset($nodes[$nid]->last_comment_timestamp)) {
|
Chris@0
|
68 $nodes[$nid]->last_comment_timestamp = $statistics->last_comment_timestamp;
|
Chris@0
|
69 }
|
Chris@0
|
70 else {
|
Chris@0
|
71 $nodes[$nid]->last_comment_timestamp = max($nodes[$nid]->last_comment_timestamp, $statistics->last_comment_timestamp);
|
Chris@0
|
72 }
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 // Display the data.
|
Chris@0
|
76 foreach ($nodes as $node) {
|
Chris@0
|
77 // Set the last activity time from tracker data. This also takes into
|
Chris@0
|
78 // account comment activity, so getChangedTime() is not used.
|
Chris@0
|
79 $node->last_activity = $tracker_data[$node->id()]->changed;
|
Chris@0
|
80
|
Chris@0
|
81 // Determine the number of comments.
|
Chris@0
|
82 $comments = 0;
|
Chris@0
|
83 if ($node->comment_count) {
|
Chris@0
|
84 $comments = $node->comment_count;
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 $row = [
|
Chris@0
|
88 'type' => node_get_type_label($node),
|
Chris@0
|
89 'title' => [
|
Chris@0
|
90 'data' => [
|
Chris@0
|
91 '#type' => 'link',
|
Chris@18
|
92 '#url' => $node->toUrl(),
|
Chris@0
|
93 '#title' => $node->getTitle(),
|
Chris@0
|
94 ],
|
Chris@0
|
95 'data-history-node-id' => $node->id(),
|
Chris@0
|
96 'data-history-node-timestamp' => $node->getChangedTime(),
|
Chris@0
|
97 ],
|
Chris@0
|
98 'author' => [
|
Chris@0
|
99 'data' => [
|
Chris@0
|
100 '#theme' => 'username',
|
Chris@0
|
101 '#account' => $node->getOwner(),
|
Chris@0
|
102 ],
|
Chris@0
|
103 ],
|
Chris@0
|
104 'comments' => [
|
Chris@0
|
105 'class' => ['comments'],
|
Chris@0
|
106 'data' => $comments,
|
Chris@0
|
107 'data-history-node-last-comment-timestamp' => $node->last_comment_timestamp,
|
Chris@0
|
108 ],
|
Chris@0
|
109 'last updated' => [
|
Chris@0
|
110 'data' => t('@time ago', [
|
Chris@0
|
111 '@time' => \Drupal::service('date.formatter')->formatTimeDiffSince($node->last_activity),
|
Chris@0
|
112 ]),
|
Chris@0
|
113 ],
|
Chris@0
|
114 ];
|
Chris@0
|
115
|
Chris@0
|
116 $rows[] = $row;
|
Chris@0
|
117
|
Chris@0
|
118 // Add node and node owner to cache tags.
|
Chris@0
|
119 $cache_tags = Cache::mergeTags($cache_tags, $node->getCacheTags());
|
Chris@0
|
120 if ($node->getOwner()) {
|
Chris@0
|
121 $cache_tags = Cache::mergeTags($cache_tags, $node->getOwner()->getCacheTags());
|
Chris@0
|
122 }
|
Chris@0
|
123 }
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 // Add the list cache tag for nodes.
|
Chris@0
|
127 $cache_tags = Cache::mergeTags($cache_tags, \Drupal::entityManager()->getDefinition('node')->getListCacheTags());
|
Chris@0
|
128
|
Chris@0
|
129 $page['tracker'] = [
|
Chris@0
|
130 '#rows' => $rows,
|
Chris@0
|
131 '#header' => [t('Type'), t('Title'), t('Author'), t('Comments'), t('Last updated')],
|
Chris@0
|
132 '#type' => 'table',
|
Chris@0
|
133 '#empty' => t('No content available.'),
|
Chris@0
|
134 ];
|
Chris@0
|
135 $page['pager'] = [
|
Chris@0
|
136 '#type' => 'pager',
|
Chris@0
|
137 '#weight' => 10,
|
Chris@0
|
138 ];
|
Chris@0
|
139 $page['#sorted'] = TRUE;
|
Chris@0
|
140 $page['#cache']['tags'] = $cache_tags;
|
Chris@0
|
141 $page['#cache']['contexts'][] = 'user.node_grants:view';
|
Chris@0
|
142
|
Chris@0
|
143 // Display the reading history if that module is enabled.
|
Chris@0
|
144 if (\Drupal::moduleHandler()->moduleExists('history')) {
|
Chris@0
|
145 // Reading history is tracked for authenticated users only.
|
Chris@0
|
146 if (\Drupal::currentUser()->isAuthenticated()) {
|
Chris@0
|
147 $page['#attached']['library'][] = 'tracker/history';
|
Chris@0
|
148 }
|
Chris@0
|
149 $page['#cache']['contexts'][] = 'user.roles:authenticated';
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 return $page;
|
Chris@0
|
153 }
|