diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index 0bf22a8..39f86b7 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -645,7 +645,7 @@ function comment_update_8006(&$sandbox) { 'label' => 'hidden', 'type' => 'comment_links', 'settings' => array('comment' => 1, 'new' => 1, 'add' => 1), - 'weight' => 1, + 'weight' => 10, ); $display = _update_8000_entity_get_display('node', $node_type, 'teaser'); $display->set('content.' . $field['field_name'], $display_options) diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 1574c9e..a2125af 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -11,7 +11,6 @@ */ use Drupal\node\Plugin\Core\Entity\Node; -use Drupal\entity\Plugin\Core\Entity\EntityDisplay; use Drupal\file\Plugin\Core\Entity\File; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityNG; @@ -473,9 +472,8 @@ function comment_permission() { function comment_permalink($cid) { if (($comment = comment_load($cid)) && ($entity = entity_load($comment->entity_type->value, $comment->entity_id->target_id))) { - $instance = field_info_instance($entity->entityType(), $comment->field_name->value, $entity->bundle()); // Find the current display page for this comment. - $page = comment_get_display_page($comment->id(), $instance); + $page = comment_get_display_page($comment, $entity); // @todo: Cleaner sub request handling. $request = drupal_container()->get('request'); @@ -745,21 +743,19 @@ function comment_get_thread(EntityInterface $entity, $field_name, $mode, $commen $query->condition('c.status', COMMENT_PUBLISHED); $count_query->condition('c.status', COMMENT_PUBLISHED); } - if ($mode === COMMENT_MODE_FLAT) { - $query->orderBy('c.cid', 'ASC'); - } - else { + if ($mode == COMMENT_MODE_THREADED) { // See comment above. Analysis reveals that this doesn't cost too // much. It scales much much better than having the whole comment // structure. $query->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder'); $query->orderBy('torder', 'ASC'); } + else { + $query->orderBy('c.cid', 'ASC'); + } $query->setCountQuery($count_query); - $cids = $query->execute()->fetchCol(); - - return $cids; + return $query->execute()->fetchCol(); } /** @@ -818,8 +814,6 @@ function comment_prepare_thread(&$comments, $mode) { * * @param \Drupal\comment\Plugin\Core\Entity\comment $comment * The comment object. - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity the comment is attached to. * @param $view_mode * (optional) View mode, e.g. 'full', 'teaser'... Defaults to 'full'. * @param $langcode @@ -1343,10 +1337,10 @@ function comment_num_new($entity_id, $entity_type, $field_name = NULL, $timestam * Count the number of comments which appear before the comment we want to * display, taking into account display settings and threading. * - * @param integer $cid - * The comment ID. - * @param array $instance - * Field instance as returned from field_info_instance. + * @param \Drupal\comment\Plugin\Core\Entity\comment $comment + * The comment object. + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity the comment is attached to. * * @return * The display ordinal for the comment. @@ -1354,23 +1348,25 @@ function comment_num_new($entity_id, $entity_type, $field_name = NULL, $timestam * @see comment_get_display_page() * @see field_info_instance(). */ -function comment_get_display_ordinal($cid, $instance) { +function comment_get_display_ordinal($comment, EntityInterface $entity) { // Count how many comments (c1) are before $cid (c2) in display order. This is // the 0-based display ordinal. $query = db_select('comment', 'c1'); $query->innerJoin('comment', 'c2', 'c2.entity_id = c1.entity_id AND c2.entity_type = c1.entity_type AND c2.field_name = c1.field_name'); $query->addExpression('COUNT(*)', 'count'); - $query->condition('c2.cid', $cid); + $query->condition('c2.cid', $comment->id()); if (!user_access('administer comments')) { $query->condition('c1.status', COMMENT_PUBLISHED); } - $mode = $instance['settings']['comment']['comment_default_mode']; + // Get settings from display of commented entity. + $component = entity_get_display($entity->entityType(), $entity->bundle(), 'default')->getComponent($comment->field_name->value); + $mode = $component['settings']['default_mode']; if ($mode == COMMENT_MODE_FLAT) { // For flat comments, cid is used for ordering comments due to // unpredicatable behavior with timestamp, so we make the same assumption // here. - $query->condition('c1.cid', $cid, '<'); + $query->condition('c1.cid', $comment->id(), '<'); } else { // For threaded comments, the c.thread column is used for ordering. We can @@ -1388,18 +1384,19 @@ function comment_get_display_ordinal($cid, $instance) { * Finds the correct page number for a comment taking into account display * and paging settings. * - * @param $cid - * The comment ID. - * @param array $instance - * Field instance as returned from field_info_instance(). + * @param \Drupal\comment\Plugin\Core\Entity\comment $comment + * The comment object. + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity the comment is attached to. * * @return * The page number. */ -function comment_get_display_page($cid, $instance) { - $ordinal = comment_get_display_ordinal($cid, $instance); - // @todo Get settings from default display. - $comments_per_page = $instance['settings']['comment']['comment_default_per_page']; +function comment_get_display_page($comment, EntityInterface $entity) { + $ordinal = comment_get_display_ordinal($comment, $entity); + // Get settings from default display of the commented entity. + $component = entity_get_display($entity->entityType(), $entity->bundle(), 'default')->getComponent($comment->field_name->value); + $comments_per_page = $component['settings']['per_page']; return floor($ordinal / $comments_per_page); } @@ -1471,7 +1468,7 @@ function comment_preview(Comment $comment) { } $preview_build['comment_output_below'] = $build; - $preview_build['comment_output_below']['#weight'] = 1000; + $preview_build['comment_output_below']['#weight'] = 110; return $preview_build; } @@ -2098,6 +2095,16 @@ function comment_add_default_comment_field($entity_type, $bundle, $field_name = 'weight' => 1, )) ->save(); + // Update teaser for nodes. + if ($entity_type == 'node') { + entity_get_display($entity_type, $bundle, 'teaser') + ->setComponent($field_name, array( + 'label' => 'hidden', + 'type' => 'comment_links', + 'weight' => 10, + )) + ->save(); + } } _comment_body_field_create($entity_type, $bundle, $field_name); } @@ -2110,17 +2117,6 @@ function comment_field_create_instance($instance) { if ($field['type'] == 'comment') { _comment_body_field_create($instance['entity_type'], $instance['bundle'], $instance['field_name']); cache()->delete('comment_entity_info'); - // Add BC for node teaser. - if ($instance['entity_type'] == 'node' && $teaser_display = entity_load('entity_display', $instance['entity_type'] . '.' . $instance['bundle'] . '.teaser')) { - // $bundle_settings = field_bundle_settings($this->entity_type, $this->bundle); - $teaser_display - ->setComponent($instance['field_name'], array( - 'label' => 'hidden', - 'type' => 'comment_links', - 'weight' => 10, - )) - ->save(); - } } } diff --git a/core/modules/comment/lib/Drupal/comment/CommentFormController.php b/core/modules/comment/lib/Drupal/comment/CommentFormController.php index ff996ba..9631fd4 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentFormController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentFormController.php @@ -312,15 +312,13 @@ public function preview(array $form, array &$form_state) { * Overrides Drupal\Core\Entity\EntityFormController::save(). */ public function save(array $form, array &$form_state) { - $entity = entity_load($form_state['values']['entity_type'], $form_state['values']['entity_id']); $comment = $this->getEntity($form_state); - $field_name = $form_state['values']['field_name']; - $instance = field_info_instance($entity->entityType(), $field_name, $entity->bundle()); - $items = field_get_items($entity, $field_name); - $status = reset($items); + $entity = entity_load($comment->entity_type->value, $comment->entity_id->target_id); + $items = field_get_items($entity, $comment->field_name->value); + $status = isset($items[0]['status']) ? $items[0]['status'] : COMMENT_OPEN; $uri = $entity->uri(); - if (user_access('post comments') && (user_access('administer comments') || $status['status'] == COMMENT_OPEN)) { + if (user_access('post comments') && (user_access('administer comments') || $status == COMMENT_OPEN)) { // Save the anonymous user information to a cookie for reuse. if (user_is_anonymous()) { user_cookie_save(array_intersect_key($form_state['values'], array_flip(array('name', 'mail', 'homepage')))); @@ -343,7 +341,7 @@ public function save(array $form, array &$form_state) { } $query = array(); // Find the current display page for this comment. - $page = comment_get_display_page($comment->id(), $instance); + $page = comment_get_display_page($comment, $entity); if ($page > 0) { $query['page'] = $page; } diff --git a/core/modules/comment/lib/Drupal/comment/Plugin/field/formatter/CommentLinksFormatter.php b/core/modules/comment/lib/Drupal/comment/Plugin/field/formatter/CommentLinksFormatter.php index 3c31af8..805b329 100644 --- a/core/modules/comment/lib/Drupal/comment/Plugin/field/formatter/CommentLinksFormatter.php +++ b/core/modules/comment/lib/Drupal/comment/Plugin/field/formatter/CommentLinksFormatter.php @@ -82,20 +82,21 @@ public function settingsSummary() { * Implements FormatterInterface::viewElements(). */ public function viewElements(EntityInterface $entity, $langcode, array $items) { - $settings = $this->settings; $field_name = $this->field['field_name']; // We only ever process first value. $status = isset($items[0]['status']) ? $items[0]['status'] : COMMENT_OPEN;; + $access_comments = user_access('access comments'); + $post_comments = user_access('post comments'); $links = array(); $uri = $entity->uri(); - foreach ($settings as $link => $v) { + foreach ($this->settings as $link => $v) { $link_attributes = array(); switch ($link) { case 'comment': - if (!empty($entity->comment_statistics[$field_name]->comment_count)) { + if ($access_comments && !empty($entity->comment_statistics[$field_name]->comment_count)) { $link_attributes['title'] = format_plural($entity->comment_statistics[$field_name]->comment_count, '1 comment', '@count comments'); $link_attributes['attributes'] = array('title' => t('Jump to the first comment of this posting.')); $link_attributes['fragment'] = 'comments'; @@ -105,7 +106,7 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { break; case 'new': - if (!empty($entity->comment_statistics[$field_name]->comment_count) && $new = comment_num_new($entity->id(), $entity->entityType(), $field_name)) { + if ($access_comments && !empty($entity->comment_statistics[$field_name]->comment_count) && $new = comment_num_new($entity->id(), $entity->entityType(), $field_name)) { $link_attributes['title'] = format_plural($new, '1 new comment', '@count new comments'); $link_attributes['attributes'] = array('title' => t('Jump to the first new comment of this posting.')); $link_attributes['query'] = comment_new_page_count($entity->comment_statistics[$field_name]->comment_count, $new, $entity, $field_name); @@ -117,7 +118,7 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { case 'add': if ($status == COMMENT_OPEN) { - if (user_access('post comments')) { + if ($post_comments) { $link_attributes['title'] = t('Add new comment'); $link_attributes['attributes'] = array('title' => t('Add a new comment to this page.')); $link_attributes['fragment'] = 'comment-form'; diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php index 7ac6ed6..c7cf938 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php @@ -154,7 +154,7 @@ function setEnvironment(array $info) { } // Change comment settings. - $this->setCommentSettings('comment_form_location', $info['form'], 'Set comment form location'); + $this->setFormatterSettings('form_location', $info['form'], 'Set comment form location'); $this->setCommentAnonymous($info['contact']); if ($this->node->comment[LANGUAGE_NOT_SPECIFIED][0]['status'] != $info['comments']) { $this->node->comment[LANGUAGE_NOT_SPECIFIED][0]['status'] = $info['comments'];