diff --git a/core/modules/tracker/src/Controller/TrackerBase.php b/core/modules/tracker/src/Controller/TrackerBase.php new file mode 100644 index 0000000000..407b31f084 --- /dev/null +++ b/core/modules/tracker/src/Controller/TrackerBase.php @@ -0,0 +1,226 @@ +database = $database; + $this->databaseReplica = $databaseReplica; + $this->commentStatistics = $commentStatistics; + $this->dateFormatter = $dateFormatter; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('database'), + $container->get('database.replica'), + $container->get('comment.statistics'), + $container->get('date.formatter') + ); + } + + /** + * Builds content for the tracker controllers. + * + * @param \Drupal\Core\Session\AccountInterface|null $account + * (optional) The user account. + * + * @return array + * The render array. + */ + public function buildContent(AccountInterface $account = NULL) { + if ($account) { + $query = $this->database->select('tracker_user', 't') + ->extend(PagerSelectExtender::class) + ->addMetaData('base_table', 'tracker_user') + ->condition('t.uid', $account->id()); + } + else { + $query = $this->databaseReplica->select('tracker_node', 't') + ->extend(PagerSelectExtender::class) + ->addMetaData('base_table', 'tracker_node'); + } + + // This array acts as a placeholder for the data selected later + // while keeping the correct order. + $tracker_data = $query + ->addTag('node_access') + ->fields('t', ['nid', 'changed']) + ->condition('t.published', 1) + ->orderBy('t.changed', 'DESC') + ->limit(25) + ->execute() + ->fetchAllAssoc('nid'); + + $cache_tags = []; + $rows = []; + if (!empty($tracker_data)) { + // Load nodes into an array with the same order as $tracker_data. + $nodes = Node::loadMultiple(array_keys($tracker_data)); + + // Enrich the node data. + $result = $this->commentStatistics->read($nodes, 'node', FALSE); + foreach ($result as $statistics) { + // The node ID may not be unique; there can be multiple comment fields. + // Make comment_count the total of all comments. + $nid = $statistics->entity_id; + if (empty($nodes[$nid]->comment_count) + || !is_numeric($nodes[$nid]->comment_count)) { + $nodes[$nid]->comment_count = $statistics->comment_count; + } + else { + $nodes[$nid]->comment_count += $statistics->comment_count; + } + // Make the last comment timestamp reflect the latest comment. + if (!isset($nodes[$nid]->last_comment_timestamp)) { + $nodes[$nid]->last_comment_timestamp = $statistics->last_comment_timestamp; + } + else { + $nodes[$nid]->last_comment_timestamp = max($nodes[$nid]->last_comment_timestamp, $statistics->last_comment_timestamp); + } + } + + // Display the data. + foreach ($nodes as $node) { + // Set the last activity time from tracker data. This also takes into + // account comment activity, so getChangedTime() is not used. + $node->last_activity = $tracker_data[$node->id()]->changed; + + // Determine the number of comments. + $comments = 0; + if ($node->comment_count) { + $comments = $node->comment_count; + } + + $row = [ + 'type' => node_get_type_label($node), + 'title' => [ + 'data' => [ + '#type' => 'link', + '#url' => $node->urlInfo(), + '#title' => $node->getTitle(), + ], + 'data-history-node-id' => $node->id(), + 'data-history-node-timestamp' => $node->getChangedTime(), + ], + 'author' => [ + 'data' => [ + '#theme' => 'username', + '#account' => $node->getOwner(), + ], + ], + 'comments' => [ + 'class' => ['comments'], + 'data' => $comments, + 'data-history-node-last-comment-timestamp' => $node->last_comment_timestamp, + ], + 'last updated' => [ + 'data' => t('@time ago', [ + '@time' => $this->dateFormatter->formatTimeDiffSince($node->last_activity), + ]), + ], + ]; + + $rows[] = $row; + + // Add node and node owner to cache tags. + $cache_tags = Cache::mergeTags($cache_tags, $node->getCacheTags()); + if ($node->getOwner()) { + $cache_tags = Cache::mergeTags($cache_tags, $node->getOwner()->getCacheTags()); + } + } + } + + // Add the list cache tag for nodes. + $cache_tags = Cache::mergeTags($cache_tags, $this->entityTypeManager()->getDefinition('node')->getListCacheTags()); + + $page['tracker'] = [ + '#rows' => $rows, + '#header' => [ + $this->t('Type'), + $this->t('Title'), + $this->t('Author'), + $this->t('Comments'), + $this->t('Last updated'), + ], + '#type' => 'table', + '#empty' => $this->t('No content available.'), + ]; + $page['pager'] = [ + '#type' => 'pager', + '#weight' => 10, + ]; + $page['#sorted'] = TRUE; + $page['#cache']['tags'] = $cache_tags; + $page['#cache']['contexts'][] = 'user.node_grants:view'; + + // Display the reading history if that module is enabled. + if ($this->moduleHandler()->moduleExists('history')) { + // Reading history is tracked for authenticated users only. + if ($this->currentUser()->isAuthenticated()) { + $page['#attached']['library'][] = 'tracker/history'; + } + $page['#cache']['contexts'][] = 'user.roles:authenticated'; + } + + return $page; + } + +} diff --git a/core/modules/tracker/src/Controller/TrackerPage.php b/core/modules/tracker/src/Controller/TrackerPage.php index 03b077dbdd..f51da68615 100644 --- a/core/modules/tracker/src/Controller/TrackerPage.php +++ b/core/modules/tracker/src/Controller/TrackerPage.php @@ -2,19 +2,16 @@ namespace Drupal\tracker\Controller; -use Drupal\Core\Controller\ControllerBase; - /** * Controller for tracker.page route. */ -class TrackerPage extends ControllerBase { +class TrackerPage extends TrackerBase { /** * Content callback for the tracker.page route. */ public function getContent() { - module_load_include('inc', 'tracker', 'tracker.pages'); - return tracker_page(); + return $this->buildContent(); } } diff --git a/core/modules/tracker/src/Controller/TrackerUserRecent.php b/core/modules/tracker/src/Controller/TrackerUserRecent.php index ff3d4b02f0..cdb6e55285 100644 --- a/core/modules/tracker/src/Controller/TrackerUserRecent.php +++ b/core/modules/tracker/src/Controller/TrackerUserRecent.php @@ -3,21 +3,19 @@ namespace Drupal\tracker\Controller; use Drupal\Core\Access\AccessResult; -use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Session\AccountInterface; use Drupal\user\UserInterface; /** * Controller for tracker.users_recent_content route. */ -class TrackerUserRecent extends ControllerBase { +class TrackerUserRecent extends TrackerBase { /** * Content callback for the tracker.users_recent_content route. */ public function getContent(UserInterface $user) { - module_load_include('inc', 'tracker', 'tracker.pages'); - return tracker_page($user); + return $this->buildContent($user); } /** diff --git a/core/modules/tracker/src/Controller/TrackerUserTab.php b/core/modules/tracker/src/Controller/TrackerUserTab.php index c57aff0655..e6d3b42db4 100644 --- a/core/modules/tracker/src/Controller/TrackerUserTab.php +++ b/core/modules/tracker/src/Controller/TrackerUserTab.php @@ -2,20 +2,18 @@ namespace Drupal\tracker\Controller; -use Drupal\Core\Controller\ControllerBase; use Drupal\user\UserInterface; /** * Controller for tracker.user_tab route. */ -class TrackerUserTab extends ControllerBase { +class TrackerUserTab extends TrackerBase { /** * Content callback for the tracker.user_tab route. */ public function getContent(UserInterface $user) { - module_load_include('inc', 'tracker', 'tracker.pages'); - return tracker_page($user); + return $this->buildContent($user); } /** diff --git a/core/modules/tracker/tracker.pages.inc b/core/modules/tracker/tracker.pages.inc deleted file mode 100644 index 9093776de2..0000000000 --- a/core/modules/tracker/tracker.pages.inc +++ /dev/null @@ -1,153 +0,0 @@ -select('tracker_user', 't') - ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->addMetaData('base_table', 'tracker_user') - ->condition('t.uid', $account->id()); - } - else { - $query = \Drupal::service('database.replica')->select('tracker_node', 't') - ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->addMetaData('base_table', 'tracker_node'); - } - - // This array acts as a placeholder for the data selected later - // while keeping the correct order. - $tracker_data = $query - ->addTag('node_access') - ->fields('t', ['nid', 'changed']) - ->condition('t.published', 1) - ->orderBy('t.changed', 'DESC') - ->limit(25) - ->execute() - ->fetchAllAssoc('nid'); - - $cache_tags = []; - $rows = []; - if (!empty($tracker_data)) { - // Load nodes into an array with the same order as $tracker_data. - $nodes = Node::loadMultiple(array_keys($tracker_data)); - - // Enrich the node data. - $result = \Drupal::service('comment.statistics')->read($nodes, 'node', FALSE); - foreach ($result as $statistics) { - // The node ID may not be unique; there can be multiple comment fields. - // Make comment_count the total of all comments. - $nid = $statistics->entity_id; - if (empty($nodes[$nid]->comment_count) - || !is_numeric($nodes[$nid]->comment_count)) { - $nodes[$nid]->comment_count = $statistics->comment_count; - } - else { - $nodes[$nid]->comment_count += $statistics->comment_count; - } - // Make the last comment timestamp reflect the latest comment. - if (!isset($nodes[$nid]->last_comment_timestamp)) { - $nodes[$nid]->last_comment_timestamp = $statistics->last_comment_timestamp; - } - else { - $nodes[$nid]->last_comment_timestamp = max($nodes[$nid]->last_comment_timestamp, $statistics->last_comment_timestamp); - } - } - - // Display the data. - foreach ($nodes as $node) { - // Set the last activity time from tracker data. This also takes into - // account comment activity, so getChangedTime() is not used. - $node->last_activity = $tracker_data[$node->id()]->changed; - - // Determine the number of comments. - $comments = 0; - if ($node->comment_count) { - $comments = $node->comment_count; - } - - $row = [ - 'type' => node_get_type_label($node), - 'title' => [ - 'data' => [ - '#type' => 'link', - '#url' => $node->urlInfo(), - '#title' => $node->getTitle(), - ], - 'data-history-node-id' => $node->id(), - 'data-history-node-timestamp' => $node->getChangedTime(), - ], - 'author' => [ - 'data' => [ - '#theme' => 'username', - '#account' => $node->getOwner(), - ], - ], - 'comments' => [ - 'class' => ['comments'], - 'data' => $comments, - 'data-history-node-last-comment-timestamp' => $node->last_comment_timestamp, - ], - 'last updated' => [ - 'data' => t('@time ago', [ - '@time' => \Drupal::service('date.formatter')->formatTimeDiffSince($node->last_activity), - ]), - ], - ]; - - $rows[] = $row; - - // Add node and node owner to cache tags. - $cache_tags = Cache::mergeTags($cache_tags, $node->getCacheTags()); - if ($node->getOwner()) { - $cache_tags = Cache::mergeTags($cache_tags, $node->getOwner()->getCacheTags()); - } - } - } - - // Add the list cache tag for nodes. - $cache_tags = Cache::mergeTags($cache_tags, \Drupal::entityManager()->getDefinition('node')->getListCacheTags()); - - $page['tracker'] = [ - '#rows' => $rows, - '#header' => [t('Type'), t('Title'), t('Author'), t('Comments'), t('Last updated')], - '#type' => 'table', - '#empty' => t('No content available.'), - ]; - $page['pager'] = [ - '#type' => 'pager', - '#weight' => 10, - ]; - $page['#sorted'] = TRUE; - $page['#cache']['tags'] = $cache_tags; - $page['#cache']['contexts'][] = 'user.node_grants:view'; - - // Display the reading history if that module is enabled. - if (\Drupal::moduleHandler()->moduleExists('history')) { - // Reading history is tracked for authenticated users only. - if (\Drupal::currentUser()->isAuthenticated()) { - $page['#attached']['library'][] = 'tracker/history'; - } - $page['#cache']['contexts'][] = 'user.roles:authenticated'; - } - - return $page; -}