diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 539cce8..24c4667 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -465,9 +465,9 @@ function comment_node_links_alter(array &$node_links, NodeInterface $node, array $links['comment-add'] += $node->urlInfo()->toArray(); } } - else { + elseif (\Drupal::currentUser()->isAnonymous()) { $links['comment-forbidden'] = array( - 'title' => \Drupal::service('comment.manager')->forbiddenMessage($node, $field_name), + 'title' => \Drupal::service('comment.helper')->forbiddenMessage($node, $field_name), 'html' => TRUE, ); } @@ -500,9 +500,9 @@ function comment_node_links_alter(array &$node_links, NodeInterface $node, array } } } - else { + elseif (\Drupal::currentUser()->isAnonymous()) { $links['comment-forbidden'] = array( - 'title' => \Drupal::service('comment.manager')->forbiddenMessage($node, $field_name), + 'title' => \Drupal::service('comment.helper')->forbiddenMessage($node, $field_name), 'html' => TRUE, ); } diff --git a/core/modules/comment/comment.services.yml b/core/modules/comment/comment.services.yml index 8524045..ad35a2f 100644 --- a/core/modules/comment/comment.services.yml +++ b/core/modules/comment/comment.services.yml @@ -7,7 +7,11 @@ services: comment.manager: class: Drupal\comment\CommentManager - arguments: ['@field.info', '@entity.manager', '@current_user', '@config.factory', '@string_translation', '@url_generator'] + arguments: ['@field.info', '@entity.manager'] + + comment.helper: + class: Drupal\comment\CommentHelper + arguments: ['@entity.manager', '@config.factory', '@string_translation', '@url_generator'] comment.statistics: class: Drupal\comment\CommentStatistics diff --git a/core/modules/comment/lib/Drupal/comment/CommentHelper.php b/core/modules/comment/lib/Drupal/comment/CommentHelper.php new file mode 100644 index 0000000..a7845e5 --- /dev/null +++ b/core/modules/comment/lib/Drupal/comment/CommentHelper.php @@ -0,0 +1,124 @@ +entityManager = $entity_manager; + $this->userConfig = $config_factory->get('user.settings'); + $this->translationManager = $translation_manager; + $this->urlGenerator = $url_generator; + } + + /** + * {@inheritdoc} + */ + public function forbiddenMessage(EntityInterface $entity, $field_name) { + 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->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 ''; + } + + /** + * 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/comment/lib/Drupal/comment/CommentHelperInterface.php b/core/modules/comment/lib/Drupal/comment/CommentHelperInterface.php new file mode 100644 index 0000000..dd2d282 --- /dev/null +++ b/core/modules/comment/lib/Drupal/comment/CommentHelperInterface.php @@ -0,0 +1,34 @@ +fieldInfo = $field_info; $this->entityManager = $entity_manager; - $this->currentUser = $current_user; - $this->userConfig = $config_factory->get('user.settings'); - $this->translationManager = $translation_manager; - $this->urlGenerator = $url_generator; } /** @@ -270,58 +218,7 @@ public function getFieldUIPageTitle($commented_entity_type, $field_name) { $bundles = $field_info->getBundles(); $sample_bundle = reset($bundles); $sample_instance = $this->fieldInfo->getInstance($commented_entity_type, $sample_bundle, $field_name); - return String::checkPlain($sample_instance->label); - } - - /** - * {@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 ($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)), - )); - } - } - } - return ''; - } - - /** - * 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); + return String::checkPlain($sample_instance->getLabel()); } } diff --git a/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php b/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php index 52de708..4b23a19 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php +++ b/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php @@ -6,7 +6,6 @@ */ namespace Drupal\comment; -use Drupal\Core\Entity\EntityInterface; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; @@ -89,21 +88,4 @@ public function addBodyField($entity_type, $field_name); */ public function getFieldUIPageTitle($commented_entity_type, $field_name); - /** - * Provides a message if posting comments is forbidden. - * - * If authenticated users can post comments, a message is returned that - * prompts the anonymous user to log in (or register, if applicable) that - * redirects to entity comment form. Otherwise, no message is returned. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity to which comments are attached to. - * @param string $field_name - * The field name on the entity to which comments are attached to. - * - * @return string - * HTML for a "you can't post comments" notice. - */ - public function forbiddenMessage(EntityInterface $entity, $field_name); - } diff --git a/core/modules/comment/lib/Drupal/comment/CommentViewBuilder.php b/core/modules/comment/lib/Drupal/comment/CommentViewBuilder.php index 3661080..ae88ae9 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentViewBuilder.php +++ b/core/modules/comment/lib/Drupal/comment/CommentViewBuilder.php @@ -208,8 +208,6 @@ protected static function buildLinks(CommentInterface $entity, EntityInterface $ $links = array(); $status = $commented_entity->get($entity->getFieldName())->status; - $container = \Drupal::getContainer(); - if ($status == CommentItemInterface::OPEN) { if ($entity->access('delete')) { $links['comment-delete'] = array( @@ -241,14 +239,14 @@ protected static function buildLinks(CommentInterface $entity, EntityInterface $ 'html' => TRUE, ); } - if (empty($links)) { - $links['comment-forbidden']['title'] = \Drupal::service('comment.manager')->forbiddenMessage($commented_entity, $entity->getFieldName()); + if (empty($links) && \Drupal::currentUser()->isAnonymous()) { + $links['comment-forbidden']['title'] = \Drupal::service('comment.helper')->forbiddenMessage($commented_entity, $entity->getFieldName()); $links['comment-forbidden']['html'] = TRUE; } } // Add translations link for translation-enabled comment bundles. - if ($container->get('module_handler')->moduleExists('content_translation') && content_translation_translate_access($entity)) { + if (\Drupal::moduleHandler()->moduleExists('content_translation') && content_translation_translate_access($entity)) { $links['comment-translations'] = array( 'title' => t('Translate'), 'href' => 'comment/' . $entity->id() . '/translations', diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php index cf7af82..1418021 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php @@ -262,7 +262,7 @@ function assertCommentLinks(array $info) { // Anonymous users should see a note to log in or register in case // authenticated users are allowed to post comments. - // @see \Drupal\comment\CommentManagerInterface::forbiddenMessage() + // @see \Drupal\comment\CommentHelperInterface::forbiddenMessage() if (!$this->loggedInUser) { if (user_access('post comments', $this->web_user)) { // The note depends on whether users are actually able to register.