diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index 0df2dcc..1f9cbc4 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -35,19 +35,10 @@ function comment_install() { * Implements hook_modules_installed(). */ function comment_modules_installed($modules) { - // Book is being enabled. - if (in_array('book', $modules)) { - // Disable comments in print view mode. - foreach (\Drupal::service('comment.manager')->getFields('node') as $field_name => $detail) { - foreach ($detail['bundles'] as $bundle) { - $display = entity_get_display('node', $bundle, 'print'); - $display->removeComponent($field_name)->save(); - } - } - } - // Comment is being enabled, check if book exists. - if (in_array('comment', $modules) && \Drupal::moduleHandler()->moduleExists('book')) { - // Disable comments in print view mode. + // Book was just installed, or Comment was just installed while Book + // is (already/newly) installed too. + if (in_array('book', $modules) + || (in_array('comment', $modules) && \Drupal::moduleHandler()->moduleExists('book'))) { foreach (\Drupal::service('comment.manager')->getFields('node') as $field_name => $detail) { foreach ($detail['bundles'] as $bundle) { $display = entity_get_display('node', $bundle, 'print'); diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index aa58127..13575d9 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -12,7 +12,6 @@ use Drupal\comment\CommentInterface; use Drupal\comment\Entity\Comment; -use Drupal\comment\CommentManagerInterface; use Drupal\comment\Entity\CommentType; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; @@ -221,8 +220,6 @@ function comment_node_links_alter(array &$node_links, NodeInterface $node, array // Comment links are only added to node entity type for backwards // compatibility. Should you require comment links for other entity types you // can do so by implementing a new field formatter. - // @todo Make this configurable from the formatter see - // http://drupal.org/node/1901110 $links = \Drupal::service('comment.link_builder')->buildCommentedEntityLinks($node, $context); $node_links += $links; @@ -473,17 +470,18 @@ function comment_node_update_index(EntityInterface $node, $langcode) { $build = array(); if ($index_comments) { + $node_base_defs = \Drupal::entityManager()->getFieldStorageDefinitions($node->getEntityTypeId()); foreach (\Drupal::service('comment.manager')->getFields('node') as $field_name => $info) { // Skip fields that entity does not have. if (!$node->hasField($field_name)) { continue; } - $field_definition = $node->getFieldDefinition($field_name); - $mode = $field_definition->getSetting('default_mode'); - $comments_per_page = $field_definition->getSetting('per_page'); if ($node->get($field_name)->status) { + $comment_type = CommentType::load($node_base_defs[$field_name]->getSetting('comment_type')); + $display_settings = entity_get_display($node->getEntityTypeId(), $node->bundle(), 'default') + ->getComponent($field_name); $comments = \Drupal::entityManager()->getStorage('comment') - ->loadThread($node, $field_name, $mode, $comments_per_page); + ->loadThread($node, $field_name, $comment_type->getThreadingMode(), $display_settings['settings']['per_page']); if ($comments) { comment_prepare_thread($comments); $build[] = \Drupal::entityManager()->getViewBuilder('comment')->viewMultiple($comments); @@ -812,7 +810,7 @@ function comment_preprocess_field(&$variables) { $element = $variables['element']; if ($element['#field_type'] == 'comment') { // Provide contextual information. - $variables['comment_display_mode'] = $element[0]['#comment_display_mode']; + $variables['comment_threading_mode'] = $element[0]['#comment_threading_mode']; $variables['comment_type'] = $element[0]['#comment_type']; // Adjust a comment field's attributes. diff --git a/core/modules/comment/comment.services.yml b/core/modules/comment/comment.services.yml index 239cdb2..b67b08f 100644 --- a/core/modules/comment/comment.services.yml +++ b/core/modules/comment/comment.services.yml @@ -20,4 +20,4 @@ services: comment.link_builder: class: Drupal\comment\CommentLinkBuilder - arguments: ['@current_user', '@comment.manager', '@module_handler', '@string_translation'] + arguments: ['@current_user', '@entity.manager', '@comment.manager', '@module_handler', '@string_translation'] diff --git a/core/modules/comment/config/schema/comment.schema.yml b/core/modules/comment/config/schema/comment.schema.yml index b5bcd86..36bea18 100644 --- a/core/modules/comment/config/schema/comment.schema.yml +++ b/core/modules/comment/config/schema/comment.schema.yml @@ -59,6 +59,9 @@ comment.type.*: description: type: text label: 'Description' + threading_mode: + type: integer + label: 'Threading' field.comment.storage_settings: type: sequence @@ -97,18 +100,9 @@ field.comment.field_settings: type: mapping label: 'Comment settings' mapping: - default_mode: - type: integer - label: 'Threading' - per_page: - type: integer - label: 'Comments per page' anonymous: type: integer label: 'Mode' - form_location: - type: boolean - label: ' Allow comment title' preview: type: integer label: 'Preview comment' diff --git a/core/modules/comment/js/node-new-comments-link.js b/core/modules/comment/js/node-new-comments-link.js index 6324fda..9b72498 100644 --- a/core/modules/comment/js/node-new-comments-link.js +++ b/core/modules/comment/js/node-new-comments-link.js @@ -41,11 +41,11 @@ } var perPage = $placeholder.closest('[data-comment-per-page]').attr('data-comment-per-page'); - var defaultMode = $placeholder.closest('[data-comment-default-mode]').attr('data-comment-default-mode'); + var threadingMode = $placeholder.closest('[data-comment-threading-mode]').attr('data-comment-threading-mode'); // Perform an AJAX request to retrieve node read timestamps. Drupal.history.fetchTimestamps(nodeIDs, function () { - processNodeNewCommentLinks($placeholders, perPage, defaultMode); + processNodeNewCommentLinks($placeholders, perPage, threadingMode); }); } }; @@ -74,7 +74,7 @@ .end().show(); } - function processNodeNewCommentLinks($placeholders, perPage, defaultMode) { + function processNodeNewCommentLinks($placeholders, perPage, threadingMode) { // Figure out which placeholders need the "x new comments" links. var $placeholdersToUpdate = {}; var fieldName = 'comment'; @@ -124,7 +124,7 @@ $.ajax({ url: Drupal.url('comments/render_new_comments_node_links'), type: 'POST', - data: { 'node_ids[]': nodeIDs, 'field_name': fieldName, 'per_page': perPage, 'default_mode': defaultMode }, + data: { 'node_ids[]': nodeIDs, 'field_name': fieldName, 'per_page': perPage, 'threading_mode': threadingMode }, dataType: 'json', success: render }); diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 578c87b..7901bda 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -384,11 +384,13 @@ public function save(array $form, FormStateInterface $form_state) { else { drupal_set_message($this->t('Your comment has been posted.')); } - $query = array(); // Find the current display page for this comment. $display_settings = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'default') ->getComponent($field_name); - $page = $this->entityManager->getStorage('comment')->getDisplayOrdinal($comment, $display_settings['settings']['default_mode'], $display_settings['settings']['per_page']); + $field_definition = $this->entityManager->getFieldStorageDefinitions($entity->getEntityTypeId())[$field_name]; + $comment_type = $this->entityManager->getStorage('comment_type')->load($field_definition->getSetting('comment_type')); + $page = $this->entityManager->getStorage('comment')->getDisplayOrdinal($comment, $comment_type->getThreadingMode(), $display_settings['settings']['per_page']); + $query = array(); if ($page > 0) { $query['page'] = $page; } diff --git a/core/modules/comment/src/CommentLinkBuilder.php b/core/modules/comment/src/CommentLinkBuilder.php index f689b1a..22c1340 100644 --- a/core/modules/comment/src/CommentLinkBuilder.php +++ b/core/modules/comment/src/CommentLinkBuilder.php @@ -9,6 +9,7 @@ use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\Core\Entity\FieldableEntityInterface; +use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; @@ -32,6 +33,13 @@ class CommentLinkBuilder implements CommentLinkBuilderInterface { protected $currentUser; /** + * Entity manager. + * + * @var \Drupal\Core\Entity\EntityManagerInterface + */ + protected $entityManager; + + /** * Comment manager service. * * @var \Drupal\comment\CommentManagerInterface @@ -57,8 +65,9 @@ class CommentLinkBuilder implements CommentLinkBuilderInterface { * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * String translation service. */ - public function __construct(AccountInterface $current_user, CommentManagerInterface $comment_manager, ModuleHandlerInterface $module_handler, TranslationInterface $string_translation) { + public function __construct(AccountInterface $current_user, EntityManagerInterface $entity_manager, CommentManagerInterface $comment_manager, ModuleHandlerInterface $module_handler, TranslationInterface $string_translation) { $this->currentUser = $current_user; + $this->entityManager = $entity_manager; $this->commentManager = $comment_manager; $this->moduleHandler = $module_handler; $this->stringTranslation = $string_translation; @@ -88,7 +97,7 @@ public function buildCommentedEntityLinks(FieldableEntityInterface $entity, arra $commenting_status = $entity->get($field_name)->status; if ($commenting_status != CommentItemInterface::HIDDEN) { // Entity has commenting status open or closed. - $field_definition = $entity->getFieldDefinition($field_name); + if ($view_mode == 'rss') { // Add a comments RSS element which is a URL to the comments of this // entity. @@ -101,77 +110,80 @@ public function buildCommentedEntityLinks(FieldableEntityInterface $entity, arra 'value' => $entity->url('canonical', $options), ); } - elseif ($view_mode == 'teaser') { - // Teaser view: display the number of comments that have been posted, - // or a link to add new comments if the user has permission, the - // entity is open to new comments, and there currently are none. - if ($this->currentUser->hasPermission('access comments')) { - if (!empty($entity->get($field_name)->comment_count)) { - $links['comment-comments'] = array( - 'title' => $this->formatPlural($entity->get($field_name)->comment_count, '1 comment', '@count comments'), - 'attributes' => array('title' => $this->t('Jump to the first comment of this posting.')), - 'fragment' => 'comments', - 'url' => $entity->urlInfo(), - ); - if ($this->moduleHandler->moduleExists('history')) { - $links['comment-new-comments'] = array( - 'title' => '', - 'url' => Url::fromRoute(''), - 'attributes' => array( - 'class' => 'hidden', - 'title' => $this->t('Jump to the first new comment of this posting.'), - 'data-history-node-last-comment-timestamp' => $entity->get($field_name)->last_comment_timestamp, - 'data-history-node-field-name' => $field_name, - ), - ); - } - } + else { + $field_definition = $this->entityManager->getFieldStorageDefinitions($entity->getEntityTypeId())[$field_name]; + $comment_type = $this->entityManager->getStorage('comment_type')->load($field_definition->getSetting('comment_type')); + + // We create links to the first new comment using the standard entity + // urlInfo, i.e. the full/default view mode. + $display = $this->entityManager->getStorage('entity_view_display') + ->load($entity->getEntityTypeId() . '.' . $entity->bundle() . '.full'); + if (!$display) { + $display = $this->entityManager->getStorage('entity_view_display') + ->load($entity->getEntityTypeId() . '.' . $entity->bundle() . '.default'); } - // Provide a link to new comment form. - if ($commenting_status == CommentItemInterface::OPEN) { - $comment_form_location = $field_definition->getSetting('form_location'); - if ($this->currentUser->hasPermission('post comments')) { - $links['comment-add'] = array( - 'title' => $this->t('Add new comment'), - 'language' => $entity->language(), - 'attributes' => array('title' => $this->t('Add a new comment to this page.')), - 'fragment' => 'comment-form', - ); - if ($comment_form_location == CommentItemInterface::FORM_SEPARATE_PAGE) { - $links['comment-add']['url'] = Url::fromRoute('comment.reply', [ - 'entity_type' => $entity->getEntityTypeId(), - 'entity' => $entity->id(), - 'field_name' => $field_name, - ]); - } - else { - $links['comment-add'] += ['url' => $entity->urlInfo()]; - } - } - elseif ($this->currentUser->isAnonymous()) { - $links['comment-forbidden'] = array( - 'title' => $this->commentManager->forbiddenMessage($entity, $field_name), - 'html' => TRUE, + $formatter_options_full = $display + ? $display->getComponent($field_name): NULL; + + // Add the number of comments that have been posted and a link to the + // first new comment, if they are displayed on 'full' mode and the + // user has permission. Historically this is done for teaser only. + if ($view_mode == 'teaser' && $formatter_options_full + && !empty($entity->get($field_name)->comment_count) + && $this->currentUser->hasPermission('access comments')) { + // Comments can theoretically be displayed inside a teaser, but even + // then we still choose to link to the 'full page' comments. + $links['comment-comments'] = array( + 'title' => $this->formatPlural($entity->get($field_name)->comment_count, '1 comment', '@count comments'), + 'attributes' => array('title' => $this->t('Jump to the first comment of this posting.')), + 'fragment' => 'comments', + 'url' => $entity->urlInfo(), + ); + if ($this->moduleHandler->moduleExists('history')) { + $links['comment-new-comments'] = array( + 'title' => '', + 'url' => Url::fromRoute(''), + 'attributes' => array( + 'class' => 'hidden', + 'title' => $this->t('Jump to the first new comment of this posting.'), + 'data-history-node-last-comment-timestamp' => $entity->get($field_name)->last_comment_timestamp, + 'data-history-node-field-name' => $field_name, + 'data-comment-threading-mode' => $comment_type->getThreadingMode(), + 'data-comment-per-page' => $formatter_options_full['settings']['per_page'], + ), ); } } - } - else { - // Entity in other view modes: add a "post comment" link if the user - // is allowed to post comments and if this entity is allowing new - // comments. + + // Add a "add new comment" link if this entity is allowing new + // comments and the user has permission. if ($commenting_status == CommentItemInterface::OPEN) { - $comment_form_location = $field_definition->getSetting('form_location'); if ($this->currentUser->hasPermission('post comments')) { + + // We decide the link to the comment-add form using our own view + // mode. + $display = $this->entityManager->getStorage('entity_view_display') + ->load($entity->getEntityTypeId() . '.' . $entity->bundle() . '.' . $view_mode); + if (!$display) { + $display = $this->entityManager->getStorage('entity_view_display') + ->load($entity->getEntityTypeId() . '.' . $entity->bundle() . '.default'); + } + $formatter_options = $display + ? $display->getComponent($field_name): NULL; + // Show the "post comment" link if the form is on another page, or // if there are existing comments that the link will skip past. - if ($comment_form_location == CommentItemInterface::FORM_SEPARATE_PAGE || (!empty($entity->get($field_name)->comment_count) && $this->currentUser->hasPermission('access comments'))) { + if (!$formatter_options + || $formatter_options['settings']['form_location'] == CommentItemInterface::FORM_SEPARATE_PAGE + || (!empty($entity->get($field_name)->comment_count) + && $this->currentUser->hasPermission('access comments'))) { $links['comment-add'] = array( 'title' => $this->t('Add new comment'), 'attributes' => array('title' => $this->t('Share your thoughts and opinions related to this posting.')), 'fragment' => 'comment-form', ); - if ($comment_form_location == CommentItemInterface::FORM_SEPARATE_PAGE) { + if (!$formatter_options + || $formatter_options['settings']['form_location'] == CommentItemInterface::FORM_SEPARATE_PAGE) { $links['comment-add']['url'] = Url::fromRoute('comment.reply', [ 'entity_type' => $entity->getEntityTypeId(), 'entity' => $entity->id(), @@ -190,30 +202,35 @@ public function buildCommentedEntityLinks(FieldableEntityInterface $entity, arra ); } } - } - } - if (!empty($links)) { - $entity_links['comment__' . $field_name] = array( - '#theme' => 'links__entity__comment__' . $field_name, - '#links' => $links, - '#attributes' => array('class' => array('links', 'inline')), - ); - if ($view_mode == 'teaser' && $this->moduleHandler->moduleExists('history') && $this->currentUser->isAuthenticated()) { - $entity_links['comment__' . $field_name]['#attached']['library'][] = 'comment/drupal.node-new-comments-link'; + if (!empty($links)) { + $entity_links['comment__' . $field_name] = array( + '#theme' => 'links__entity__comment__' . $field_name, + '#links' => $links, + '#attributes' => array('class' => array('links', 'inline')), + ); + if ($view_mode == 'teaser' + && $this->moduleHandler->moduleExists('history') + && $this->currentUser->isAuthenticated() + && $formatter_options_full) { + $entity_links['comment__' . $field_name]['#attached']['library'][] = 'comment/drupal.node-new-comments-link'; - // Embed the metadata for the "X new comments" link (if any) on this - // entity. - $entity_links['comment__' . $field_name]['#post_render_cache']['history_attach_timestamp'] = array( - array('node_id' => $entity->id()), - ); - $entity_links['comment__' . $field_name]['#post_render_cache']['Drupal\comment\CommentViewBuilder::attachNewCommentsLinkMetadata'] = array( - array( - 'entity_type' => $entity->getEntityTypeId(), - 'entity_id' => $entity->id(), - 'field_name' => $field_name, - ), - ); + // Embed the metadata for the "X new comments" link (if any) on + // this entity. + $entity_links['comment__' . $field_name]['#post_render_cache']['history_attach_timestamp'] = array( + array('node_id' => $entity->id()), + ); + $entity_links['comment__' . $field_name]['#post_render_cache']['Drupal\comment\CommentViewBuilder::attachNewCommentsLinkMetadata'] = array( + array( + 'entity_type' => $entity->getEntityTypeId(), + 'entity_id' => $entity->id(), + 'field_name' => $field_name, + 'threading_mode' => $comment_type->getThreadingMode(), + 'per_page' => $formatter_options_full['settings']['per_page'], + ), + ); + } + } } } } diff --git a/core/modules/comment/src/CommentManager.php b/core/modules/comment/src/CommentManager.php index 3847218..c29f894 100644 --- a/core/modules/comment/src/CommentManager.php +++ b/core/modules/comment/src/CommentManager.php @@ -131,6 +131,8 @@ public function addDefaultField($entity_type, $bundle, $field_name = 'comment', '%entity_type' => $entity_type, ))); } + $comment_type->setThreadingMode(CommentTypeInterface::THREADING_MODE_THREADED); + $comment_type->save(); } else { // Silently create the comment-type for the calling code. @@ -139,6 +141,7 @@ public function addDefaultField($entity_type, $bundle, $field_name = 'comment', 'label' => Unicode::ucfirst($comment_type_id), 'target_entity_type_id' => $entity_type, 'description' => 'Default comment field', + 'threading_mode' => CommentTypeInterface::THREADING_MODE_THREADED, ))->save(); } // Make sure the field doesn't already exist. @@ -201,9 +204,7 @@ public function addDefaultField($entity_type, $bundle, $field_name = 'comment', 'weight' => 20, 'settings' => array( 'per_page' => 50, - 'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED, 'form_location' => CommentItemInterface::FORM_BELOW, - 'show_links' => CommentDefaultFormatter::LINKS_PAGE, ) )) ->save(); diff --git a/core/modules/comment/src/CommentManagerInterface.php b/core/modules/comment/src/CommentManagerInterface.php index 0f00351..8e9f782 100644 --- a/core/modules/comment/src/CommentManagerInterface.php +++ b/core/modules/comment/src/CommentManagerInterface.php @@ -17,16 +17,6 @@ interface CommentManagerInterface { /** - * Comments are displayed in a flat list - expanded. - */ - const COMMENT_MODE_FLAT = 0; - - /** - * Comments are displayed as a threaded list - expanded. - */ - const COMMENT_MODE_THREADED = 1; - - /** * Utility function to return an array of comment fields. * * @param string $entity_type_id diff --git a/core/modules/comment/src/CommentStorage.php b/core/modules/comment/src/CommentStorage.php index fa0422e..5b93225 100644 --- a/core/modules/comment/src/CommentStorage.php +++ b/core/modules/comment/src/CommentStorage.php @@ -107,7 +107,7 @@ public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $div $query->condition('c1.status', CommentInterface::PUBLISHED); } - if ($comment_mode == CommentManagerInterface::COMMENT_MODE_FLAT) { + if ($comment_mode == CommentTypeInterface::THREADING_MODE_FLAT) { // For rendering flat comments, cid is used for ordering comments due to // unpredictable behavior with timestamp, so we make the same assumption // here. @@ -131,13 +131,13 @@ public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $div /** * {@inheritdoc} */ - public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $mode = CommentManagerInterface::COMMENT_MODE_THREADED, $comments_per_page = 50, $field_name = 'comment') { + public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $mode = CommentTypeInterface::THREADING_MODE_THREADED, $comments_per_page = 50, $field_name = 'comment') { if ($total_comments <= $comments_per_page) { // Only one page of comments. $count = 0; } - elseif ($mode == CommentManagerInterface::COMMENT_MODE_FLAT) { + elseif ($mode == CommentTypeInterface::THREADING_MODE_FLAT) { // Flat comments. $count = $total_comments - $new_comments; } @@ -294,7 +294,7 @@ public function loadThread(EntityInterface $entity, $field_name, $mode, $comment $query->condition('c.status', CommentInterface::PUBLISHED); $count_query->condition('c.status', CommentInterface::PUBLISHED); } - if ($mode == CommentManagerInterface::COMMENT_MODE_FLAT) { + if ($mode == CommentTypeInterface::THREADING_MODE_FLAT) { $query->orderBy('c.cid', 'ASC'); } else { diff --git a/core/modules/comment/src/CommentStorageInterface.php b/core/modules/comment/src/CommentStorageInterface.php index a32c475..aa0dfe0 100644 --- a/core/modules/comment/src/CommentStorageInterface.php +++ b/core/modules/comment/src/CommentStorageInterface.php @@ -49,8 +49,8 @@ public function getMaxThreadPerThread(CommentInterface $comment); * @param \Drupal\Core\Entity\FieldableEntityInterface $entity * The entity to which the comments belong. * @param int $mode - * (optional) The display mode: CommentManagerInterface::COMMENT_MODE_FLAT - * or CommentManagerInterface::COMMENT_MODE_THREADED (the default). + * (optional) The threading mode: CommentTypeInterface::THREADING_MODE_FLAT + * or CommentTypeInterface::THREADING_MODE_THREADED (the default). * @param int $comments_per_page * (optional) The amount of comments to display per page. Defaults to 50. * @param string $field_name @@ -59,7 +59,7 @@ public function getMaxThreadPerThread(CommentInterface $comment); * @return array|null * The page number where first new comment appears. (First page returns 0.) */ - public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $mode = CommentManagerInterface::COMMENT_MODE_THREADED, $comments_per_page = 50, $field_name = 'comment'); + public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $mode = CommentTypeInterface::THREADING_MODE_THREADED, $comments_per_page = 50, $field_name = 'comment'); /** * Gets the display ordinal or page number for a comment. @@ -67,8 +67,8 @@ public function getNewCommentPageNumber($total_comments, $new_comments, Fieldabl * @param \Drupal\comment\CommentInterface $comment * The comment to use as a reference point. * @param int $comment_mode - * The comment display mode: CommentManagerInterface::COMMENT_MODE_FLAT or - * CommentManagerInterface::COMMENT_MODE_THREADED. + * The threading mode: CommentTypeInterface::THREADING_MODE_FLAT or + * CommentTypeInterface::THREADING_MODE_THREADED. * @param int $divisor * Defaults to 1, which returns the display ordinal for a comment. If the * number of comments per page is provided, the returned value will be the @@ -98,8 +98,8 @@ public function getChildCids(array $comments); * @param string $field_name * The field_name whose comment(s) needs rendering. * @param int $mode - * The comment display mode: CommentManagerInterface::COMMENT_MODE_FLAT or - * CommentManagerInterface::COMMENT_MODE_THREADED. + * The threading mode: CommentTypeInterface::THREADING_MODE_FLAT or + * CommentTypeInterface::THREADING_MODE_THREADED. * @param int $comments_per_page * (optional) The amount of comments to display per page. * Defaults to 0, which means show all comments. diff --git a/core/modules/comment/src/CommentTypeForm.php b/core/modules/comment/src/CommentTypeForm.php index 8e73652..b06b888 100644 --- a/core/modules/comment/src/CommentTypeForm.php +++ b/core/modules/comment/src/CommentTypeForm.php @@ -63,6 +63,7 @@ public function __construct(EntityManagerInterface $entity_manager, LoggerInterf public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); + /** @var $comment_type CommentTypeInterface */ $comment_type = $this->entity; $form['label'] = array( @@ -114,6 +115,13 @@ public function form(array $form, FormStateInterface $form_state) { ); } + $form['threading_mode'] = array( + '#type' => 'checkbox', + '#title' => t('Threading'), + '#default_value' => $comment_type->getThreadingMode(), + '#description' => t('Show comment replies in a threaded list.'), + ); + if ($this->moduleHandler->moduleExists('content_translation')) { $form['language'] = array( '#type' => 'details', diff --git a/core/modules/comment/src/CommentTypeInterface.php b/core/modules/comment/src/CommentTypeInterface.php index 2ccc0c1..b7015a7 100644 --- a/core/modules/comment/src/CommentTypeInterface.php +++ b/core/modules/comment/src/CommentTypeInterface.php @@ -15,6 +15,16 @@ interface CommentTypeInterface extends ConfigEntityInterface { /** + * Comments are displayed as a flat list. + */ + const THREADING_MODE_FLAT = 0; + + /** + * Comments are displayed as a threaded list. + */ + const THREADING_MODE_THREADED = 1; + + /** * Returns the comment type description. * * @return string @@ -40,4 +50,23 @@ public function setDescription($description); */ public function getTargetEntityTypeId(); + /** + * Gets the threading mode for this comment type. + * + * @return int + * The comment threading mode (CommentTypeInterface::THREADING_MODE_FLAT or + * CommentTypeInterface::THREADING_MODE_THREADED) + */ + public function getThreadingMode(); + + /** + * Sets the threading mode for this comment type. + * + * @param int $mode + * The threading mode. + * + * @return $this + */ + public function setThreadingMode($mode); + } diff --git a/core/modules/comment/src/CommentViewBuilder.php b/core/modules/comment/src/CommentViewBuilder.php index 1118b85..fe879ee 100644 --- a/core/modules/comment/src/CommentViewBuilder.php +++ b/core/modules/comment/src/CommentViewBuilder.php @@ -15,7 +15,6 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityViewBuilder; -use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -74,9 +73,13 @@ public function __construct(EntityTypeInterface $entity_type, EntityManagerInter protected function getBuildDefaults(EntityInterface $entity, $view_mode, $langcode) { $build = parent::getBuildDefaults($entity, $view_mode, $langcode); + // Threading mode setting is stored in comment_type config; the name of the + // specific comment type is stored in our field definition. + $field_definition = $this->entityManager->getFieldStorageDefinitions($entity->getCommentedEntity()->getEntityTypeId())[$entity->getFieldName()]; + $comment_type = $this->entityManager->getStorage('comment_type')->load($field_definition->getSetting('comment_type')); // If threading is enabled, don't render cache individual comments, but do // keep the cache tags, so they can bubble up. - if ($entity->getCommentedEntity()->getFieldDefinition($entity->getFieldName())->getSetting('default_mode') === CommentManagerInterface::COMMENT_MODE_THREADED) { + if ($comment_type->getThreadingMode() === CommentTypeInterface::THREADING_MODE_THREADED) { $cache_tags = $build['#cache']['tags']; $build['#cache'] = []; $build['#cache']['tags'] = $cache_tags; @@ -309,9 +312,10 @@ protected function alterBuild(array &$build, EntityInterface $comment, EntityVie if (empty($comment->in_preview)) { $prefix = ''; $commented_entity = $comment->getCommentedEntity(); - $field_definition = $this->entityManager->getFieldDefinitions($commented_entity->getEntityTypeId(), $commented_entity->bundle())[$comment->getFieldName()]; + $field_definition = $this->entityManager->getFieldStorageDefinitions($commented_entity->getEntityTypeId())[$comment->getFieldName()]; + $comment_type = $this->entityManager->getStorage('comment_type')->load($field_definition->getSetting('comment_type')); $is_threaded = isset($comment->divs) - && $field_definition->getSetting('default_mode') == CommentManagerInterface::COMMENT_MODE_THREADED; + && $comment_type->getThreadingMode() == CommentTypeInterface::THREADING_MODE_THREADED; // Add indentation div or close open divs as needed. if ($is_threaded) { @@ -360,7 +364,7 @@ public static function attachNewCommentsLinkMetadata(array $element, array $cont $field_name = $context['field_name']; $page_number = \Drupal::entityManager() ->getStorage('comment') - ->getNewCommentPageNumber($entity->{$field_name}->comment_count, $new, $entity, $context['default_mode'], $context['per_page']); + ->getNewCommentPageNumber($entity->{$field_name}->comment_count, $new, $entity, $context['threading_mode'], $context['per_page']); $query = $page_number ? array('page' => $page_number) : NULL; // Attach metadata. diff --git a/core/modules/comment/src/Controller/CommentController.php b/core/modules/comment/src/Controller/CommentController.php index 2ce45eb..d2e0023 100644 --- a/core/modules/comment/src/Controller/CommentController.php +++ b/core/modules/comment/src/Controller/CommentController.php @@ -9,6 +9,7 @@ use Drupal\comment\CommentInterface; use Drupal\comment\CommentManagerInterface; +use Drupal\comment\CommentTypeInterface; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Entity\EntityInterface; @@ -112,10 +113,12 @@ public function commentPermalink(Request $request, CommentInterface $comment) { if (!$entity->access('view')) { throw new AccessDeniedHttpException(); } + $field_definition = $this->entityManager()->getFieldStorageDefinitions($entity->getEntityTypeId())[$comment->getFieldName()]; + $comment_type = $this->entityManager()->getStorage('comment_type')->load($field_definition->getSetting('comment_type')); $display_settings = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'default') ->getComponent($comment->getFieldName()); // Find the current display page for this comment. - $page = $this->entityManager->getStorage('comment')->getDisplayOrdinal($comment, $display_settings['settings']['default_mode'], $display_settings['settings']['per_page']); + $page = $this->entityManager()->getStorage('comment')->getDisplayOrdinal($comment, $comment_type->getThreadingMode(), $display_settings['settings']['per_page']); // @todo: Cleaner sub request handling. $redirect_request = Request::create($entity->getSystemPath(), 'GET', $request->query->all(), $request->cookies->all(), array(), $request->server->all()); $redirect_request->query->set('page', $page); @@ -279,8 +282,8 @@ public function renderNewCommentsNodeLinks(Request $request) { $nids = $request->request->get('node_ids'); $field_name = $request->request->get('field_name'); $per_page = $request->request->get('per_page'); - $default_mode = $request->request->get('default_mode'); - if (!isset($nids) || !in_array($default_mode, array(CommentManagerInterface::COMMENT_MODE_THREADED, CommentManagerInterface::COMMENT_MODE_FLAT))) { + $threading_mode = $request->request->get('threading_mode'); + if (!isset($nids) || !in_array($threading_mode, array(CommentTypeInterface::THREADING_MODE_THREADED, CommentTypeInterface::THREADING_MODE_FLAT))) { throw new NotFoundHttpException(); } // Only handle up to 100 nodes. @@ -291,7 +294,7 @@ public function renderNewCommentsNodeLinks(Request $request) { foreach ($nodes as $nid => $node) { $new = $this->commentManager->getCountNewComments($node); $page_number = $this->entityManager()->getStorage('comment') - ->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node, $default_mode, $per_page); + ->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node, $threading_mode, $per_page); $query = $page_number ? array('page' => $page_number) : NULL; $links[$nid] = array( 'new_comment_count' => (int) $new, diff --git a/core/modules/comment/src/Entity/CommentType.php b/core/modules/comment/src/Entity/CommentType.php index 3a1b3a8..d32ed36 100644 --- a/core/modules/comment/src/Entity/CommentType.php +++ b/core/modules/comment/src/Entity/CommentType.php @@ -71,6 +71,13 @@ class CommentType extends ConfigEntityBundleBase implements CommentTypeInterface protected $target_entity_type_id; /** + * The comment threading mode (flat or threaded). + * + * @var int + */ + protected $threading_mode; + + /** * {@inheritdoc} */ public function getDescription() { @@ -95,6 +102,22 @@ public function getTargetEntityTypeId() { /** * {@inheritdoc} */ + public function getThreadingMode() { + return isset($this->threading_mode) ? $this->threading_mode : + CommentTypeInterface::THREADING_MODE_THREADED; + } + + /** + * {@inheritdoc} + */ + public function setThreadingMode($mode) { + $this->threading_mode = $mode; + return $this; + } + + /** + * {@inheritdoc} + */ public function postSave(EntityStorageInterface $storage, $update = TRUE) { parent::postSave($storage, $update); if (!$update && !$this->isSyncing()) { diff --git a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php index 3b19695..7364a7f 100644 --- a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php +++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php @@ -7,14 +7,9 @@ namespace Drupal\comment\Plugin\Field\FieldFormatter; -use Drupal\comment\CommentManagerInterface; -use Drupal\comment\CommentStorageInterface; -use Drupal\comment\Entity\Comment; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\Core\Entity\EntityManagerInterface; -use Drupal\Core\Entity\EntityViewBuilderInterface; use Drupal\Core\Entity\EntityFormBuilderInterface; -use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FormatterBase; @@ -41,33 +36,11 @@ class CommentDefaultFormatter extends FormatterBase implements ContainerFactoryPluginInterface { /** - * Display no links. - */ - const LINKS_NONE = 0; - - /** - * Display default 'add new comment' and 'x comments' links seen on page view. - */ - const LINKS_PAGE = 1; - - /** - * Display RSS style links. - */ - const LINKS_RSS = 2; - - /** - * Display teaser style links. - */ - const LINKS_TEASER = 3; - - /** * {@inheritdoc} */ public static function defaultSettings() { return array( 'pager_id' => 0, - 'show_links' => static::LINKS_NONE, - 'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED, 'per_page' => 50, 'form_location' => CommentItemInterface::FORM_BELOW, ) + parent::defaultSettings(); @@ -109,20 +82,6 @@ public static function defaultSettings() { protected $entityFormBuilder; /** - * The module handler service. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - protected $moduleHandler; - - /** - * The comment manager service. - * - * @var \Drupal\comment\CommentManagerInterface - */ - protected $commentManager; - - /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { @@ -165,20 +124,14 @@ public static function create(ContainerInterface $container, array $configuratio * The entity manager * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder * The entity form builder. - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler service. - * @param \Drupal\comment\CommentManagerInterface $comment_manager - * The comment manager service. */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityManagerInterface $entity_manager, EntityFormBuilderInterface $entity_form_builder, ModuleHandlerInterface $module_handler, CommentManagerInterface $comment_manager) { + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityManagerInterface $entity_manager, EntityFormBuilderInterface $entity_form_builder) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); $this->viewBuilder = $entity_manager->getViewBuilder('comment'); $this->storage = $entity_manager->getStorage('comment'); $this->currentUser = $current_user; $this->entityManager = $entity_manager; $this->entityFormBuilder = $entity_form_builder; - $this->moduleHandler = $module_handler; - $this->commentManager = $comment_manager; } /** @@ -199,6 +152,8 @@ public function viewElements(FieldItemListInterface $items) { // return anything if the view mode is search_index or search_result. !in_array($this->viewMode, array('search_result', 'search_index'))) { + $comment_type = $this->entityManager->getStorage('comment_type')->load($this->getFieldSetting('comment_type')); + // Only attempt to render comments if the entity has visible comments. // Unpublished comments are not included in // $entity->get($field_name)->comment_count, but unpublished comments @@ -209,7 +164,7 @@ public function viewElements(FieldItemListInterface $items) { $output['comments']['#cache']['tags'] = $this->entityManager->getDefinition('comment')->getListCacheTags(); if ($entity->get($field_name)->comment_count || $this->currentUser->hasPermission('administer comments')) { - $mode = $this->getSetting('default_mode'); + $mode = $comment_type->getThreadingMode(); $comments_per_page = $this->getSetting('per_page'); $comments = $this->storage->loadThread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id')); if ($comments) { @@ -265,10 +220,9 @@ public function viewElements(FieldItemListInterface $items) { $elements[] = $output + array( '#comment_type' => $this->getFieldSetting('comment_type'), - '#comment_display_mode' => $this->getSetting('default_mode'), + '#comment_threading_mode' => $comment_type->getThreadingMode(), 'comments' => array(), 'comment_form' => array(), - 'links' => $this->buildLinks($items), ); } @@ -280,24 +234,6 @@ public function viewElements(FieldItemListInterface $items) { */ public function settingsForm(array $form, FormStateInterface $form_state) { $element = array(); - $element['default_mode'] = array( - '#type' => 'checkbox', - '#title' => t('Threading'), - '#default_value' => $this->getSetting('default_mode'), - '#description' => t('Show comment replies in a threaded list.'), - ); - $element['show_links'] = array( - '#type' => 'select', - '#title' => t('Show links'), - '#default_value' => $this->getSetting('show_links'), - '#description' => t('Show comment links.'), - '#options' => array( - static::LINKS_NONE => $this->t('None'), - static::LINKS_PAGE => $this->t('Default links'), - static::LINKS_RSS => $this->t('RSS links'), - static::LINKS_TEASER => $this->t('Teaser links'), - ), - ); $element['per_page'] = array( '#type' => 'number', '#title' => t('Comments per page'), @@ -326,180 +262,17 @@ public function settingsForm(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function settingsSummary() { - $links_map = array( - static::LINKS_NONE => $this->t('without links'), - static::LINKS_RSS => $this->t('RSS style links'), - static::LINKS_PAGE => $this->t('with full-page style links'), - static::LINKS_TEASER => $this->t('with teaser style links'), - ); $variables = array( '@id' => $this->getSetting('pager_id'), '@form' => $this->getSetting('form_location') ? $this->t('inline form') : $this->t('form on separate page'), '@per_page' => $this->getSetting('per_page'), - '@mode' => $this->getSetting('default_mode') ? $this->t('threaded') : $this->t('flat'), - '@links' => $links_map[$this->getSetting('show_links')], ); if ($this->getSetting('pager_id')) { // Only include pager details in summary if we're using a non-standard // pager id. - return array($this->t('Showing @per_page @mode comments with @form, using pager ID @id and @links', $variables)); - } - return array($this->t('Showing @per_page @mode comments with @form and @links', $variables)); - } - - /** - * Builds links for commented entity. - */ - protected function buildLinks(FieldItemListInterface $items) { - $links = array(); - if ($this->getSetting('show_links') == static::LINKS_NONE) { - return $links; - } - - $field_name = $this->fieldDefinition->getName(); - $entity = $items->getEntity(); - $commenting_status = $items->status; - if ($commenting_status) { - $field_definition = $this->fieldDefinition; - $link_style = $this->getSetting('show_links'); - // Node has commenting open or closed. - if ($link_style == static::LINKS_RSS) { - // Add a comments RSS element which is a URL to the comments of this - // node. - $options = array( - 'fragment' => 'comments', - 'absolute' => TRUE, - ); - $entity->rss_elements[] = array( - 'key' => 'comments', - 'value' => $entity->url('canonical', $options), - ); - } - elseif ($link_style == static::LINKS_PAGE) { - // Default links: display the number of comments that have been posted, - // or a link to add new comments if the user has permission, the node - // is open to new comments, and there currently are none. - if ($this->currentUser->hasPermission('access comments')) { - if (!empty($items->comment_count)) { - $links['comment-comments'] = array( - 'title' => $this->formatPlural($items->comment_count, '1 comment', '@count comments'), - 'attributes' => array('title' => $this->t('Jump to the first comment of this posting.')), - 'fragment' => 'comments', - 'html' => TRUE, - ) + $entity->urlInfo()->toArray(); - if ($this->moduleHandler->moduleExists('history')) { - $links['comment-new-comments'] = array( - 'title' => '', - 'href' => '', - 'attributes' => array( - 'class' => 'hidden', - 'title' => t('Jump to the first new comment of this posting.'), - 'data-history-node-last-comment-timestamp' => $items->last_comment_timestamp, - 'data-history-node-field-name' => $field_name, - 'data-comment-per-page' => $this->getSetting('per_page'), - 'data-comment-default-mode' => $this->getSetting('default_mode'), - ), - 'html' => TRUE, - ); - } - } - } - // Provide a link to new comment form. - if ($commenting_status == CommentItemInterface::OPEN) { - $comment_form_location = $this->getSetting('form_location'); - if ($this->currentUser->hasPermission('post comments')) { - $links['comment-add'] = array( - 'title' => t('Add new comment'), - 'language' => $entity->language(), - 'attributes' => array('title' => t('Add a new comment to this page.')), - 'fragment' => 'comment-form', - ); - if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE) { - $links['comment-add']['route_name'] = 'comment.reply'; - $links['comment-add']['route_parameters'] = array( - 'entity_type' => $entity->getEntityTypeId(), - 'entity_id' => $entity->id(), - 'field_name' => $field_name, - ); - } - else { - $links['comment-add'] += $entity->urlInfo()->toArray(); - } - } - elseif ($this->currentUser->isAnonymous()) { - $links['comment-forbidden'] = array( - 'title' => $this->commentManager->forbiddenMessage($entity, $field_name), - 'html' => TRUE, - ); - } - } - } - else { - // Teaser style links - Node in other view modes: add a "post comment" - // link if the user is allowed to post comments and if this node is - // allowing new comments. - if ($commenting_status == CommentItemInterface::OPEN) { - $comment_form_location = $this->getSetting('form_location'); - if ($this->currentUser->hasPermission('post comments')) { - // Show the "post comment" link if the form is on another page, or - // if there are existing comments that the link will skip past. - if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE || (!empty($items->comment_count) && $this->currentUser->hasPermission('access comments'))) { - $links['comment-add'] = array( - 'title' => t('Add new comment'), - 'attributes' => array('title' => t('Share your thoughts and opinions related to this posting.')), - 'fragment' => 'comment-form', - ); - if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE) { - $links['comment-add']['route_name'] = 'comment.reply'; - $links['comment-add']['route_parameters'] = array( - 'entity_type' => $entity->getEntityTypeId(), - 'entity_id' => $entity->id(), - 'field_name' => $field_name, - ); - } - else { - $links['comment-add'] += $entity->urlInfo()->toArray(); - } - } - } - elseif ($this->currentUser->isAnonymous()) { - $links['comment-forbidden'] = array( - 'title' => $this->commentManager->forbiddenMessage($entity, $field_name), - 'html' => TRUE, - ); - } - } - } - - if (!empty($links)) { - $build['comment__' . $field_name] = array( - '#theme' => 'links__entity__comment__' . $field_name, - '#links' => $links, - '#attributes' => array('class' => array('links', 'inline')), - ); - if ($link_style == static::LINKS_TEASER && $this->moduleHandler->moduleExists('history') && $this->currentUser->isAuthenticated()) { - if ($entity->getEntityTypeId() == 'node') { - $build['comment__' . $field_name]['#attached']['library'][] = 'comment/drupal.node-new-comments-link'; - } - - // Embed the metadata for the "X new comments" link (if any) on this - // node. - $build['comment__' . $field_name]['#post_render_cache']['history_attach_timestamp'] = array( - array($entity->getEntityTypeId() . '_id' => $entity->id()), - ); - $build['comment__' . $field_name]['#post_render_cache']['Drupal\comment\CommentViewBuilder::attachNewCommentsLinkMetadata'] = array( - array( - 'entity_type' => $entity->getEntityTypeId(), - 'entity_id' => $entity->id(), - 'field_name' => $field_name, - 'per_page' => $this->getSetting('per_page'), - 'default_mode' => $this->getSetting('default_mode'), - ), - ); - } - return $build; - } + return array($this->t('Showing @per_page comments with @form, using pager ID @id', $variables)); } + return array($this->t('Showing @per_page comments with @form', $variables)); } } diff --git a/core/modules/comment/src/Plugin/views/field/NodeNewComments.php b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php index 1092d58..98cd059 100644 --- a/core/modules/comment/src/Plugin/views/field/NodeNewComments.php +++ b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php @@ -90,10 +90,10 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { '#type' => 'checkbox', '#default_value' => $this->options['link_to_comment'], ); - $form['default_mode'] = array( + $form['threading_mode'] = array( '#type' => 'checkbox', '#title' => t('Threading'), - '#default_value' => $this->options['default_mode'], + '#default_value' => $this->options['threading_mode'], '#description' => t('Comment replies use a threaded list.'), ); $element['per_page'] = array( @@ -168,7 +168,7 @@ protected function renderLink($data, ResultRow $values) { 'type' => $this->getValue($values, 'type'), )); $page_number = \Drupal::entityManager()->getStorage('comment') - ->getNewCommentPageNumber($this->getValue($values, 'comment_count'), $this->getValue($values), $node, $this->options['default_mode'], $this->options['per_page']); + ->getNewCommentPageNumber($this->getValue($values, 'comment_count'), $this->getValue($values), $node, $this->options['threading_mode'], $this->options['per_page']); $this->options['alter']['make_link'] = TRUE; $this->options['alter']['path'] = 'node/' . $node->id(); $this->options['alter']['query'] = $page_number ? array('page' => $page_number) : NULL; diff --git a/core/modules/comment/src/Tests/CommentCacheTagsTest.php b/core/modules/comment/src/Tests/CommentCacheTagsTest.php index 5da5555..ce4ce59 100644 --- a/core/modules/comment/src/Tests/CommentCacheTagsTest.php +++ b/core/modules/comment/src/Tests/CommentCacheTagsTest.php @@ -7,9 +7,9 @@ namespace Drupal\comment\Tests; -use Drupal\comment\CommentManagerInterface; +use Drupal\comment\CommentTypeInterface; +use Drupal\comment\Entity\CommentType; use Drupal\Core\Entity\EntityInterface; -use Drupal\field\Entity\FieldConfig; use Drupal\system\Tests\Entity\EntityWithUriCacheTagsTestBase; use Drupal\user\Entity\Role; @@ -50,9 +50,9 @@ protected function createEntity() { \Drupal::service('comment.manager')->addDefaultField('entity_test', 'bar', 'comment'); // Display comments in a flat list; threaded comments are not render cached. - $field = FieldConfig::loadByName('entity_test', 'bar', 'comment'); - $field->settings['default_mode'] = CommentManagerInterface::COMMENT_MODE_FLAT; - $field->save(); + $comment_type = CommentType::load('comment'); + $comment_type->setThreadingMode(CommentTypeInterface::THREADING_MODE_FLAT); + $comment_type->save(); // Create a "Camelids" test entity. $entity_test = entity_create('entity_test', array( diff --git a/core/modules/comment/src/Tests/CommentInterfaceTest.php b/core/modules/comment/src/Tests/CommentInterfaceTest.php index ccfe8d6..e107aee 100644 --- a/core/modules/comment/src/Tests/CommentInterfaceTest.php +++ b/core/modules/comment/src/Tests/CommentInterfaceTest.php @@ -7,7 +7,7 @@ namespace Drupal\comment\Tests; -use Drupal\comment\CommentManagerInterface; +use Drupal\comment\CommentTypeInterface; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\comment\Entity\Comment; @@ -27,7 +27,7 @@ function testCommentInterface() { $this->setCommentPreview(DRUPAL_DISABLED); $this->setCommentForm(TRUE); $this->setCommentSubject(FALSE); - $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setThreadingMode(CommentTypeInterface::THREADING_MODE_THREADED); $this->drupalLogout(); // Post comment #1 without subject or preview. diff --git a/core/modules/comment/src/Tests/CommentLinksTest.php b/core/modules/comment/src/Tests/CommentLinksTest.php index 97c2d6f..773e7ea8 100644 --- a/core/modules/comment/src/Tests/CommentLinksTest.php +++ b/core/modules/comment/src/Tests/CommentLinksTest.php @@ -8,11 +8,8 @@ namespace Drupal\comment\Tests; use Drupal\comment\CommentInterface; -use Drupal\comment\CommentManagerInterface; -use Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\Core\Language\LanguageInterface; -use Drupal\entity\Entity\EntityViewDisplay; /** * Basic comment links tests to ensure markup present. @@ -45,28 +42,6 @@ class CommentLinksTest extends CommentTestBase { public static $modules = array('views'); /** - * {@inheritdoc} - */ - public function setUp() { - parent::setUp(); - // Set comment field to use the link formatter on article teasers. - entity_get_display('node', 'article', 'teaser') - ->setComponent('comment', array( - 'label' => 'hidden', - 'type' => 'comment_links', - 'weight' => 20, - 'settings' => array( - 'per_page' => 50, - 'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED, - 'form_location' => CommentItemInterface::FORM_BELOW, - 'show_links' => CommentDefaultFormatter::LINKS_TEASER, - ), - )) - ->save(); - } - - - /** * Tests that comment links are output and can be hidden. */ public function testCommentLinks() { diff --git a/core/modules/comment/src/Tests/CommentNewIndicatorTest.php b/core/modules/comment/src/Tests/CommentNewIndicatorTest.php index d7471bd..0be4f01 100644 --- a/core/modules/comment/src/Tests/CommentNewIndicatorTest.php +++ b/core/modules/comment/src/Tests/CommentNewIndicatorTest.php @@ -110,7 +110,7 @@ public function testCommentNewCommentsIndicator() { $this->assertIdentical(1, count($this->xpath('//*[@data-history-node-last-comment-timestamp="' . $comment->getChangedTime() . '"]')), 'data-history-node-last-comment-timestamp attribute is set to the correct value.'); $this->assertIdentical(1, count($this->xpath('//*[@data-history-node-field-name="comment"]')), 'data-history-node-field-name attribute is set to the correct value.'); $this->assertIdentical(1, count($this->xpath('//*[@data-comment-per-page="50"]')), 'data-comment-per-page attribute is set to the correct value.'); - $this->assertIdentical(1, count($this->xpath('//*[@data-comment-default-mode="1"]')), 'data-comment-default-mode is set to the correct value.'); + $this->assertIdentical(1, count($this->xpath('//*[@data-comment-threading-mode="1"]')), 'data-comment-threading-mode is set to the correct value.'); $response = $this->renderNewCommentsNodeLinks(array($this->node->id())); $this->assertResponse(200); $json = Json::decode($response); diff --git a/core/modules/comment/src/Tests/CommentNodeAccessTest.php b/core/modules/comment/src/Tests/CommentNodeAccessTest.php index e0cec66..28e76d7 100644 --- a/core/modules/comment/src/Tests/CommentNodeAccessTest.php +++ b/core/modules/comment/src/Tests/CommentNodeAccessTest.php @@ -7,7 +7,7 @@ namespace Drupal\comment\Tests; -use Drupal\comment\CommentManagerInterface; +use Drupal\comment\CommentTypeInterface; /** * Tests comments with node access. @@ -54,7 +54,7 @@ function testThreadedCommentView() { $this->setCommentPreview(DRUPAL_DISABLED); $this->setCommentForm(TRUE); $this->setCommentSubject(TRUE); - $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setThreadingMode(CommentTypeInterface::THREADING_MODE_THREADED); $this->drupalLogout(); // Post comment. diff --git a/core/modules/comment/src/Tests/CommentPagerTest.php b/core/modules/comment/src/Tests/CommentPagerTest.php index 920e4aa..42af43a 100644 --- a/core/modules/comment/src/Tests/CommentPagerTest.php +++ b/core/modules/comment/src/Tests/CommentPagerTest.php @@ -7,7 +7,7 @@ namespace Drupal\comment\Tests; -use Drupal\comment\CommentManagerInterface; +use Drupal\comment\CommentTypeInterface; use Drupal\Component\Utility\String; /** @@ -34,7 +34,7 @@ function testCommentPaging() { $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE); $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE); - $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.'); + $this->setThreadingMode(CommentTypeInterface::THREADING_MODE_FLAT); // Set comments to one per page so that we are able to test paging without // needing to insert large numbers of comments. @@ -74,7 +74,7 @@ function testCommentPaging() { // If we switch to threaded mode, the replies on the oldest comment // should be bumped to the first page and comment 6 should be bumped // to the second page. - $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.'); + $this->setThreadingMode(CommentTypeInterface::THREADING_MODE_THREADED, 'Switched to threaded mode.'); $this->drupalGet('node/' . $node->id(), array('query' => array('page' => 0))); $this->assertTrue($this->commentExists($reply, TRUE), 'In threaded mode, reply appears on page 1.'); $this->assertFalse($this->commentExists($comments[1]), 'In threaded mode, comment 2 has been bumped off of page 1.'); @@ -134,7 +134,7 @@ function testCommentOrderingThreading() { // - 2 // - 5 - $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.'); + $this->setThreadingMode(CommentTypeInterface::THREADING_MODE_FLAT); $expected_order = array( 0, @@ -148,7 +148,7 @@ function testCommentOrderingThreading() { $this->drupalGet('node/' . $node->id()); $this->assertCommentOrder($comments, $expected_order); - $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.'); + $this->setThreadingMode(CommentTypeInterface::THREADING_MODE_THREADED, 'Switched to threaded mode.'); $expected_order = array( 0, @@ -229,7 +229,7 @@ function testCommentNewPageIndicator() { // - 2 // - 5 - $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.'); + $this->setThreadingMode(CommentTypeInterface::THREADING_MODE_FLAT); $expected_pages = array( 1 => 5, // Page of comment 5 @@ -243,11 +243,11 @@ function testCommentNewPageIndicator() { $node = node_load($node->id()); foreach ($expected_pages as $new_replies => $expected_page) { $returned_page = \Drupal::entityManager()->getStorage('comment') - ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, CommentManagerInterface::COMMENT_MODE_FLAT, 1); + ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, CommentTypeInterface::THREADING_MODE_FLAT, 1); $this->assertIdentical($expected_page, $returned_page, format_string('Flat mode, @new replies: expected page @expected, returned page @returned.', array('@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page))); } - $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.'); + $this->setThreadingMode(CommentTypeInterface::THREADING_MODE_THREADED, 'Switched to threaded mode.'); $expected_pages = array( 1 => 5, // Page of comment 5 @@ -262,7 +262,7 @@ function testCommentNewPageIndicator() { $node = node_load($node->id()); foreach ($expected_pages as $new_replies => $expected_page) { $returned_page = \Drupal::entityManager()->getStorage('comment') - ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, CommentManagerInterface::COMMENT_MODE_THREADED, 1); + ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, CommentTypeInterface::THREADING_MODE_THREADED, 1); $this->assertEqual($expected_page, $returned_page, format_string('Threaded mode, @new replies: expected page @expected, returned page @returned.', array('@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page))); } } @@ -309,7 +309,7 @@ function testTwoPagers() { foreach (array('comment', 'comment_2') as $field_name) { $this->setCommentForm(TRUE, $field_name); $this->setCommentPreview(DRUPAL_OPTIONAL, $field_name); - $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.', $field_name); + $this->setThreadingMode(CommentTypeInterface::THREADING_MODE_FLAT); // Set comments to one per page so that we are able to test paging without // needing to insert large numbers of comments. diff --git a/core/modules/comment/src/Tests/CommentPreviewTest.php b/core/modules/comment/src/Tests/CommentPreviewTest.php index 7a5c251..80f652a 100644 --- a/core/modules/comment/src/Tests/CommentPreviewTest.php +++ b/core/modules/comment/src/Tests/CommentPreviewTest.php @@ -7,7 +7,7 @@ namespace Drupal\comment\Tests; -use Drupal\comment\CommentManagerInterface; +use Drupal\comment\CommentTypeInterface; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\comment\Entity\Comment; @@ -36,7 +36,7 @@ function testCommentPreview() { $this->setCommentPreview(DRUPAL_OPTIONAL); $this->setCommentForm(TRUE); $this->setCommentSubject(TRUE); - $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setThreadingMode(CommentTypeInterface::THREADING_MODE_THREADED); $this->drupalLogout(); // Login as web user and add a signature and a user picture. @@ -79,7 +79,7 @@ function testCommentEditPreviewSave() { $this->setCommentPreview(DRUPAL_OPTIONAL); $this->setCommentForm(TRUE); $this->setCommentSubject(TRUE); - $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setThreadingMode(CommentTypeInterface::THREADING_MODE_THREADED); $edit = array(); $date = new DrupalDateTime('2008-03-02 17:23'); diff --git a/core/modules/comment/src/Tests/CommentStatisticsTest.php b/core/modules/comment/src/Tests/CommentStatisticsTest.php index a6495a7..8c19182 100644 --- a/core/modules/comment/src/Tests/CommentStatisticsTest.php +++ b/core/modules/comment/src/Tests/CommentStatisticsTest.php @@ -7,7 +7,7 @@ namespace Drupal\comment\Tests; -use Drupal\comment\CommentManagerInterface; +use Drupal\comment\CommentTypeInterface; use Drupal\comment\Entity\Comment; /** @@ -41,7 +41,7 @@ function testCommentNodeCommentStatistics() { $this->setCommentPreview(DRUPAL_DISABLED); $this->setCommentForm(TRUE); $this->setCommentSubject(FALSE); - $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setThreadingMode(CommentTypeInterface::THREADING_MODE_THREADED); $this->drupalLogout(); // Checks the initial values of node comment statistics with no comment. diff --git a/core/modules/comment/src/Tests/CommentTestBase.php b/core/modules/comment/src/Tests/CommentTestBase.php index 76c1137..71c7402 100644 --- a/core/modules/comment/src/Tests/CommentTestBase.php +++ b/core/modules/comment/src/Tests/CommentTestBase.php @@ -82,12 +82,12 @@ protected function setUp() { $this->container->get('comment.manager')->addDefaultField('node', 'article'); // Set some display options for search results and print pages. - if (module_exists('book')) { + if ($this->container->get('module_handler')->moduleExists('book')) { $display = entity_get_display('node', 'article', 'print'); $display->removeComponent('comment'); $display->save(); } - if (module_exists('search')) { + if ($this->container->get('module_handler')->moduleExists('search')) { foreach (array('search_result', 'search_index') as $view_mode) { $display = entity_get_display('node', 'article', $view_mode); $display->removeComponent('comment'); @@ -281,7 +281,7 @@ public function setCommentPreview($mode, $field_name = 'comment') { * Defaults to 'comment'. */ public function setCommentForm($enabled, $field_name = 'comment') { - $this->setCommentFormatterSettings('form_location', ($enabled ? CommentItemInterface::FORM_BELOW : CommentItemInterface::COMMENT_FORM_SEPARATE_PAGE), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.', $field_name); + $this->setCommentFormatterSettings('form_location', ($enabled ? CommentItemInterface::FORM_BELOW : CommentItemInterface::FORM_SEPARATE_PAGE), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.', $field_name); } /** @@ -311,7 +311,23 @@ public function setCommentsPerPage($number, $field_name = 'comment') { } /** - * Sets a comment settings variable for the article content type. + * Sets the threading mode for this comment type. + * + * @param int $mode + * The threading mode. + * @param string $message + * (optional) Status message to display. Defaults to 'Comment threading + * changed.' + */ + public function setThreadingMode($mode, $message = 'Comment threading changed.') { + $comment_type = CommentType::load('article'); + $comment_type->setThreadingMode($mode); + $comment_type->save(); + $this->pass($message); + } + + /** + * Sets a comment field settings variable for the article content type. * * @param string $name * Name of variable. @@ -332,7 +348,7 @@ public function setCommentSettings($name, $value, $message, $field_name = 'comme } /** - * Sets a comment settings variable for the article content type. + * Sets a display formatter settings variable for the article content type. * * @param string $name * Name of variable. @@ -345,7 +361,10 @@ public function setCommentSettings($name, $value, $message, $field_name = 'comme * Defaults to 'comment'. */ public function setCommentFormatterSettings($name, $value, $message, $field_name = 'comment') { - $display = entity_get_display('node', 'article', 'full'); + // Some of our tests (like CommentLinksTest) assume we are working with the + // 'default' display for full node pages. This works (just like default site + // settings do), as long as we never create/save 'full' display. + $display = entity_get_display('node', 'article', 'default'); $component = $display->getComponent($field_name); $component['settings'][$name] = $value; $display->setComponent($field_name, $component); diff --git a/core/modules/comment/src/Tests/CommentThreadingTest.php b/core/modules/comment/src/Tests/CommentThreadingTest.php index f919d13..848ba2c 100644 --- a/core/modules/comment/src/Tests/CommentThreadingTest.php +++ b/core/modules/comment/src/Tests/CommentThreadingTest.php @@ -7,7 +7,7 @@ namespace Drupal\comment\Tests; -use Drupal\comment\CommentManagerInterface; +use Drupal\comment\CommentTypeInterface; /** * Tests to make sure the comment number increments properly. @@ -24,7 +24,7 @@ function testCommentThreading() { $this->setCommentPreview(DRUPAL_DISABLED); $this->setCommentForm(TRUE); $this->setCommentSubject(TRUE); - $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setThreadingMode(CommentTypeInterface::THREADING_MODE_THREADED); $this->drupalLogout(); // Create a node. diff --git a/core/modules/comment/templates/field--comment.html.twig b/core/modules/comment/templates/field--comment.html.twig index 9aae572..9691012 100644 --- a/core/modules/comment/templates/field--comment.html.twig +++ b/core/modules/comment/templates/field--comment.html.twig @@ -15,7 +15,7 @@ * - comments: List of comments rendered through comment.html.twig. * - content_attributes: HTML attributes for the form title. * - comment_form: The 'Add new comment' form. - * - comment_display_mode: Is the comments are threaded. + * - comment_threading_mode: Is non-zero if the comments are threaded. * - comment_type: The comment type bundle ID for the comment field. * * @see template_preprocess_field() diff --git a/core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php b/core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php index fdcb8f5..df3e18e 100644 --- a/core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php +++ b/core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php @@ -8,6 +8,7 @@ namespace Drupal\Tests\comment\Unit; use Drupal\comment\CommentLinkBuilder; +use Drupal\comment\CommentTypeInterface; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\Core\Url; use Drupal\node\NodeInterface; @@ -21,6 +22,55 @@ class CommentLinkBuilderTest extends UnitTestCase { /** + * Entity manager mock. + * + * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $entityManager; + + /** + * Entity storage mock which knows entity_view_display load method/argument. + * + * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $entityStorage; + + /** + * Entity storage mock which knows comment_type load method/argument. + * + * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $commentTypeStorage; + + /** + * Entity view display mock. + * + * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $entityViewDisplay; + + /** + * Entity view display mock used for the 'full' display mode. + * + * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $entityViewDisplayFull; + + /** + * Comment type mock. + * + * @var \Drupal\comment\CommentTypeInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $commentType; + + /** + * Field storage definition mock. + * + * @var \Drupal\Core\Field\FieldStorageDefinitionInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $fieldStorageDefinition; + + /** * Comment manager mock. * * @var \Drupal\comment\CommentManagerInterface|\PHPUnit_Framework_MockObject_MockObject @@ -64,15 +114,44 @@ class CommentLinkBuilderTest extends UnitTestCase { * Prepares mocks for the test. */ protected function setUp() { + $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); + $this->entityStorage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface'); + $this->entityViewDisplay = $this->getMock('Drupal\Core\Entity\Display\EntityViewDisplayInterface'); + $this->entityViewDisplayFull = $this->getMock('Drupal\Core\Entity\Display\EntityViewDisplayInterface'); + $this->commentTypeStorage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface'); + $this->commentType = $this->getMock('\Drupal\comment\CommentTypeInterface'); + $this->entityViewDisplay = $this->getMock('Drupal\Core\Entity\Display\EntityViewDisplayInterface'); + $this->fieldStorageDefinition = $this->getMock('\Drupal\Core\Field\FieldStorageDefinitionInterface'); $this->commentManager = $this->getMock('\Drupal\comment\CommentManagerInterface'); $this->stringTranslation = $this->getStringTranslationStub(); $this->moduleHandler = $this->getMock('\Drupal\Core\Extension\ModuleHandlerInterface'); $this->currentUser = $this->getMock('\Drupal\Core\Session\AccountProxyInterface'); - $this->commentLinkBuilder = new CommentLinkBuilder($this->currentUser, $this->commentManager, $this->moduleHandler, $this->stringTranslation); + $this->commentLinkBuilder = new CommentLinkBuilder($this->currentUser, $this->entityManager, $this->commentManager, $this->moduleHandler, $this->stringTranslation); + $this->fieldStorageDefinition->expects($this->any()) + ->method('getSetting') + ->with('comment_type') + ->willReturn('comment'); + $this->entityManager->expects($this->any()) + ->method('getStorage') + ->will($this->returnValueMap(array( + array('entity_view_display', $this->entityStorage), + array('comment_type', $this->commentTypeStorage), + ))); + $this->entityManager->expects($this->any()) + ->method('getFieldStorageDefinitions') + ->with('node') + ->willReturn(array( + // The field name from getFields() + 'comment' => $this->fieldStorageDefinition + )); + $this->commentTypeStorage->expects($this->any()) + ->method('load') + ->willReturn($this->commentType); $this->commentManager->expects($this->any()) ->method('getFields') ->with('node') ->willReturn(array( + // Key is a field name; we're choosing comment (and don't need a value). 'comment' => array(), )); $this->commentManager->expects($this->any()) @@ -98,6 +177,10 @@ protected function setUp() { * TRUE if the use has 'post comments' permission. * @param bool $is_anonymous * TRUE if the user is anonymous. + * @param int|null $form_location + * One of CommentItemInterface::FORM_BELOW|FORM_SEPARATE_PAGE or NULL + * @param int|null $threading_mode + * One of CommentTypeInterface::THREADING_MODE_THREADED or NULL * @param array $expected * Array of expected links keyed by link ID. Can be either string (link * title) or array of link properties. @@ -106,7 +189,7 @@ protected function setUp() { * * @covers ::buildCommentedEntityLinks() */ - public function testCommentLinkBuilder(NodeInterface $node, $context, $has_access_comments, $history_exists, $has_post_comments, $is_anonymous, $expected) { + public function testCommentLinkBuilder(NodeInterface $node, $context, $has_access_comments, $history_exists, $has_post_comments, $is_anonymous, $form_location, $threading_mode, $expected) { $this->moduleHandler->expects($this->any()) ->method('moduleExists') ->with('history') @@ -123,6 +206,69 @@ public function testCommentLinkBuilder(NodeInterface $node, $context, $has_acces $this->currentUser->expects($this->any()) ->method('isAnonymous') ->willReturn($is_anonymous); + + if ($context['view_mode'] == 'full') { + $this->entityViewDisplay->expects($this->any()) + ->method('getComponent') + ->willReturn(isset($form_location) && isset($threading_mode) + ? array( + 'settings' => array( + 'form_location' => $form_location, + // This only influences the page argument to the new-comments + // url, which we're not testing, so return static value. + 'per_page' => 50 + ) + ) + : NULL); + $this->entityStorage->expects($this->any()) + ->method('load') + ->with($node->getEntityTypeId() . '.' . $node->bundle() . '.full') + ->willReturn($this->entityViewDisplay); + } + else { + // From the field formatter settings for the current display, the class + // should be using 'form_location'. + $this->entityViewDisplay->expects($this->any()) + ->method('getComponent') + ->willReturn(isset($form_location) + ? array( + 'settings' => array( + 'form_location' => $form_location, + 'per_page' => 50 + ) + ) + : NULL); + // From the field formatter settings for the full display, the class + // should be using 'form_location'. + $this->entityViewDisplayFull->expects($this->any()) + ->method('getComponent') + ->willReturn(isset($threading_mode) + ? array( + 'settings' => array( + 'form_location' => CommentItemInterface::FORM_BELOW, + 'per_page' => 50 + ) + ) + : NULL); + $this->entityStorage->expects($this->any()) + ->method('load') + ->willReturnMap(array( + array( + $node->getEntityTypeId() . '.' . $node->bundle() . '.full', + $this->entityViewDisplayFull + ), + array( + $node->getEntityTypeId() . '.' . $node->bundle() . '.' . $context['view_mode'], + $this->entityViewDisplay + ), + )); + } + + // This setting influences the page argument to the 'new comments' url, + // which we're not testing, so return static values. + $this->commentType->expects($this->any()) + ->method('getThreadingMode') + ->willReturn(CommentTypeInterface::THREADING_MODE_THREADED); $links = $this->commentLinkBuilder->buildCommentedEntityLinks($node, $context); if (!empty($expected)) { if (!empty($links)) { @@ -148,12 +294,10 @@ public function testCommentLinkBuilder(NodeInterface $node, $context, $has_acces } if ($context['view_mode'] == 'rss' && $node->get('comment')->status) { $found = FALSE; - if ($node->get('comment')->status) { - foreach ($node->rss_elements as $element) { - if ($element['key'] == 'comments') { - $found = TRUE; - break; - } + foreach ($node->rss_elements as $element) { + if ($element['key'] == 'comments') { + $found = TRUE; + break; } } $this->assertTrue($found); @@ -167,23 +311,27 @@ public function getLinkCombinations() { $cases = array(); // No links should be created if the entity doesn't have the field. $cases[] = array( - $this->getMockNode(FALSE, CommentItemInterface::OPEN, CommentItemInterface::FORM_BELOW, 1), + $this->getMockNode(FALSE, CommentItemInterface::OPEN, 1), array('view_mode' => 'teaser'), TRUE, TRUE, TRUE, TRUE, + CommentItemInterface::FORM_BELOW, + CommentTypeInterface::THREADING_MODE_THREADED, array(), ); foreach (array('search_result', 'search_index', 'print') as $view_mode) { // Nothing should be output in these view modes. $cases[] = array( - $this->getMockNode(TRUE, CommentItemInterface::OPEN, CommentItemInterface::FORM_BELOW, 1), + $this->getMockNode(TRUE, CommentItemInterface::OPEN, 1), array('view_mode' => $view_mode), TRUE, TRUE, TRUE, TRUE, + CommentItemInterface::FORM_BELOW, + CommentTypeInterface::THREADING_MODE_THREADED, array(), ); } @@ -194,7 +342,23 @@ public function getLinkCombinations() { 'has_access_comments' => array(0, 1), 'history_exists' => array(FALSE, TRUE), 'has_post_comments' => array(0, 1), - 'form_location' => array(CommentItemInterface::FORM_BELOW, CommentItemInterface::FORM_SEPARATE_PAGE), + // form_location is a field formatter setting which is checked on the + // current view mode (see below); NULL is used for "the field is hidden" + // (so no formatter settings) in this mode. + 'form_location' => array( + CommentItemInterface::FORM_BELOW, + CommentItemInterface::FORM_SEPARATE_PAGE, + NULL, + ), + // threading_mode is a field formatter setting which is checked on the + // full view mode; NULL is used for "the field is hidden" (so no formatter + // settings) in full mode. + // Threading mode only influences the 'page' URL argument, which we are + // not testing (yet?) here, so we only distinguish NULL and non-NULL. + 'threading_mode' => array( + CommentTypeInterface::THREADING_MODE_THREADED, + NULL, + ), 'comments' => array( CommentItemInterface::OPEN, CommentItemInterface::CLOSED, @@ -206,18 +370,35 @@ public function getLinkCombinations() { ); $permutations = TestBase::generatePermutations($combinations); foreach ($permutations as $combination) { + if ($combination['view_mode'] == 'full' + && isset($combination['form_location']) != isset($combination['threading_mode'])) { + // Comment field in full mode is hidden AND visible at the same time; + // impossible. + continue; + } + + $formatter_options_set = isset($combination['form_location']); + $formatter_options_set_full_mode = isset($combination['threading_mode']); + $case = array( - $this->getMockNode(TRUE, $combination['comments'], $combination['form_location'], $combination['comment_count']), + $this->getMockNode(TRUE, $combination['comments'], $combination['comment_count']), array('view_mode' => $combination['view_mode']), $combination['has_access_comments'], $combination['history_exists'], $combination['has_post_comments'], $combination['is_anonymous'], + $combination['form_location'], + $combination['threading_mode'], ); $expected = array(); - // When comments are enabled in teaser mode, and comments exist, and the - // user has access - we can output the comment count. - if ($combination['comments'] && $combination['view_mode'] == 'teaser' && $combination['comment_count'] && $combination['has_access_comments']) { + // When comments exist, you're viewing teaser mode and comments are + // visible in 'full' view mode and the user has access, we can output the + // comment count with link. + if ($combination['view_mode'] == 'teaser' + && $formatter_options_set_full_mode + && $combination['comments'] != CommentItemInterface::HIDDEN + && $combination['comment_count'] + && $combination['has_access_comments']) { $expected['comment-comments'] = '1 comment'; // And if history module exists, we can show a 'new comments' link. if ($combination['history_exists']) { @@ -230,9 +411,12 @@ public function getLinkCombinations() { if ($combination['comments'] == CommentItemInterface::OPEN) { // And the user has post-comments permission. if ($combination['has_post_comments']) { - // If the view mode is teaser, or the user can access comments and - // comments exist or the form is on a separate page. - if ($combination['view_mode'] == 'teaser' || ($combination['has_access_comments'] && $combination['comment_count']) || $combination['form_location'] == CommentItemInterface::FORM_SEPARATE_PAGE) { + // If the form is on a separate page (which is also the case always + // when comment formatter settings are not set for this view mode), + // or the user can access comments and comments exist (in which + // case we need the link to skip beyond the existing comments). + if (($combination['has_access_comments'] && $combination['comment_count']) + || $combination['form_location'] == CommentItemInterface::FORM_SEPARATE_PAGE) { // There should be a add comment link. $expected['comment-add'] = array('title' => 'Add new comment'); if ($combination['form_location'] == CommentItemInterface::FORM_BELOW) { @@ -266,15 +450,13 @@ public function getLinkCombinations() { * TRUE if the node has the 'comment' field. * @param int $comment_status * One of CommentItemInterface::OPEN|HIDDEN|CLOSED - * @param int $form_location - * One of CommentItemInterface::FORM_BELOW|FORM_SEPARATE_PAGE * @param int $comment_count * Number of comments against the field. * * @return \Drupal\node\NodeInterface|\PHPUnit_Framework_MockObject_MockObject * Mock node for testing. */ - protected function getMockNode($has_field, $comment_status, $form_location, $comment_count) { + protected function getMockNode($has_field, $comment_status, $comment_count) { $node = $this->getMock('\Drupal\node\NodeInterface'); $node->expects($this->once()) ->method('hasField') @@ -294,10 +476,6 @@ protected function getMockNode($has_field, $comment_status, $form_location, $com ->willReturn($field_item); $field_definition = $this->getMock('\Drupal\Core\Field\FieldDefinitionInterface'); - $field_definition->expects($this->any()) - ->method('getSetting') - ->with('form_location') - ->willReturn($form_location); $node->expects($this->any()) ->method('getFieldDefinition') ->with('comment') @@ -312,6 +490,10 @@ protected function getMockNode($has_field, $comment_status, $form_location, $com ->willReturn('node'); $node->expects($this->any()) + ->method('bundle') + ->willReturn('article'); + + $node->expects($this->any()) ->method('id') ->willReturn(1); diff --git a/core/modules/forum/forum.install b/core/modules/forum/forum.install index 6605c1d..3ab6db8 100644 --- a/core/modules/forum/forum.install +++ b/core/modules/forum/forum.install @@ -5,10 +5,11 @@ * Install, update, and uninstall functions for the Forum module. */ +use Drupal\comment\CommentTypeInterface; +use Drupal\comment\Entity\CommentType; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; -use Drupal\comment\CommentManagerInterface; /** * Implements hook_install(). @@ -91,9 +92,9 @@ function forum_install() { Drupal::service('comment.manager')->addDefaultField('node', 'forum', 'comment_forum', CommentItemInterface::OPEN, 'comment_forum'); // Add here because we don't have param in addDefaultField function. - $field = FieldConfig::loadByName('node', 'forum', 'comment_forum'); - $field->settings['default_mode'] = CommentManagerInterface::COMMENT_MODE_FLAT; - $field->save(); + $comment_type = CommentType::load('comment_forum'); + $comment_type->setThreadingMode(CommentTypeInterface::THREADING_MODE_FLAT); + $comment_type->save(); // Hide label for comment field. entity_get_display('node', 'forum', 'default') diff --git a/core/modules/forum/forum.module b/core/modules/forum/forum.module index ade6155..ee43ca8 100644 --- a/core/modules/forum/forum.module +++ b/core/modules/forum/forum.module @@ -467,6 +467,8 @@ function template_preprocess_forums(&$variables) { $variables['tid'] = $variables['term']->id(); $display = entity_get_display('node', 'forum', 'full'); $settings = $display->getComponent('comment_forum')['settings']; + $field_definition = \Drupal::entityManager()->getFieldStorageDefinitions('node')['comment_forum']; + $comment_type = CommentType::load($field_definition->getSetting('comment_type')); if ($variables['forums_defined'] = count($variables['forums']) || count($variables['parents'])) { if (!empty($variables['forums'])) { $variables['forums'] = array( @@ -533,7 +535,7 @@ function template_preprocess_forums(&$variables) { if ($topic->new_replies) { $page_number = \Drupal::entityManager()->getStorage('comment') - ->getNewCommentPageNumber($topic->comment_count, $topic->new_replies, $topic, $settings['default_mode'], $settings['per_page'], 'comment_node_forum'); + ->getNewCommentPageNumber($topic->comment_count, $topic->new_replies, $topic, $comment_type->getThreadingMode(), $settings['per_page'], 'comment_forum'); $query = $page_number ? array('page' => $page_number) : NULL; $variables['topics'][$id]->new_text = format_plural($topic->new_replies, '1 new post in topic %title', '@count new posts in topic %title', array('%title' => $variables['topics'][$id]->label())); $variables['topics'][$id]->new_url = \Drupal::url('entity.node.canonical', ['node' => $topic->id()], ['query' => $query, 'fragment' => 'new']); diff --git a/core/profiles/standard/standard.install b/core/profiles/standard/standard.install index 647a406..87c4845 100644 --- a/core/profiles/standard/standard.install +++ b/core/profiles/standard/standard.install @@ -4,7 +4,8 @@ * Install, update and uninstall functions for the standard installation profile. */ -use Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter; +use Drupal\comment\CommentTypeInterface; +use Drupal\comment\Entity\CommentType; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; /** @@ -26,26 +27,15 @@ function standard_install() { // Add comment field to article node type. \Drupal::service('comment.manager')->addDefaultField('node', 'article', 'comment', CommentItemInterface::OPEN); + $comment_type = CommentType::load('comment'); + $comment_type->setThreadingMode(CommentTypeInterface::THREADING_MODE_THREADED); + $comment_type->save(); // Set some display options for comments in search results. foreach (array('search_result', 'search_index') as $view_mode) { $display = entity_get_display('node', 'article', $view_mode); $display->removeComponent('comment'); $display->save(); } - // Set comment field to use the link formatter on article teasers. - entity_get_display('node', 'article', 'teaser') - ->setComponent('comment', array( - 'label' => 'hidden', - 'type' => 'comment_links', - 'weight' => 20, - 'settings' => array( - 'per_page' => 50, - 'default_mode' => COMMENT_MODE_THREADED, - 'form_location' => COMMENT_FORM_BELOW, - 'show_links' => CommentDefaultFormatter::LINKS_TEASER, - ), - )) - ->save(); // Hide the comment field in the rss view mode. entity_get_display('node', 'article', 'rss')