diff -u b/core/modules/tracker/tests/src/Functional/TrackerTest.php b/core/modules/tracker/tests/src/Functional/TrackerTest.php --- b/core/modules/tracker/tests/src/Functional/TrackerTest.php +++ b/core/modules/tracker/tests/src/Functional/TrackerTest.php @@ -155,8 +155,8 @@ ]); $this->drupalGet('user/' . $this->user->id() . '/activity'); - $this->assertSession()->pageTextNotContains($nodes['unpublished']->label(), "Unpublished nodes do not show up in the user's tracker listing."); - $this->assertSession()->pageTextContains($nodes['my_published']->label(), "Published nodes show up in the user's tracker listing."); + $this->assertSession()->pageTextNotContains($nodes['unpublished']->label()); + $this->assertSession()->pageTextContains($nodes['my_published']->label()); // Assert cache contexts. $this->assertCacheContexts([ only in patch2: unchanged: --- a/core/modules/tracker/src/Controller/TrackerController.php +++ b/core/modules/tracker/src/Controller/TrackerController.php @@ -68,7 +68,7 @@ class TrackerController extends ControllerBase { * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager. */ - public function __construct(Connection $database, Connection $databaseReplica, CommentStatisticsInterface $commentStatistics, DateFormatterInterface $dateFormatter, EntityTypeManagerInterface $entityTypeManager) { + public function __construct(Connection $database, Connection $databaseReplica, CommentStatisticsInterface $commentStatistics = NULL, DateFormatterInterface $dateFormatter, EntityTypeManagerInterface $entityTypeManager) { $this->database = $database; $this->databaseReplica = $databaseReplica; $this->commentStatistics = $commentStatistics; @@ -81,10 +81,15 @@ public function __construct(Connection $database, Connection $databaseReplica, C * {@inheritdoc} */ public static function create(ContainerInterface $container) { + $comment_service = NULL; + if (\Drupal::hasService('comment.statistics')) { + $comment_service = $container->get('comment.statistics'); + } + return new static( $container->get('database'), $container->get('database.replica'), - $container->get('comment.statistics'), + $comment_service, $container->get('date.formatter'), $container->get('entity_type.manager') ); @@ -141,6 +146,11 @@ public function buildContent(UserInterface $user = NULL) { ->addMetaData('base_table', 'tracker_node'); } + $comment_installed = FALSE; + if (isset($this->commentStatistics)) { + $comment_installed = TRUE; + } + // This array acts as a placeholder for the data selected later // while keeping the correct order. $tracker_data = $query @@ -160,24 +170,26 @@ public function buildContent(UserInterface $user = NULL) { $nodes = $this->nodeStorage->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($tracker_data[$nid]->comment_count)) { - $tracker_data[$nid]->comment_count = $statistics->comment_count; - } - else { - $tracker_data[$nid]->comment_count += $statistics->comment_count; - } - // Make the last comment timestamp reflect the latest comment. - if (!isset($tracker_data[$nid]->last_comment_timestamp)) { - $tracker_data[$nid]->last_comment_timestamp = $statistics->last_comment_timestamp; - } - else { - $tracker_data[$nid]->last_comment_timestamp = max($tracker_data[$nid]->last_comment_timestamp, $statistics->last_comment_timestamp); + if ($comment_installed) { + $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($tracker_data[$nid]->comment_count)) { + $tracker_data[$nid]->comment_count = $statistics->comment_count; + } + else { + $tracker_data[$nid]->comment_count += $statistics->comment_count; + } + // Make the last comment timestamp reflect the latest comment. + if (!isset($tracker_data[$nid]->last_comment_timestamp)) { + $tracker_data[$nid]->last_comment_timestamp = $statistics->last_comment_timestamp; + } + else { + $tracker_data[$nid]->last_comment_timestamp = max($tracker_data[$nid]->last_comment_timestamp, $statistics->last_comment_timestamp); + } } } @@ -205,11 +217,6 @@ public function buildContent(UserInterface $user = NULL) { '#account' => $owner, ], ], - 'comments' => [ - 'class' => ['comments'], - 'data' => $tracker_data[$node->id()]->comment_count ?? 0, - 'data-history-node-last-comment-timestamp' => $tracker_data[$node->id()]->last_comment_timestamp ?? 0, - ], 'last updated' => [ 'data' => t('@time ago', [ '@time' => $this->dateFormatter->formatTimeDiffSince($last_activity), @@ -217,6 +224,17 @@ public function buildContent(UserInterface $user = NULL) { ], ]; + if ($comment_installed) { + $comment_row = [ + 'comments' => [ + 'class' => ['comments'], + 'data' => $tracker_data[$node->id()]->comment_count ?? 0, + 'data-history-node-last-comment-timestamp' => $tracker_data[$node->id()]->last_comment_timestamp ?? 0, + ], + ]; + $row = array_slice($row, 0, 3) + $comment_row + array_slice($row, 3); + } + $rows[] = $row; // Add node and node owner to cache tags. @@ -236,12 +254,19 @@ public function buildContent(UserInterface $user = NULL) { $this->t('Type'), $this->t('Title'), $this->t('Author'), - $this->t('Comments'), $this->t('Last updated'), ], '#type' => 'table', '#empty' => $this->t('No content available.'), ]; + + if ($comment_installed) { + $comment_header = [$this->t('Comments')]; + $beginning = array_slice($page['tracker']['#header'], 0, 3); + $end = array_slice($page['tracker']['#header'], 3); + $page['tracker']['#header'] = array_merge($beginning, $comment_header, $end); + } + $page['pager'] = [ '#type' => 'pager', '#weight' => 10,