diff --git a/core/modules/comment/comment.services.yml b/core/modules/comment/comment.services.yml index 0b320d3ca4..45792e390b 100644 --- a/core/modules/comment/comment.services.yml +++ b/core/modules/comment/comment.services.yml @@ -7,9 +7,7 @@ services: comment.manager: class: Drupal\comment\CommentManager - arguments: ['@entity_type.manager', '@config.factory', '@string_translation', '@module_handler', '@current_user', '@entity_field.manager', '@entity_display.repository'] - calls: - - [setHistoryRepositoryService, ['@?history.repository']] + arguments: ['@entity_type.manager', '@config.factory', '@string_translation', '@module_handler', '@current_user', '@entity_field.manager', '@entity_display.repository', '@?history.repository'] comment.statistics: class: Drupal\comment\CommentStatistics @@ -23,6 +21,4 @@ services: comment.link_builder: class: Drupal\comment\CommentLinkBuilder - arguments: ['@current_user', '@comment.manager', '@module_handler', '@string_translation', '@entity_type.manager'] - calls: - - [setHistoryRepositoryService, ['@?history.repository']] + arguments: ['@current_user', '@comment.manager', '@module_handler', '@string_translation', '@entity_type.manager', '@?history.repository'] diff --git a/core/modules/comment/comment.views.inc b/core/modules/comment/comment.views.inc index e5df31a1a0..0d36fa6204 100644 --- a/core/modules/comment/comment.views.inc +++ b/core/modules/comment/comment.views.inc @@ -11,8 +11,7 @@ * Implements hook_views_data_alter(). */ function comment_views_data_alter(&$data) { - // New comments are only supported for node table because it requires the - // history table. + // New comments are supported for node table when the history module enabled. $data['node']['new_comments'] = [ 'title' => t('New comments'), 'help' => t('The number of new comments on the node.'), diff --git a/core/modules/comment/src/CommentLinkBuilder.php b/core/modules/comment/src/CommentLinkBuilder.php index 65df2113ce..7ce7bffe24 100644 --- a/core/modules/comment/src/CommentLinkBuilder.php +++ b/core/modules/comment/src/CommentLinkBuilder.php @@ -70,24 +70,15 @@ class CommentLinkBuilder implements CommentLinkBuilderInterface { * String translation service. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. + * @param \Drupal\history\HistoryRepositoryInterface $history_repository + * (Optional) The history repository service. */ - public function __construct(AccountInterface $current_user, CommentManagerInterface $comment_manager, ModuleHandlerInterface $module_handler, TranslationInterface $string_translation, EntityTypeManagerInterface $entity_type_manager) { + public function __construct(AccountInterface $current_user, CommentManagerInterface $comment_manager, ModuleHandlerInterface $module_handler, TranslationInterface $string_translation, EntityTypeManagerInterface $entity_type_manager, HistoryRepositoryInterface $history_repository = NULL) { $this->currentUser = $current_user; $this->commentManager = $comment_manager; $this->moduleHandler = $module_handler; $this->stringTranslation = $string_translation; $this->entityTypeManager = $entity_type_manager; - } - - /** - * Set the history repository service. - * - * @param \Drupal\history\HistoryRepositoryInterface $history_repository - * The history repository service. - * - * @see https://www.drupal.org/node/2081585 - */ - public function setHistoryRepositoryService(HistoryRepositoryInterface $history_repository) { $this->historyRepository = $history_repository; } diff --git a/core/modules/comment/src/CommentManager.php b/core/modules/comment/src/CommentManager.php index e458ee09af..3569e45435 100644 --- a/core/modules/comment/src/CommentManager.php +++ b/core/modules/comment/src/CommentManager.php @@ -99,8 +99,10 @@ class CommentManager implements CommentManagerInterface { * The entity field manager service. * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository * The entity display repository service. + * @param \Drupal\history\HistoryRepositoryInterface $history_repository + * (Optional) The history repository service. */ - public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, AccountInterface $current_user, EntityFieldManagerInterface $entity_field_manager, EntityDisplayRepositoryInterface $entity_display_repository) { + public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, AccountInterface $current_user, EntityFieldManagerInterface $entity_field_manager, EntityDisplayRepositoryInterface $entity_display_repository, HistoryRepositoryInterface $history_repository = NULL) { $this->entityTypeManager = $entity_type_manager; $this->userConfig = $config_factory->get('user.settings'); $this->stringTranslation = $string_translation; @@ -108,17 +110,6 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager, Con $this->currentUser = $current_user; $this->entityFieldManager = $entity_field_manager; $this->entityDisplayRepository = $entity_display_repository; - } - - /** - * Set the history repository service. - * - * @param \Drupal\history\HistoryRepositoryInterface $history_repository - * The history repository service. - * - * @see https://www.drupal.org/node/2081585 - */ - public function setHistoryRepositoryService(HistoryRepositoryInterface $history_repository) { $this->historyRepository = $history_repository; } diff --git a/core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php b/core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php index 15586c2a9a..efb82b013b 100644 --- a/core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php +++ b/core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php @@ -83,8 +83,7 @@ protected function setUp(): void { $this->historyRepository = $this->createMock(HistoryRepositoryInterface::class); $this->moduleHandler = $this->createMock('\Drupal\Core\Extension\ModuleHandlerInterface'); $this->currentUser = $this->createMock('\Drupal\Core\Session\AccountProxyInterface'); - $this->commentLinkBuilder = new CommentLinkBuilder($this->currentUser, $this->commentManager, $this->moduleHandler, $this->stringTranslation, $this->entityTypeManager); - $this->commentLinkBuilder->setHistoryRepositoryService($this->historyRepository); + $this->commentLinkBuilder = new CommentLinkBuilder($this->currentUser, $this->commentManager, $this->moduleHandler, $this->stringTranslation, $this->entityTypeManager, $this->historyRepository); $this->commentManager->expects($this->any()) ->method('getFields') ->with('node') diff --git a/core/modules/comment/tests/src/Unit/CommentManagerTest.php b/core/modules/comment/tests/src/Unit/CommentManagerTest.php index dcc41593d3..c345bc8782 100644 --- a/core/modules/comment/tests/src/Unit/CommentManagerTest.php +++ b/core/modules/comment/tests/src/Unit/CommentManagerTest.php @@ -53,7 +53,7 @@ public function testGetFields() { $history_repository->expects($this->any()) ->method('getLastViewed') - ->willReturn(0); + ->willReturn([]); $comment_manager = new CommentManager( $entity_type_manager, @@ -62,9 +62,9 @@ public function testGetFields() { $this->createMock('Drupal\Core\Extension\ModuleHandlerInterface'), $this->createMock(AccountInterface::class), $entity_field_manager, - $this->prophesize(EntityDisplayRepositoryInterface::class)->reveal() + $this->prophesize(EntityDisplayRepositoryInterface::class)->reveal(), + $history_repository ); - $comment_manager->setHistoryRepositoryService($history_repository); $comment_fields = $comment_manager->getFields('node'); $this->assertArrayHasKey('field_foobar', $comment_fields); } diff --git a/core/modules/history/src/HistoryRepository.php b/core/modules/history/src/HistoryRepository.php index 96872c3366..de37eb15e7 100644 --- a/core/modules/history/src/HistoryRepository.php +++ b/core/modules/history/src/HistoryRepository.php @@ -14,13 +14,6 @@ */ class HistoryRepository implements HistoryRepositoryInterface { - /** - * An array of history IDs keyed by entity type and entity id. - * - * @var array - */ - protected $history = []; - /** * The database connection. * @@ -71,7 +64,7 @@ public function __construct(Connection $connection, TimeInterface $time, MemoryC /** * {@inheritdoc} */ - public function getLastViewed($entity_type, array $entity_ids) { + public function getLastViewed(string $entity_type, array $entity_ids): array { $entities = []; $entities_to_read = []; $user_id = $this->currentUser->id(); @@ -116,7 +109,7 @@ public function getLastViewed($entity_type, array $entity_ids) { /** * {@inheritdoc} */ - public function updateLastViewed(EntityInterface $entity) { + public function updateLastViewed(EntityInterface $entity): HistoryRepositoryInterface { if ($this->currentUser->isAuthenticated()) { $user_id = $this->currentUser->id(); $time = $this->time->getRequestTime(); @@ -153,17 +146,17 @@ public function updateLastViewed(EntityInterface $entity) { * @return string * Cache ID that can be passed to the cache backend. */ - protected function buildCacheId($uid, $entity_type, $entity_id) { + protected function buildCacheId($uid, $entity_type, $entity_id): string { return implode(':', ['history', $uid, $entity_type, $entity_id]); } /** * {@inheritdoc} */ - public function getCacheTags($uid, $entity_id) { + public function getCacheTags($user_id, $entity_id): array { return [ 'history', - "history:user:{$uid}", + "history:user:{$user_id}", "history:entity:{$entity_id}", ]; } @@ -171,7 +164,7 @@ public function getCacheTags($uid, $entity_id) { /** * {@inheritdoc} */ - public function purge() { + public function purge(): void { $this->connection->delete('history') ->condition('timestamp', HISTORY_READ_LIMIT, '<') ->execute(); @@ -182,7 +175,7 @@ public function purge() { /** * {@inheritdoc} */ - public function deleteByUser(AccountInterface $account) { + public function deleteByUser(AccountInterface $account): void { $this->connection->delete('history') ->condition('uid', $account->id()) ->execute(); @@ -193,7 +186,7 @@ public function deleteByUser(AccountInterface $account) { /** * {@inheritdoc} */ - public function deleteByEntity(EntityInterface $entity) { + public function deleteByEntity(EntityInterface $entity): void { $this->connection->delete('history') ->condition('entity_id', $entity->id()) ->condition('entity_type', $entity->getEntityTypeId()) diff --git a/core/modules/history/src/HistoryRepositoryInterface.php b/core/modules/history/src/HistoryRepositoryInterface.php index 679e9605e9..3ad66da550 100644 --- a/core/modules/history/src/HistoryRepositoryInterface.php +++ b/core/modules/history/src/HistoryRepositoryInterface.php @@ -21,9 +21,9 @@ interface HistoryRepositoryInterface { * @return array * Array of timestamps keyed by entity ID. If a entity has been previously * viewed by the user, the timestamp in seconds of when the last view - * occurred; otherwise, zero. + * occurred. */ - public function getLastViewed($entity_type, array $entity_ids); + public function getLastViewed(string $entity_type, array $entity_ids): array; /** * Updates 'last viewed' timestamp of the entity for the user account. @@ -33,25 +33,25 @@ public function getLastViewed($entity_type, array $entity_ids); * * @return self */ - public function updateLastViewed(EntityInterface $entity); + public function updateLastViewed(EntityInterface $entity): HistoryRepositoryInterface; /** * Gets an array of cache tags for the history timestamp. * - * @param int $uid + * @param int|string $user_id * The User ID. - * @param int $entity_id + * @param int|string $entity_id * The entity ID. * * @return string[] * An array of cache tags based on the current view. */ - public function getCacheTags($uid, $entity_id); + public function getCacheTags($user_id, $entity_id): array; /** * Purges outdated history. */ - public function purge(); + public function purge(): void; /** * Deletes the history for the given user account. @@ -59,7 +59,7 @@ public function purge(); * @param \Drupal\Core\Session\AccountInterface $account * The user account to purge history. */ - public function deleteByUser(AccountInterface $account); + public function deleteByUser(AccountInterface $account): void; /** * Deletes the history for the given entity. @@ -67,6 +67,6 @@ public function deleteByUser(AccountInterface $account); * @param \Drupal\Core\Entity\EntityInterface $entity * The entity that history should be deleted. */ - public function deleteByEntity(EntityInterface $entity); + public function deleteByEntity(EntityInterface $entity): void; }