Drupal investigation

tracker.pages.inc 4.9KB

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