diff --git a/core/modules/comment/comment.services.yml b/core/modules/comment/comment.services.yml index ec127e5..5a8051d 100644 --- a/core/modules/comment/comment.services.yml +++ b/core/modules/comment/comment.services.yml @@ -13,4 +13,4 @@ services: comment.manager: class: Drupal\comment\CommentManager - arguments: ['@field.info', '@entity.manager'] + arguments: ['@field.info', '@entity.manager', '@database', '@module_handler', '@current_user'] diff --git a/core/modules/comment/lib/Drupal/comment/CommentManager.php b/core/modules/comment/lib/Drupal/comment/CommentManager.php index 35f723c..6d59775 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentManager.php +++ b/core/modules/comment/lib/Drupal/comment/CommentManager.php @@ -10,6 +10,9 @@ use Drupal\Component\Utility\String; use Drupal\field\FieldInfo; use Drupal\Core\Entity\EntityManager; +use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Database\Connection; +use Drupal\Core\Session\AccountInterface; /** * Comment manager contains common functions to manage comment fields. @@ -31,6 +34,27 @@ class CommentManager { protected $entityManager; /** + * Database connection + * + * @var \Drupal\Core\Database\Connection + */ + protected $connection; + + /** + * The module handler. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** + * The current user service. + * + * @var \Drupal\Core\Session\AccountInterface + */ + protected $currentUser; + + /** * Construct the CommentManager object. * * @param \Drupal\field\FieldInfo $field_info @@ -38,9 +62,12 @@ class CommentManager { * @param \Drupal\Core\Entity\EntityManager $entity_manager * The entity manager service. */ - public function __construct(FieldInfo $field_info, EntityManager $entity_manager) { + public function __construct(FieldInfo $field_info, EntityManager $entity_manager, Connection $connection, ModuleHandlerInterface $module_handler, AccountInterface $current_user) { $this->fieldInfo = $field_info; $this->entityManager = $entity_manager; + $this->connection = $connection; + $this->moduleHandler = $module_handler; + $this->currentUser = $current_user; } /** @@ -244,8 +271,6 @@ public function getFieldUIPageTitle($field_name) { /** * Returns the number of new comments for an user and the specified entity. * - * @todo Help, this uses procedural code! We've to get rid of that. - * * @param int $entity_id * Entity ID of the entity to which the comments are attached. * @param string $entity_type @@ -260,9 +285,7 @@ public function getFieldUIPageTitle($field_name) { * The number of new comments or FALSE if the user is not logged in. */ public function countNewComments($entity_id, $entity_type, $field_name = NULL, $timestamp = 0) { - $user = \Drupal::currentUser(); - - if ($user->isAuthenticated() && \Drupal::moduleHandler()->moduleExists('history')) { + if ($this->currentUser->isAuthenticated() && $this->moduleHandler->moduleExists('history')) { // Retrieve the timestamp when the current user last viewed this entity. if (!$timestamp) { if ($entity_type == 'node') { @@ -283,7 +306,7 @@ public function countNewComments($entity_id, $entity_type, $field_name = NULL, $ $timestamp = ($timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT); // Use the timestamp to retrieve the number of new comments. - $query = db_select('comment', 'c'); + $query = $this->connection->select('comment', 'c'); $query->addExpression('COUNT(cid)'); $query->condition('c.entity_type', $entity_type) ->condition('c.entity_id', $entity_id) @@ -301,4 +324,5 @@ public function countNewComments($entity_id, $entity_type, $field_name = NULL, $ return FALSE; } } + } diff --git a/core/modules/forum/forum.services.yml b/core/modules/forum/forum.services.yml index 8ea4dac..ea5cba1 100644 --- a/core/modules/forum/forum.services.yml +++ b/core/modules/forum/forum.services.yml @@ -1,7 +1,7 @@ services: forum_manager: class: Drupal\forum\ForumManager - arguments: ['@config.factory', '@plugin.manager.entity', '@database', '@field.info', '@string_translation'] + arguments: ['@config.factory', '@plugin.manager.entity', '@database', '@field.info', '@string_translation', '@comment.manager'] forum.breadcrumb: class: Drupal\forum\ForumBreadcrumbBuilder arguments: ['@entity.manager', '@config.factory', '@forum_manager'] diff --git a/core/modules/forum/lib/Drupal/forum/ForumManager.php b/core/modules/forum/lib/Drupal/forum/ForumManager.php index 0b42def..6564d36 100644 --- a/core/modules/forum/lib/Drupal/forum/ForumManager.php +++ b/core/modules/forum/lib/Drupal/forum/ForumManager.php @@ -11,6 +11,7 @@ use Drupal\Core\Database\Connection; use Drupal\Core\Entity\EntityManager; use Drupal\Core\StringTranslation\TranslationInterface; +use Drupal\comment\CommentManager; use Drupal\field\FieldInfo; use Drupal\node\NodeInterface; @@ -61,6 +62,13 @@ class ForumManager implements ForumManagerInterface { protected $connection; /** + * The comment manager service. + * + * @var \Drupal\comment\CommentManager + */ + protected $commentManager; + + /** * Array of last post information keyed by forum (term) id. * * @var array @@ -123,12 +131,13 @@ class ForumManager implements ForumManagerInterface { * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager * The translation manager service. */ - public function __construct(ConfigFactory $config_factory, EntityManager $entity_manager, Connection $connection, FieldInfo $field_info, TranslationInterface $translation_manager) { + public function __construct(ConfigFactory $config_factory, EntityManager $entity_manager, Connection $connection, FieldInfo $field_info, TranslationInterface $translation_manager, CommentManager $comment_manager) { $this->configFactory = $config_factory; $this->entityManager = $entity_manager; $this->connection = $connection; $this->fieldInfo = $field_info; $this->translationManager = $translation_manager; + $this->commentManager = $comment_manager; } /** @@ -312,7 +321,7 @@ protected function getTopicOrder($sortby) { * Number of new comments. */ protected function numberNew($nid, $timestamp) { - return \Drupal::service('comment.manager')->countNewComments($nid, 'node', 'comment_forum', $timestamp); + return $this->commentManager->countNewComments($nid, 'node', 'comment_forum', $timestamp); } /**