diff --git a/core/modules/comment/comment.services.yml b/core/modules/comment/comment.services.yml index f6d9d84..e4181bd 100644 --- a/core/modules/comment/comment.services.yml +++ b/core/modules/comment/comment.services.yml @@ -7,7 +7,7 @@ services: comment.manager: class: Drupal\comment\CommentManager - arguments: ['@field.info', '@entity.manager', '@entity.query', '@current_user', '@config.factory', '@string_translation', '@url_generator', '@module_handler'] + arguments: ['@field.info', '@entity.manager', '@entity.query', '@config.factory', '@string_translation', '@url_generator', '@module_handler'] comment.statistics: class: Drupal\comment\CommentStatistics diff --git a/core/modules/comment/lib/Drupal/comment/CommentManager.php b/core/modules/comment/lib/Drupal/comment/CommentManager.php index 2f64cac..5a2efe9 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentManager.php +++ b/core/modules/comment/lib/Drupal/comment/CommentManager.php @@ -16,6 +16,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Routing\UrlGeneratorInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\field\FieldInfo; @@ -23,6 +24,7 @@ * Comment manager contains common functions to manage comment fields. */ class CommentManager implements CommentManagerInterface { + use StringTranslationTrait; /** * The field info service. @@ -46,13 +48,6 @@ class CommentManager implements CommentManagerInterface { protected $queryFactory; /** - * The current user. - * - * @var \Drupal\Core\Session\AccountInterface $current_user - */ - protected $currentUser; - - /** * Whether the DRUPAL_AUTHENTICATED_RID can post comments. * * @var bool @@ -67,13 +62,6 @@ class CommentManager implements CommentManagerInterface { protected $userConfig; /** - * The string translation service. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; - - /** * The url generator service. * * @var \Drupal\Core\Routing\UrlGeneratorInterface @@ -96,24 +84,21 @@ class CommentManager implements CommentManagerInterface { * The entity manager service. * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory * The entity query factory. - * @param \Drupal\Core\Session\AccountInterface $current_user - * The current user. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The string translation service. * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator * The url generator service. - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler service. */ - public function __construct(FieldInfo $field_info, EntityManagerInterface $entity_manager, QueryFactory $query_factory, AccountInterface $current_user, ConfigFactoryInterface $config_factory, TranslationInterface $translation_manager, UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler) { + public function __construct(FieldInfo $field_info, EntityManagerInterface $entity_manager, QueryFactory $query_factory, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler) { $this->fieldInfo = $field_info; $this->entityManager = $entity_manager; $this->queryFactory = $query_factory; - $this->currentUser = $current_user; $this->userConfig = $config_factory->get('user.settings'); - $this->translationManager = $translation_manager; + $this->stringTranslation = $string_translation; $this->urlGenerator = $url_generator; $this->moduleHandler = $module_handler; } @@ -121,16 +106,6 @@ public function __construct(FieldInfo $field_info, EntityManagerInterface $entit /** * {@inheritdoc} */ - public function getParentEntityUri(CommentInterface $comment) { - return $this->entityManager - ->getStorage($comment->getCommentedEntityTypeId()) - ->load($comment->getCommentedEntityId()) - ->urlInfo(); - } - - /** - * {@inheritdoc} - */ public function getFields($entity_type_id) { $entity_type = $this->entityManager->getDefinition($entity_type_id); if (!$entity_type->isSubclassOf('\Drupal\Core\Entity\ContentEntityInterface')) { @@ -299,51 +274,49 @@ public function getFieldUIPageTitle($commented_entity_type, $field_name) { * {@inheritdoc} */ public function forbiddenMessage(EntityInterface $entity, $field_name) { - if ($this->currentUser->isAnonymous()) { - if (!isset($this->authenticatedCanPostComments)) { - // We only output a link if we are certain that users will get the - // permission to post comments by logging in. - $this->authenticatedCanPostComments = $this->entityManager - ->getStorage('user_role') - ->load(DRUPAL_AUTHENTICATED_RID) - ->hasPermission('post comments'); - } + if (!isset($this->authenticatedCanPostComments)) { + // We only output a link if we are certain that users will get the + // permission to post comments by logging in. + $this->authenticatedCanPostComments = $this->entityManager + ->getStorage('user_role') + ->load(DRUPAL_AUTHENTICATED_RID) + ->hasPermission('post comments'); + } - if ($this->authenticatedCanPostComments) { - // We cannot use drupal_get_destination() because these links - // sometimes appear on /node and taxonomy listing pages. - if ($entity->get($field_name)->getFieldDefinition()->getSetting('form_location') == COMMENT_FORM_SEPARATE_PAGE) { - $destination = array('destination' => 'comment/reply/' . $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $field_name . '#comment-form'); - } - else { - $destination = array('destination' => $entity->getSystemPath() . '#comment-form'); - } + if ($this->authenticatedCanPostComments) { + // We cannot use drupal_get_destination() because these links + // sometimes appear on /node and taxonomy listing pages. + if ($entity->get($field_name)->getFieldDefinition()->getSetting('form_location') == COMMENT_FORM_SEPARATE_PAGE) { + $destination = array('destination' => 'comment/reply/' . $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $field_name . '#comment-form'); + } + else { + $destination = array('destination' => $entity->getSystemPath() . '#comment-form'); + } - if ($this->userConfig->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) { - // Users can register themselves. - return $this->t('Log in or register to post comments', array( - '@login' => $this->urlGenerator->generateFromRoute('user.login', array(), array('query' => $destination)), - '@register' => $this->urlGenerator->generateFromRoute('user.register', array(), array('query' => $destination)), - )); - } - else { - // Only admins can add new users, no public registration. - return $this->t('Log in to post comments', array( - '@login' => $this->urlGenerator->generateFromRoute('user.login', array(), array('query' => $destination)), - )); - } + if ($this->userConfig->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) { + // Users can register themselves. + return $this->t('Log in or register to post comments', array( + '@login' => $this->urlGenerator->generateFromRoute('user.login', array(), array('query' => $destination)), + '@register' => $this->urlGenerator->generateFromRoute('user.register', array(), array('query' => $destination)), + )); + } + else { + // Only admins can add new users, no public registration. + return $this->t('Log in to post comments', array( + '@login' => $this->urlGenerator->generateFromRoute('user.login', array(), array('query' => $destination)), + )); } } return ''; } - /** + /* * {@inheritdoc} */ - public function getCountNewComments(EntityInterface $entity, $field_name = NULL, $timestamp = 0) { + public function getCountNewComments(EntityInterface $entity, AccountInterface $account, $field_name = NULL, $timestamp = 0) { // @todo Replace module handler with optional history service injection // after http://drupal.org/node/2081585 - if ($this->currentUser->isAuthenticated() && $this->moduleHandler->moduleExists('history')) { + if ($account->isAuthenticated() && $this->moduleHandler->moduleExists('history')) { // Retrieve the timestamp at which the current user last viewed this entity. if (!$timestamp) { if ($entity->getEntityTypeId() == 'node') { @@ -378,14 +351,4 @@ public function getCountNewComments(EntityInterface $entity, $field_name = NULL, } return FALSE; } - - /** - * Translates a string to the current language or to a given language. - * - * See the t() documentation for details. - */ - protected function t($string, array $args = array(), array $options = array()) { - return $this->translationManager->translate($string, $args, $options); - } - } diff --git a/core/modules/forum/lib/Drupal/forum/ForumManager.php b/core/modules/forum/lib/Drupal/forum/ForumManager.php index cababf8..2cf3d11 100644 --- a/core/modules/forum/lib/Drupal/forum/ForumManager.php +++ b/core/modules/forum/lib/Drupal/forum/ForumManager.php @@ -13,6 +13,7 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\TranslationInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\comment\CommentInterface; use Drupal\comment\CommentManagerInterface; use Drupal\field\FieldInfo; @@ -22,6 +23,7 @@ * Provides forum manager service. */ class ForumManager extends DependencySerialization implements ForumManagerInterface { + use StringTranslationTrait; /** * Forum sort order, newest first. @@ -114,13 +116,6 @@ class ForumManager extends DependencySerialization implements ForumManagerInterf protected $fieldInfo; /** - * Translation manager service. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; - - /** * Constructs the forum manager service. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory @@ -131,17 +126,17 @@ class ForumManager extends DependencySerialization implements ForumManagerInterf * The current database connection. * @param \Drupal\field\FieldInfo $field_info * The field info service. - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The translation manager service. * @param \Drupal\comment\CommentManagerInterface $comment_manager * The comment manager service. */ - public function __construct(ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager, Connection $connection, FieldInfo $field_info, TranslationInterface $translation_manager, CommentManagerInterface $comment_manager) { + public function __construct(ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager, Connection $connection, FieldInfo $field_info, TranslationInterface $string_translation, CommentManagerInterface $comment_manager) { $this->configFactory = $config_factory; $this->entityManager = $entity_manager; $this->connection = $connection; $this->fieldInfo = $field_info; - $this->translationManager = $translation_manager; + $this->stringTranslation = $string_translation; $this->commentManager = $comment_manager; } @@ -547,15 +542,6 @@ public function updateIndex($nid) { } /** - * Translates a string to the current language or to a given language. - * - * See the t() documentation for details. - */ - protected function t($string, array $args = array(), array $options = array()) { - return $this->translationManager->translate($string, $args, $options); - } - - /** * {@inheritdoc} */ public function __sleep() {