diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index d7e5338..ec56f8e 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\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\Component\Utility\String; @@ -473,16 +472,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; } 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, $display_settings['settings']['default_mode'], $display_settings['settings']['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); @@ -830,7 +831,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/config/schema/comment.schema.yml b/core/modules/comment/config/schema/comment.schema.yml index 47965e2..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 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 9894312..19449cc 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -381,11 +381,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 6038f37..05554f4 100644 --- a/core/modules/comment/src/CommentLinkBuilder.php +++ b/core/modules/comment/src/CommentLinkBuilder.php @@ -7,6 +7,7 @@ namespace Drupal\comment; +use Drupal\comment\CommentTypeInterface; use Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\Core\Entity\ContentEntityInterface; @@ -133,6 +134,7 @@ public function buildCommentedEntityFieldLinks($field_name, FieldItemListInterfa return $build; } + $comment_type = $this->entityManager->getStorage('comment_type')->load($this->getFieldSetting('comment_type')); $entity = $items->getEntity(); if ($link_style == CommentDefaultFormatter::LINKS_RSS) { // Add a comments RSS element which is a URL to the comments of this @@ -166,7 +168,7 @@ public function buildCommentedEntityFieldLinks($field_name, FieldItemListInterfa '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-default-mode' => $display_settings['default_mode'], + 'data-comment-threading-mode' => $comment_type->getThreadingMode(), 'data-comment-per-page' => $display_settings['per_page'], ), ); @@ -261,7 +263,7 @@ public function buildCommentedEntityFieldLinks($field_name, FieldItemListInterfa 'entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name, - 'default_mode' => $display_settings['default_mode'], + 'threading_mode' => $comment_type->getThreadingMode(), 'per_page' => $display_settings['per_page'], ), ); diff --git a/core/modules/comment/src/CommentManager.php b/core/modules/comment/src/CommentManager.php index 0174975..3609082 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,7 +204,6 @@ 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, ) 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 0dbe492..d33065c 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, ContentEntityInterface $entity, $mode = CommentManagerInterface::COMMENT_MODE_THREADED, $comments_per_page = 50, $field_name = 'comment') { + public function getNewCommentPageNumber($total_comments, $new_comments, ContentEntityInterface $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 21adaf0..6d52d1d 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\ContentEntityInterface $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, ContentEntityInterface $entity, $mode = CommentManagerInterface::COMMENT_MODE_THREADED, $comments_per_page = 50, $field_name = 'comment'); + public function getNewCommentPageNumber($total_comments, $new_comments, ContentEntityInterface $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, ContentE * @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 0d93b1c..bc33325 100644 --- a/core/modules/comment/src/CommentTypeForm.php +++ b/core/modules/comment/src/CommentTypeForm.php @@ -62,6 +62,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( @@ -111,6 +112,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 3326560..dbe1ccf 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 Symfony\Component\DependencyInjection\ContainerInterface; @@ -305,9 +304,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) { @@ -356,7 +356,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..706daf4 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 acd41b5..e84fcd4 100644 --- a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php +++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php @@ -66,7 +66,6 @@ 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(); @@ -204,13 +203,15 @@ 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 // should display if the user is an administrator. if ((($entity->get($field_name)->comment_count && $this->currentUser->hasPermission('access comments')) || $this->currentUser->hasPermission('administer comments'))) { - $comments = $this->storage->loadThread($entity, $field_name, $this->getSetting('default_mode'), $this->getSetting('per_page'), $this->getSetting('pager_id')); + $comments = $this->storage->loadThread($entity, $field_name, $comment_type->getThreadingMode(), $this->getSetting('per_page'), $this->getSetting('pager_id')); if ($comments) { comment_prepare_thread($comments); $build = $this->viewBuilder->viewMultiple($comments); @@ -267,7 +268,7 @@ 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' => $links, @@ -282,12 +283,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'), @@ -338,15 +333,14 @@ public function settingsSummary() { '@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 comments with @form, using pager ID @id and @links', $variables)); } - return array($this->t('Showing @per_page @mode comments with @form and @links', $variables)); + return array($this->t('Showing @per_page comments with @form and @links', $variables)); } } diff --git a/core/modules/comment/src/Plugin/views/field/NodeNewComments.php b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php index 735db88..51c1cd4 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/CommentInterfaceTest.php b/core/modules/comment/src/Tests/CommentInterfaceTest.php index 410a4fe..24b9150 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 e4a4304..6d0176a 100644 --- a/core/modules/comment/src/Tests/CommentLinksTest.php +++ b/core/modules/comment/src/Tests/CommentLinksTest.php @@ -8,7 +8,7 @@ namespace Drupal\comment\Tests; use Drupal\comment\CommentInterface; -use Drupal\comment\CommentManagerInterface; +use Drupal\comment\CommentTypeInterface; use Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\Core\Language\LanguageInterface; @@ -56,7 +56,7 @@ public function setUp() { 'weight' => 20, 'settings' => array( 'per_page' => 50, - 'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED, + 'default_mode' => CommentTypeInterface::THREADING_MODE_THREADED, 'form_location' => CommentItemInterface::FORM_BELOW, 'show_links' => CommentDefaultFormatter::LINKS_TEASER, ), diff --git a/core/modules/comment/src/Tests/CommentNewIndicatorTest.php b/core/modules/comment/src/Tests/CommentNewIndicatorTest.php index e53ad1e..f613b0d 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..b49e08f 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->setCommentFormatterSettings('default_mode', CommentTypeInterface::THREADING_MODE_THREADED, 'Comment paging changed.'); $this->drupalLogout(); // Post comment. diff --git a/core/modules/comment/src/Tests/CommentPagerTest.php b/core/modules/comment/src/Tests/CommentPagerTest.php index fb809cc..1224871 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; /** * Tests paging of comments and their settings. @@ -33,7 +33,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. @@ -73,7 +73,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.'); @@ -133,7 +133,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, @@ -147,7 +147,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, @@ -228,7 +228,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 @@ -242,11 +242,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 @@ -261,7 +261,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))); } } @@ -308,7 +308,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 ef3dfe9..9d23d7f 100644 --- a/core/modules/comment/src/Tests/CommentTestBase.php +++ b/core/modules/comment/src/Tests/CommentTestBase.php @@ -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. 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 8989bae..9e6808b 100644 --- a/core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php +++ b/core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php @@ -8,7 +8,6 @@ namespace Drupal\Tests\comment\Unit; use Drupal\comment\CommentLinkBuilder; -use Drupal\comment\CommentManagerInterface; use Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\node\NodeInterface; @@ -181,7 +180,6 @@ public function testCommentLinkBuilder(NodeInterface $node, $context, $has_acces 'show_links' => $link_style, // These settings influence the page argument to the 'new comments' url, // which we're not testing, so return static values. - 'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED, 'per_page' => 50 ))); diff --git a/core/modules/forum/forum.install b/core/modules/forum/forum.install index 6af734c..08e8f0f 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 0f4545d..5b606fd 100644 --- a/core/modules/forum/forum.module +++ b/core/modules/forum/forum.module @@ -496,6 +496,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( @@ -562,7 +564,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 = url('node/' . $topic->id(), array('query' => $query, 'fragment' => 'new')); diff --git a/core/profiles/standard/standard.install b/core/profiles/standard/standard.install index 0e55bc4..489afca 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\CommentManagerInterface; +use Drupal\comment\CommentTypeInterface; +use Drupal\comment\Entity\CommentType; use Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; @@ -27,6 +28,9 @@ 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); @@ -41,7 +45,6 @@ function standard_install() { 'weight' => 20, 'settings' => array( 'per_page' => 50, - 'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED, 'form_location' => CommentItemInterface::FORM_BELOW, 'show_links' => CommentDefaultFormatter::LINKS_TEASER, ),