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.
25 function tracker_page($account = NULL) {
27 $query = db_select('tracker_user', 't')
28 ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
29 ->addMetaData('base_table', 'tracker_user')
30 ->condition('t.uid', $account->id());
33 $query = db_select('tracker_node', 't', ['target' => 'replica'])
34 ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
35 ->addMetaData('base_table', 'tracker_node');
38 // This array acts as a placeholder for the data selected later
39 // while keeping the correct order.
40 $tracker_data = $query
41 ->addTag('node_access')
42 ->fields('t', ['nid', 'changed'])
43 ->condition('t.published', 1)
44 ->orderBy('t.changed', 'DESC')
47 ->fetchAllAssoc('nid');
51 if (!empty($tracker_data)) {
52 // Load nodes into an array with the same order as $tracker_data.
53 $nodes = Node::loadMultiple(array_keys($tracker_data));
55 // Enrich the node data.
56 $result = \Drupal::service('comment.statistics')->read($nodes, 'node', FALSE);
57 foreach ($result as $statistics) {
58 // The node ID may not be unique; there can be multiple comment fields.
59 // Make comment_count the total of all comments.
60 $nid = $statistics->entity_id;
61 if (empty($nodes[$nid]->comment_count)
62 || !is_numeric($nodes[$nid]->comment_count)) {
63 $nodes[$nid]->comment_count = $statistics->comment_count;
66 $nodes[$nid]->comment_count += $statistics->comment_count;
68 // Make the last comment timestamp reflect the latest comment.
69 if (!isset($nodes[$nid]->last_comment_timestamp)) {
70 $nodes[$nid]->last_comment_timestamp = $statistics->last_comment_timestamp;
73 $nodes[$nid]->last_comment_timestamp = max($nodes[$nid]->last_comment_timestamp, $statistics->last_comment_timestamp);
78 foreach ($nodes as $node) {
79 // Set the last activity time from tracker data. This also takes into
80 // account comment activity, so getChangedTime() is not used.
81 $node->last_activity = $tracker_data[$node->id()]->changed;
83 // Determine the number of comments.
85 if ($node->comment_count) {
86 $comments = $node->comment_count;
90 'type' => node_get_type_label($node),
94 '#url' => $node->urlInfo(),
95 '#title' => $node->getTitle(),
97 'data-history-node-id' => $node->id(),
98 'data-history-node-timestamp' => $node->getChangedTime(),
102 '#theme' => 'username',
103 '#account' => $node->getOwner(),
107 'class' => ['comments'],
109 'data-history-node-last-comment-timestamp' => $node->last_comment_timestamp,
112 'data' => t('@time ago', [
113 '@time' => \Drupal::service('date.formatter')->formatTimeDiffSince($node->last_activity),
120 // Add node and node owner to cache tags.
121 $cache_tags = Cache::mergeTags($cache_tags, $node->getCacheTags());
122 if ($node->getOwner()) {
123 $cache_tags = Cache::mergeTags($cache_tags, $node->getOwner()->getCacheTags());
128 // Add the list cache tag for nodes.
129 $cache_tags = Cache::mergeTags($cache_tags, \Drupal::entityManager()->getDefinition('node')->getListCacheTags());
133 '#header' => [t('Type'), t('Title'), t('Author'), t('Comments'), t('Last updated')],
135 '#empty' => t('No content available.'),
141 $page['#sorted'] = TRUE;
142 $page['#cache']['tags'] = $cache_tags;
143 $page['#cache']['contexts'][] = 'user.node_grants:view';
145 // Display the reading history if that module is enabled.
146 if (\Drupal::moduleHandler()->moduleExists('history')) {
147 // Reading history is tracked for authenticated users only.
148 if (\Drupal::currentUser()->isAuthenticated()) {
149 $page['#attached']['library'][] = 'tracker/history';
151 $page['#cache']['contexts'][] = 'user.roles:authenticated';