diff --git a/core/modules/tracker/tests/src/Functional/TrackerNodeAccessTest.php b/core/modules/tracker/tests/src/Functional/TrackerNodeAccessTest.php index 2b320ae..19c8a31 100644 --- a/core/modules/tracker/tests/src/Functional/TrackerNodeAccessTest.php +++ b/core/modules/tracker/tests/src/Functional/TrackerNodeAccessTest.php @@ -4,6 +4,7 @@ use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\comment\Tests\CommentTestTrait; +use Drupal\field\Entity\FieldStorageConfig; use Drupal\node\Entity\NodeType; use Drupal\Tests\BrowserTestBase; @@ -61,6 +62,8 @@ public function testTrackerNodeAccess() { $this->drupalGet('user/' . $access_user->id() . '/activity'); $this->assertText($private_node->getTitle(), 'Private node is visible to user with private access.'); $this->assertText($public_node->getTitle(), 'Public node is visible to user with private access.'); + // Comments column displayed. + $this->assertSession()->pageTextContains('Comments'); // User without access should not see private node. $this->drupalLogin($no_access_user); @@ -70,6 +73,19 @@ public function testTrackerNodeAccess() { $this->drupalGet('user/' . $access_user->id() . '/activity'); $this->assertNoText($private_node->getTitle(), 'Private node is not visible to user without private access.'); $this->assertText($public_node->getTitle(), 'Public node is visible to user without private access.'); + + // Ensure module pages works when comment module uninstalled. + $field = FieldStorageConfig::loadByName('node', 'comment'); + $field->delete(); + field_purge_batch(10); + /** @var \Drupal\Core\Extension\ModuleInstallerInterface $service */ + $service = \Drupal::service('module_installer'); + $service->uninstall(['comment']); + $this->drupalLogin($access_user); + $this->drupalGet('activity'); + $this->assertSession()->pageTextContains($private_node->getTitle()); + // Comments column hidden. + $this->assertSession()->pageTextNotContains('Comments'); } } diff --git a/core/modules/tracker/tracker.info.yml b/core/modules/tracker/tracker.info.yml index 79d179d..2a62162 100644 --- a/core/modules/tracker/tracker.info.yml +++ b/core/modules/tracker/tracker.info.yml @@ -3,7 +3,6 @@ type: module description: 'Enables tracking of recent content for users.' dependencies: - node - - comment package: Core version: VERSION core: 8.x diff --git a/core/modules/tracker/tracker.module b/core/modules/tracker/tracker.module index dbde704..d45c53d 100644 --- a/core/modules/tracker/tracker.module +++ b/core/modules/tracker/tracker.module @@ -93,26 +93,27 @@ function tracker_cron() { // Get unique user IDs via entityQueryAggregate because it's the easiest // database agnostic way. We don't actually care about the comments here // so don't add an aggregate field. - $result = \Drupal::entityQueryAggregate('comment') - ->condition('entity_type', 'node') - ->condition('entity_id', $node->id()) - ->condition('uid', $node->getOwnerId(), '<>') - ->condition('status', CommentInterface::PUBLISHED) - ->groupBy('uid') - ->execute(); - if ($result) { - $query = db_insert('tracker_user'); - foreach ($result as $row) { - $query->fields([ - 'uid' => $row['uid'], - 'nid' => $nid, - 'published' => CommentInterface::PUBLISHED, - 'changed' => $changed, - ]); + if (\Drupal::moduleHandler()->moduleExists('comment')) { + $result = \Drupal::entityQueryAggregate('comment') + ->condition('entity_type', 'node') + ->condition('entity_id', $node->id()) + ->condition('uid', $node->getOwnerId(), '<>') + ->condition('status', CommentInterface::PUBLISHED) + ->groupBy('uid') + ->execute(); + if ($result) { + $query = db_insert('tracker_user'); + foreach ($result as $row) { + $query->fields([ + 'uid' => $row['uid'], + 'nid' => $nid, + 'published' => CommentInterface::PUBLISHED, + 'changed' => $changed, + ]); + } + $query->execute(); } - $query->execute(); } - // Note that we have indexed at least one node. $last_nid = $nid; @@ -293,9 +294,11 @@ function _tracker_add($nid, $uid, $changed) { */ function _tracker_calculate_changed($node) { $changed = $node->getChangedTime(); - $latest_comment = \Drupal::service('comment.statistics')->read([$node], 'node', FALSE); - if ($latest_comment && $latest_comment->last_comment_timestamp > $changed) { - $changed = $latest_comment->last_comment_timestamp; + if (\Drupal::hasService('comment.statistics')) { + $latest_comment = \Drupal::service('comment.statistics')->read([$node], 'node', FALSE); + if ($latest_comment && $latest_comment->last_comment_timestamp > $changed) { + $changed = $latest_comment->last_comment_timestamp; + } } return $changed; } diff --git a/core/modules/tracker/tracker.pages.inc b/core/modules/tracker/tracker.pages.inc index 76a775f..36fd397 100644 --- a/core/modules/tracker/tracker.pages.inc +++ b/core/modules/tracker/tracker.pages.inc @@ -48,32 +48,36 @@ function tracker_page($account = NULL) { $cache_tags = []; $rows = []; + $has_comment_stats_service = \Drupal::hasService('comment.statistics'); 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); + if ($has_comment_stats_service) { + $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 @@ -103,18 +107,19 @@ function tracker_page($account = NULL) { '#account' => $node->getOwner(), ], ], - 'comments' => [ + ]; + if ($has_comment_stats_service) { + $row['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), - ]), - ], + ]; + } + $row['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. @@ -130,7 +135,7 @@ function tracker_page($account = NULL) { $page['tracker'] = [ '#rows' => $rows, - '#header' => [t('Type'), t('Title'), t('Author'), t('Comments'), t('Last updated')], + '#header' => $has_comment_stats_service ? [t('Type'), t('Title'), t('Author'), t('Comments'), t('Last updated')] : [t('Type'), t('Title'), t('Author'), t('Last updated')], '#type' => 'table', '#empty' => t('No content available.'), ];