tracker.pages.inc 4.9 KB

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