Chris@0: select('tracker_user', 't') Chris@0: ->extend('Drupal\Core\Database\Query\PagerSelectExtender') Chris@0: ->addMetaData('base_table', 'tracker_user') Chris@0: ->condition('t.uid', $account->id()); Chris@0: } Chris@0: else { Chris@18: $query = \Drupal::service('database.replica')->select('tracker_node', 't') Chris@0: ->extend('Drupal\Core\Database\Query\PagerSelectExtender') Chris@0: ->addMetaData('base_table', 'tracker_node'); Chris@0: } Chris@0: Chris@0: // This array acts as a placeholder for the data selected later Chris@0: // while keeping the correct order. Chris@0: $tracker_data = $query Chris@0: ->addTag('node_access') Chris@0: ->fields('t', ['nid', 'changed']) Chris@0: ->condition('t.published', 1) Chris@0: ->orderBy('t.changed', 'DESC') Chris@0: ->limit(25) Chris@0: ->execute() Chris@0: ->fetchAllAssoc('nid'); Chris@0: Chris@0: $cache_tags = []; Chris@0: $rows = []; Chris@0: if (!empty($tracker_data)) { Chris@0: // Load nodes into an array with the same order as $tracker_data. Chris@0: $nodes = Node::loadMultiple(array_keys($tracker_data)); Chris@0: Chris@0: // Enrich the node data. Chris@0: $result = \Drupal::service('comment.statistics')->read($nodes, 'node', FALSE); Chris@0: foreach ($result as $statistics) { Chris@0: // The node ID may not be unique; there can be multiple comment fields. Chris@0: // Make comment_count the total of all comments. Chris@0: $nid = $statistics->entity_id; Chris@0: if (empty($nodes[$nid]->comment_count) Chris@0: || !is_numeric($nodes[$nid]->comment_count)) { Chris@0: $nodes[$nid]->comment_count = $statistics->comment_count; Chris@0: } Chris@0: else { Chris@0: $nodes[$nid]->comment_count += $statistics->comment_count; Chris@0: } Chris@0: // Make the last comment timestamp reflect the latest comment. Chris@0: if (!isset($nodes[$nid]->last_comment_timestamp)) { Chris@0: $nodes[$nid]->last_comment_timestamp = $statistics->last_comment_timestamp; Chris@0: } Chris@0: else { Chris@0: $nodes[$nid]->last_comment_timestamp = max($nodes[$nid]->last_comment_timestamp, $statistics->last_comment_timestamp); Chris@0: } Chris@0: } Chris@0: Chris@0: // Display the data. Chris@0: foreach ($nodes as $node) { Chris@0: // Set the last activity time from tracker data. This also takes into Chris@0: // account comment activity, so getChangedTime() is not used. Chris@0: $node->last_activity = $tracker_data[$node->id()]->changed; Chris@0: Chris@0: // Determine the number of comments. Chris@0: $comments = 0; Chris@0: if ($node->comment_count) { Chris@0: $comments = $node->comment_count; Chris@0: } Chris@0: Chris@0: $row = [ Chris@0: 'type' => node_get_type_label($node), Chris@0: 'title' => [ Chris@0: 'data' => [ Chris@0: '#type' => 'link', Chris@18: '#url' => $node->toUrl(), Chris@0: '#title' => $node->getTitle(), Chris@0: ], Chris@0: 'data-history-node-id' => $node->id(), Chris@0: 'data-history-node-timestamp' => $node->getChangedTime(), Chris@0: ], Chris@0: 'author' => [ Chris@0: 'data' => [ Chris@0: '#theme' => 'username', Chris@0: '#account' => $node->getOwner(), Chris@0: ], Chris@0: ], Chris@0: 'comments' => [ Chris@0: 'class' => ['comments'], Chris@0: 'data' => $comments, Chris@0: 'data-history-node-last-comment-timestamp' => $node->last_comment_timestamp, Chris@0: ], Chris@0: 'last updated' => [ Chris@0: 'data' => t('@time ago', [ Chris@0: '@time' => \Drupal::service('date.formatter')->formatTimeDiffSince($node->last_activity), Chris@0: ]), Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $rows[] = $row; Chris@0: Chris@0: // Add node and node owner to cache tags. Chris@0: $cache_tags = Cache::mergeTags($cache_tags, $node->getCacheTags()); Chris@0: if ($node->getOwner()) { Chris@0: $cache_tags = Cache::mergeTags($cache_tags, $node->getOwner()->getCacheTags()); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // Add the list cache tag for nodes. Chris@0: $cache_tags = Cache::mergeTags($cache_tags, \Drupal::entityManager()->getDefinition('node')->getListCacheTags()); Chris@0: Chris@0: $page['tracker'] = [ Chris@0: '#rows' => $rows, Chris@0: '#header' => [t('Type'), t('Title'), t('Author'), t('Comments'), t('Last updated')], Chris@0: '#type' => 'table', Chris@0: '#empty' => t('No content available.'), Chris@0: ]; Chris@0: $page['pager'] = [ Chris@0: '#type' => 'pager', Chris@0: '#weight' => 10, Chris@0: ]; Chris@0: $page['#sorted'] = TRUE; Chris@0: $page['#cache']['tags'] = $cache_tags; Chris@0: $page['#cache']['contexts'][] = 'user.node_grants:view'; Chris@0: Chris@0: // Display the reading history if that module is enabled. Chris@0: if (\Drupal::moduleHandler()->moduleExists('history')) { Chris@0: // Reading history is tracked for authenticated users only. Chris@0: if (\Drupal::currentUser()->isAuthenticated()) { Chris@0: $page['#attached']['library'][] = 'tracker/history'; Chris@0: } Chris@0: $page['#cache']['contexts'][] = 'user.roles:authenticated'; Chris@0: } Chris@0: Chris@0: return $page; Chris@0: }