5 * User page callbacks for tracker.module.
8 use Drupal\Core\Cache\Cache;
9 use Drupal\node\Entity\Node;
12 * Page callback: Generates a page of tracked nodes for the site.
14 * Queries the database for info, adds RDFa info if applicable, and generates
15 * the render array that will be used to render the page.
17 * @param \Drupal\user\UserInterface $account
18 * (optional) The user account to track.
23 function tracker_page($account = NULL) {
25 $query = db_select('tracker_user', 't')
26 ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
27 ->addMetaData('base_table', 'tracker_user')
28 ->condition('t.uid', $account->id());
31 $query = db_select('tracker_node', 't', ['target' => 'replica'])
32 ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
33 ->addMetaData('base_table', 'tracker_node');
36 // This array acts as a placeholder for the data selected later
37 // while keeping the correct order.
38 $tracker_data = $query
39 ->addTag('node_access')
40 ->fields('t', ['nid', 'changed'])
41 ->condition('t.published', 1)
42 ->orderBy('t.changed', 'DESC')
45 ->fetchAllAssoc('nid');
49 if (!empty($tracker_data)) {
50 // Load nodes into an array with the same order as $tracker_data.
51 $nodes = Node::loadMultiple(array_keys($tracker_data));
53 // Enrich the node data.
54 $result = \Drupal::service('comment.statistics')->read($nodes, 'node', FALSE);
55 foreach ($result as $statistics) {
56 // The node ID may not be unique; there can be multiple comment fields.
57 // Make comment_count the total of all comments.
58 $nid = $statistics->entity_id;
59 if (empty($nodes[$nid]->comment_count)
60 || !is_numeric($nodes[$nid]->comment_count)) {
61 $nodes[$nid]->comment_count = $statistics->comment_count;
64 $nodes[$nid]->comment_count += $statistics->comment_count;
66 // Make the last comment timestamp reflect the latest comment.
67 if (!isset($nodes[$nid]->last_comment_timestamp)) {
68 $nodes[$nid]->last_comment_timestamp = $statistics->last_comment_timestamp;
71 $nodes[$nid]->last_comment_timestamp = max($nodes[$nid]->last_comment_timestamp, $statistics->last_comment_timestamp);
76 foreach ($nodes as $node) {
77 // Set the last activity time from tracker data. This also takes into
78 // account comment activity, so getChangedTime() is not used.
79 $node->last_activity = $tracker_data[$node->id()]->changed;
81 // Determine the number of comments.
83 if ($node->comment_count) {
84 $comments = $node->comment_count;
88 'type' => node_get_type_label($node),
92 '#url' => $node->urlInfo(),
93 '#title' => $node->getTitle(),
95 'data-history-node-id' => $node->id(),
96 'data-history-node-timestamp' => $node->getChangedTime(),
100 '#theme' => 'username',
101 '#account' => $node->getOwner(),
105 'class' => ['comments'],
107 'data-history-node-last-comment-timestamp' => $node->last_comment_timestamp,
110 'data' => t('@time ago', [
111 '@time' => \Drupal::service('date.formatter')->formatTimeDiffSince($node->last_activity),
118 // Add node and node owner to cache tags.
119 $cache_tags = Cache::mergeTags($cache_tags, $node->getCacheTags());
120 if ($node->getOwner()) {
121 $cache_tags = Cache::mergeTags($cache_tags, $node->getOwner()->getCacheTags());
126 // Add the list cache tag for nodes.
127 $cache_tags = Cache::mergeTags($cache_tags, \Drupal::entityManager()->getDefinition('node')->getListCacheTags());
131 '#header' => [t('Type'), t('Title'), t('Author'), t('Comments'), t('Last updated')],
133 '#empty' => t('No content available.'),
139 $page['#sorted'] = TRUE;
140 $page['#cache']['tags'] = $cache_tags;
141 $page['#cache']['contexts'][] = 'user.node_grants:view';
143 // Display the reading history if that module is enabled.
144 if (\Drupal::moduleHandler()->moduleExists('history')) {
145 // Reading history is tracked for authenticated users only.
146 if (\Drupal::currentUser()->isAuthenticated()) {
147 $page['#attached']['library'][] = 'tracker/history';
149 $page['#cache']['contexts'][] = 'user.roles:authenticated';