diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index 391a255..a239bde 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -31,6 +31,32 @@ 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. + 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(); + } + } + } +} + +/** * Implements hook_schema(). */ function comment_schema() { diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 1ab6691..b49fe82 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -262,26 +262,28 @@ function comment_permission() { * Number of new replies. * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The first new comment entity. + * @param int $per_page + * Number to display per page. + * @param int $mode + * Mode of display, either threaded or flat. * @param string $field_name * The field name on the entity to which comments are attached to. * * @return array|null * An array "page=X" if the page number is greater than zero; NULL otherwise. */ -function comment_new_page_count($num_comments, $new_replies, ContentEntityInterface $entity, $field_name = 'comment') { +function comment_new_page_count($num_comments, $new_replies, ContentEntityInterface $entity, $per_page = 50, $mode = COMMENT_MODE_THREADED, $field_name = 'comment') { $field_definition = $entity->getFieldDefinition($field_name); - $mode = $field_definition->getSetting('default_mode'); - $comments_per_page = $field_definition->getSetting('per_page'); $pagenum = NULL; $flat = $mode == COMMENT_MODE_FLAT ? TRUE : FALSE; - if ($num_comments <= $comments_per_page) { + if ($num_comments <= $per_page) { // Only one page of comments. $pageno = 0; } elseif ($flat) { // Flat comments. $count = $num_comments - $new_replies; - $pageno = $count / $comments_per_page; + $pageno = $count / $per_page; } else { // Threaded comments: we build a query with a subquery to find the first @@ -323,7 +325,7 @@ function comment_new_page_count($num_comments, $new_replies, ContentEntityInterf ':thread' => $first_thread, ))->fetchField(); - $pageno = $count / $comments_per_page; + $pageno = $count / $per_page; } if ($pageno >= 1) { @@ -353,157 +355,17 @@ function comment_entity_build_defaults_alter(array &$build, EntityInterface $ent } /** - * Implements hook_node_links_alter(). + * Implements hook_preprocess_hook() for theme_node(). */ -function comment_node_links_alter(array &$node_links, NodeInterface $node, array &$context) { - // 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 - - $view_mode = $context['view_mode']; - if ($view_mode == 'search_index' || $view_mode == 'search_result' || $view_mode == 'print') { - // Do not add any links if the node displayed for: - // - search indexing. - // - constructing a search result excerpt. - // - print. - return; - } - - $fields = \Drupal::service('comment.manager')->getFields('node'); - foreach ($fields as $field_name => $detail) { - // Skip fields that the node does not have. - if (!$node->hasField($field_name)) { - continue; - } - $links = array(); - $commenting_status = $node->get($field_name)->status; - if ($commenting_status) { - $field_definition = $node->getFieldDefinition($field_name); - // Node have commenting open or close. - if ($view_mode == 'rss') { - // Add a comments RSS element which is a URL to the comments of this node. - $options = array( - 'fragment' => 'comments', - 'absolute' => TRUE, - ); - $node->rss_elements[] = array( - 'key' => 'comments', - 'value' => $node->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 node - // is open to new comments, and there currently are none. - if (user_access('access comments')) { - if (!empty($node->get($field_name)->comment_count)) { - $links['comment-comments'] = array( - 'title' => format_plural($node->get($field_name)->comment_count, '1 comment', '@count comments'), - 'attributes' => array('title' => t('Jump to the first comment of this posting.')), - 'fragment' => 'comments', - 'html' => TRUE, - ) + $node->urlInfo()->toArray(); - if (\Drupal::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' => $node->get($field_name)->last_comment_timestamp, - 'data-history-node-field-name' => $field_name, - ), - 'html' => TRUE, - ); - } - } - } - // Provide a link to new comment form. - if ($commenting_status == CommentItemInterface::OPEN) { - $comment_form_location = $field_definition->getSetting('form_location'); - if (user_access('post comments')) { - $links['comment-add'] = array( - 'title' => t('Add new comment'), - 'language' => $node->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' => $node->getEntityTypeId(), - 'entity_id' => $node->id(), - 'field_name' => $field_name, - ); - } - else { - $links['comment-add'] += $node->urlInfo()->toArray(); - } - } - elseif (\Drupal::currentUser()->isAnonymous()) { - $links['comment-forbidden'] = array( - 'title' => \Drupal::service('comment.manager')->forbiddenMessage($node, $field_name), - 'html' => TRUE, - ); - } - } - } - else { - // 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 = $field_definition->getSetting('form_location'); - if (user_access('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($node->get($field_name)->comment_count) && user_access('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' => $node->getEntityTypeId(), - 'entity_id' => $node->id(), - 'field_name' => $field_name, - ); - } - else { - $links['comment-add'] += $node->urlInfo()->toArray(); - } - } - } - elseif (\Drupal::currentUser()->isAnonymous()) { - $links['comment-forbidden'] = array( - 'title' => \Drupal::service('comment.manager')->forbiddenMessage($node, $field_name), - 'html' => TRUE, - ); - } - } - } - } - - if (!empty($links)) { - $node_links['comment__' . $field_name] = array( - '#theme' => 'links__entity__comment__' . $field_name, - '#links' => $links, - '#attributes' => array('class' => array('links', 'inline')), - ); - if ($view_mode == 'teaser' && \Drupal::moduleHandler()->moduleExists('history') && \Drupal::currentUser()->isAuthenticated()) { - $node_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 node. - $node_links['comment__' . $field_name]['#post_render_cache']['history_attach_timestamp'] = array( - array('node_id' => $node->id()), - ); - $node_links['comment__' . $field_name]['#post_render_cache']['Drupal\comment\CommentViewBuilder::attachNewCommentsLinkMetadata'] = array( - array('entity_type' => $node->getEntityTypeId(), 'entity_id' => $node->id(), 'field_name' => $field_name), - ); - } +function comment_preprocess_node(&$variables) { + /** @var \Drupal\node\NodeInterface $node */ + $node = $variables['node']; + // The comment default formatter puts the links with the field output, we move + // it back to the 'links' area for consistency with earlier releases. + foreach (\Drupal::service('comment.manager')->getFields('node') as $field_name => $details) { + if ($node->hasField($field_name) && !empty($variables['content'][$field_name][0]['links'])) { + $variables['content']['links'] += $variables['content'][$field_name][0]['links']; + unset($variables['content'][$field_name][0]['links']); } } } diff --git a/core/modules/comment/js/node-new-comments-link.js b/core/modules/comment/js/node-new-comments-link.js index 490f25f..543eaf8 100644 --- a/core/modules/comment/js/node-new-comments-link.js +++ b/core/modules/comment/js/node-new-comments-link.js @@ -40,9 +40,12 @@ return; } + 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'); + // Perform an AJAX request to retrieve node read timestamps. Drupal.history.fetchTimestamps(nodeIDs, function () { - processNodeNewCommentLinks($placeholders); + processNodeNewCommentLinks($placeholders, perPage, defaultMode); }); } }; @@ -71,7 +74,7 @@ .end().show(); } - function processNodeNewCommentLinks($placeholders) { + function processNodeNewCommentLinks($placeholders, perPage, defaultMode) { // Figure out which placeholders need the "x new comments" links. var $placeholdersToUpdate = {}; var fieldName = 'comment'; @@ -120,7 +123,7 @@ $.ajax({ url: Drupal.url('comments/render_new_comments_node_links'), type: 'POST', - data: { 'node_ids[]': nodeIDs, 'field_name': fieldName }, + data: { 'node_ids[]': nodeIDs, 'field_name': fieldName, 'per_page': perPage, 'default_mode': defaultMode }, dataType: 'json', success: render }); diff --git a/core/modules/comment/src/CommentManager.php b/core/modules/comment/src/CommentManager.php index 2b639a2..fdbbf9c 100644 --- a/core/modules/comment/src/CommentManager.php +++ b/core/modules/comment/src/CommentManager.php @@ -7,6 +7,7 @@ namespace Drupal\comment; +use Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\Component\Utility\String; use Drupal\Component\Utility\Unicode; @@ -183,6 +184,12 @@ public function addDefaultField($entity_type, $bundle, $field_name = 'comment', 'label' => 'hidden', 'type' => 'comment_default', 'weight' => 20, + 'settings' => array( + 'per_page' => 50, + 'default_mode' => COMMENT_MODE_THREADED, + 'form_location' => COMMENT_FORM_BELOW, + 'show_links' => CommentDefaultFormatter::LINKS_PAGE, + ) )) ->save(); // The comment field should be hidden in all other view displays. diff --git a/core/modules/comment/src/CommentViewBuilder.php b/core/modules/comment/src/CommentViewBuilder.php index b7cba52..38ab21f 100644 --- a/core/modules/comment/src/CommentViewBuilder.php +++ b/core/modules/comment/src/CommentViewBuilder.php @@ -336,7 +336,7 @@ public static function attachNewCommentsLinkMetadata(array $element, array $cont ->getStorage($context['entity_type']) ->load($context['entity_id']); $field_name = $context['field_name']; - $query = comment_new_page_count($entity->{$field_name}->comment_count, $new, $entity); + $query = comment_new_page_count($entity->{$field_name}->comment_count, $new, $entity, $context['per_page'], $context['default_mode']); // Attach metadata. $element['#attached']['js'][] = array( diff --git a/core/modules/comment/src/Controller/CommentController.php b/core/modules/comment/src/Controller/CommentController.php index 94fb25e..d9c5e55 100644 --- a/core/modules/comment/src/Controller/CommentController.php +++ b/core/modules/comment/src/Controller/CommentController.php @@ -12,6 +12,7 @@ use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Entity\EntityInterface; +use Drupal\node\Entity\Node; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -280,17 +281,19 @@ public function renderNewCommentsNodeLinks(Request $request) { $nids = $request->request->get('node_ids'); $field_name = $request->request->get('field_name'); - if (!isset($nids)) { + $per_page = $request->request->get('per_page'); + $default_mode = $request->request->get('default_mode'); + if (!isset($nids) || !in_array($default_mode, array(COMMENT_MODE_THREADED, COMMENT_MODE_FLAT))) { throw new NotFoundHttpException(); } // Only handle up to 100 nodes. $nids = array_slice($nids, 0, 100); $links = array(); - foreach ($nids as $nid) { - $node = node_load($nid); + $nodes = Node::loadMultiple($nids); + foreach ($nodes as $nid => $node) { $new = comment_num_new($node->id(), 'node'); - $query = comment_new_page_count($node->{$field_name}->comment_count, $new, $node); + $query = comment_new_page_count($node->{$field_name}->comment_count, $new, $node, $per_page, $default_mode); $links[$nid] = array( 'new_comment_count' => (int) $new, 'first_new_comment_link' => $this->urlGenerator()->generateFromPath('node/' . $node->id(), array('query' => $query, 'fragment' => 'new')), diff --git a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php index 77681c9..5aa5391 100644 --- a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php +++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php @@ -7,9 +7,12 @@ namespace Drupal\comment\Plugin\Field\FieldFormatter; +use Drupal\comment\CommentManagerInterface; use Drupal\comment\CommentStorageInterface; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityViewBuilderInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Field\FieldDefinitionInterface; @@ -35,11 +38,35 @@ 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' => COMMENT_MODE_THREADED, + 'per_page' => 50, + 'form_location' => COMMENT_FORM_BELOW, ) + parent::defaultSettings(); } @@ -65,6 +92,20 @@ public static function defaultSettings() { protected $viewBuilder; /** + * 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) { @@ -78,7 +119,9 @@ public static function create(ContainerInterface $container, array $configuratio $configuration['third_party_settings'], $container->get('current_user'), $container->get('entity.manager')->getStorage('comment'), - $container->get('entity.manager')->getViewBuilder('comment') + $container->get('entity.manager')->getViewBuilder('comment'), + $container->get('module_handler'), + $container->get('comment.manager') ); } @@ -105,12 +148,18 @@ public static function create(ContainerInterface $container, array $configuratio * The comment storage. * @param \Drupal\Core\Entity\EntityViewBuilderInterface $comment_view_builder * The comment view 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, CommentStorageInterface $comment_storage, EntityViewBuilderInterface $comment_view_builder) { + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, CommentStorageInterface $comment_storage, EntityViewBuilderInterface $comment_view_builder, ModuleHandlerInterface $module_handler, CommentManagerInterface $comment_manager) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); $this->viewBuilder = $comment_view_builder; $this->storage = $comment_storage; $this->currentUser = $current_user; + $this->moduleHandler = $module_handler; + $this->commentManager = $comment_manager; } /** @@ -130,7 +179,6 @@ public function viewElements(FieldItemListInterface $items) { // comment_node_update_index() instead of by this formatter, so don't // return anything if the view mode is search_index or search_result. !in_array($this->viewMode, array('search_result', 'search_index'))) { - $comment_settings = $this->getFieldSettings(); // Only attempt to render comments if the entity has visible comments. // Unpublished comments are not included in @@ -138,9 +186,9 @@ public function viewElements(FieldItemListInterface $items) { // 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'))) { - $mode = $comment_settings['default_mode']; - $comments_per_page = $comment_settings['per_page']; - if ($cids = comment_get_thread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id'))) { + // @todo Use $this->storage once https://www.drupal.org/node/2156089 is + // in. + if ($cids = comment_get_thread($entity, $field_name, $this->getSetting('default_mode'), $this->getSetting('per_page'), $this->getSetting('pager_id'))) { $comments = $this->storage->loadMultiple($cids); comment_prepare_thread($comments); $build = $this->viewBuilder->viewMultiple($comments); @@ -168,7 +216,7 @@ public function viewElements(FieldItemListInterface $items) { // Append comment form if the comments are open and the form is set to // display below the entity. Do not show the form for the print view mode. - if ($status == CommentItemInterface::OPEN && $comment_settings['form_location'] == COMMENT_FORM_BELOW && $this->viewMode != 'print') { + if ($status == CommentItemInterface::OPEN && $this->getSetting('form_location') == COMMENT_FORM_BELOW && $this->viewMode != 'print') { // Only show the add comment form if the user has permission. if ($this->currentUser->hasPermission('post comments')) { // All users in the "anonymous" role can use the same form: it is fine @@ -201,9 +249,10 @@ public function viewElements(FieldItemListInterface $items) { $elements[] = $output + array( '#theme' => 'comment_wrapper__' . $entity->getEntityTypeId() . '__' . $entity->bundle() . '__' . $field_name, '#entity' => $entity, - '#display_mode' => $this->getFieldSetting('default_mode'), + '#display_mode' => $this->getSetting('default_mode'), 'comments' => array(), 'comment_form' => array(), + 'links' => $this->buildLinks($items), ); } @@ -215,6 +264,38 @@ public function viewElements(FieldItemListInterface $items) { */ public function settingsForm(array $form, array &$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'), + '#default_value' => $this->getSetting('per_page'), + '#required' => TRUE, + '#min' => 10, + '#max' => 1000, + '#step' => 10, + ); + $element['form_location'] = array( + '#type' => 'checkbox', + '#title' => t('Show reply form on the same page as comments'), + '#default_value' => $this->getSetting('form_location'), + ); $element['pager_id'] = array( '#type' => 'select', '#title' => $this->t('Pager ID'), @@ -229,13 +310,178 @@ public function settingsForm(array $form, array &$form_state) { * {@inheritdoc} */ public function settingsSummary() { - // Only show a summary if we're using a non-standard pager id. + $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')) { - return array($this->t('Pager ID: @id', array( - '@id' => $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)) { + $links['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()) { + $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 + // node. + $links['comment__' . $field_name]['#post_render_cache']['history_attach_timestamp'] = array( + array('node_id' => $entity->id()), + ); + $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, + 'per_page' => $field_definition->getSetting('per_page'), + 'default_mode' => $field_definition->getSetting('default_mode'), + ), + ); + } + return $links; + } } - return array(); } } diff --git a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentLinksFormatter.php b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentLinksFormatter.php new file mode 100644 index 0000000..6ae8e58 --- /dev/null +++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentLinksFormatter.php @@ -0,0 +1,82 @@ +getEntity(); + $field_name = $this->fieldDefinition->getName(); + $elements = array( + '#theme' => 'comment_wrapper__' . $entity->getEntityTypeId() . '__' . $entity->bundle() . '__' . $field_name, + '#entity' => $entity, + '#display_mode' => $this->getSetting('default_mode'), + 'comments' => array(), + 'comment_form' => array(), + 'links' => $this->buildLinks($items), + ); + + return $elements; + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, array &$form_state) { + $elements = parent::settingsForm($form, $form_state); + // Remove the 'no links' option. + unset($elements['show_links'][static::LINKS_NONE]); + return $elements; + } + + /** + * {@inheritdoc} + */ + public function settingsSummary() { + $links_map = array( + static::LINKS_RSS => $this->t('RSS style links'), + static::LINKS_PAGE => $this->t('full-page style links'), + static::LINKS_TEASER => $this->t('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 @links to compliment @per_page @mode comments with @form, using pager ID @id', $variables)); + } + return array($this->t('Showing @links to compliment @per_page @mode comments with @form', $variables)); + } + +} diff --git a/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php b/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php index 7fb70aa..9b78660 100644 --- a/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php +++ b/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php @@ -40,9 +40,6 @@ public static function defaultSettings() { */ public static function defaultInstanceSettings() { return array( - 'default_mode' => COMMENT_MODE_THREADED, - 'per_page' => 50, - 'form_location' => COMMENT_FORM_BELOW, 'anonymous' => COMMENT_ANONYMOUS_MAYNOT_CONTACT, 'subject' => 1, 'preview' => DRUPAL_OPTIONAL, @@ -116,21 +113,6 @@ public function instanceSettingsForm(array $form, array &$form_state) { 'library' => array('comment/drupal.comment'), ), ); - $element['comment']['default_mode'] = array( - '#type' => 'checkbox', - '#title' => t('Threading'), - '#default_value' => $settings['default_mode'], - '#description' => t('Show comment replies in a threaded list.'), - ); - $element['comment']['per_page'] = array( - '#type' => 'number', - '#title' => t('Comments per page'), - '#default_value' => $settings['per_page'], - '#required' => TRUE, - '#min' => 10, - '#max' => 1000, - '#step' => 10, - ); $element['comment']['anonymous'] = array( '#type' => 'select', '#title' => t('Anonymous commenting'), @@ -147,11 +129,6 @@ public function instanceSettingsForm(array $form, array &$form_state) { '#title' => t('Allow comment title'), '#default_value' => $settings['subject'], ); - $element['comment']['form_location'] = array( - '#type' => 'checkbox', - '#title' => t('Show reply form on the same page as comments'), - '#default_value' => $settings['form_location'], - ); $element['comment']['preview'] = array( '#type' => 'radios', '#title' => t('Preview comment'), diff --git a/core/modules/comment/src/Plugin/views/field/NodeNewComments.php b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php index 5ccf4e2..8a5d9c2 100644 --- a/core/modules/comment/src/Plugin/views/field/NodeNewComments.php +++ b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php @@ -89,6 +89,21 @@ public function buildOptionsForm(&$form, &$form_state) { '#type' => 'checkbox', '#default_value' => $this->options['link_to_comment'], ); + $form['default_mode'] = array( + '#type' => 'checkbox', + '#title' => t('Threading'), + '#default_value' => $this->options['default_mode'], + '#description' => t('Comment replies use a threaded list.'), + ); + $element['per_page'] = array( + '#type' => 'number', + '#title' => t('Comments per page'), + '#default_value' => $this->options['per_page'], + '#required' => TRUE, + '#min' => 10, + '#max' => 1000, + '#step' => 10, + ); parent::buildOptionsForm($form, $form_state); } @@ -153,7 +168,7 @@ protected function renderLink($data, ResultRow $values) { )); $this->options['alter']['make_link'] = TRUE; $this->options['alter']['path'] = 'node/' . $node->id(); - $this->options['alter']['query'] = comment_new_page_count($this->getValue($values, 'comment_count'), $this->getValue($values), $node); + $this->options['alter']['query'] = comment_new_page_count($this->getValue($values, 'comment_count'), $this->getValue($values), $node, $this->options['per_page'], $this->options['default_mode']); $this->options['alter']['fragment'] = 'new'; } diff --git a/core/modules/comment/src/Tests/CommentInterfaceTest.php b/core/modules/comment/src/Tests/CommentInterfaceTest.php index 42157e1..0954cbd 100644 --- a/core/modules/comment/src/Tests/CommentInterfaceTest.php +++ b/core/modules/comment/src/Tests/CommentInterfaceTest.php @@ -32,7 +32,7 @@ function testCommentInterface() { $this->setCommentPreview(DRUPAL_DISABLED); $this->setCommentForm(TRUE); $this->setCommentSubject(FALSE); - $this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.'); $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 bce2746..a32faa3 100644 --- a/core/modules/comment/src/Tests/CommentLinksTest.php +++ b/core/modules/comment/src/Tests/CommentLinksTest.php @@ -7,9 +7,11 @@ namespace Drupal\comment\Tests; +use Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\comment\CommentInterface; +use Drupal\simpletest\WebTestBase; /** * Tests comment links based on environment configurations. @@ -34,6 +36,28 @@ public static function getInfo() { } /** + * {@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' => COMMENT_MODE_THREADED, + 'form_location' => COMMENT_FORM_BELOW, + 'show_links' => CommentDefaultFormatter::LINKS_TEASER, + ), + )) + ->save(); + } + + + /** * Tests comment links. * * The output of comment links depends on various environment conditions: @@ -166,7 +190,7 @@ function setEnvironment(array $info) { } // Change comment settings. - $this->setCommentSettings('form_location', $info['form'], 'Set comment form location'); + $this->setCommentFormatterSettings('form_location', $info['form'], 'Set comment form location'); $this->setCommentAnonymous($info['contact']); if ($this->node->comment->status != $info['comments']) { $this->node->comment = $info['comments']; diff --git a/core/modules/comment/src/Tests/CommentNewIndicatorTest.php b/core/modules/comment/src/Tests/CommentNewIndicatorTest.php index f955b62..902feeb 100644 --- a/core/modules/comment/src/Tests/CommentNewIndicatorTest.php +++ b/core/modules/comment/src/Tests/CommentNewIndicatorTest.php @@ -115,6 +115,8 @@ public function testCommentNewCommentsIndicator() { // perform an HTTP request to render the "new comments" node link. $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.'); $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 a3eba40..4782539 100644 --- a/core/modules/comment/src/Tests/CommentNodeAccessTest.php +++ b/core/modules/comment/src/Tests/CommentNodeAccessTest.php @@ -60,7 +60,7 @@ function testThreadedCommentView() { $this->setCommentPreview(DRUPAL_DISABLED); $this->setCommentForm(TRUE); $this->setCommentSubject(TRUE); - $this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', COMMENT_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 f712ce0..05d55d1 100644 --- a/core/modules/comment/src/Tests/CommentPagerTest.php +++ b/core/modules/comment/src/Tests/CommentPagerTest.php @@ -37,7 +37,7 @@ function testCommentPaging() { $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE); $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE); - $this->setCommentSettings('default_mode', COMMENT_MODE_FLAT, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', COMMENT_MODE_FLAT, 'Comment paging changed.'); // Set comments to one per page so that we are able to test paging without // needing to insert large numbers of comments. @@ -77,7 +77,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->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Switched to threaded mode.'); + $this->setCommentFormatterSettings('default_mode', COMMENT_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.'); @@ -137,7 +137,7 @@ function testCommentOrderingThreading() { // - 2 // - 5 - $this->setCommentSettings('default_mode', COMMENT_MODE_FLAT, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', COMMENT_MODE_FLAT, 'Comment paging changed.'); $expected_order = array( 0, @@ -151,7 +151,7 @@ function testCommentOrderingThreading() { $this->drupalGet('node/' . $node->id()); $this->assertCommentOrder($comments, $expected_order); - $this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Switched to threaded mode.'); + $this->setCommentFormatterSettings('default_mode', COMMENT_MODE_THREADED, 'Switched to threaded mode.'); $expected_order = array( 0, @@ -232,7 +232,7 @@ function testCommentNewPageIndicator() { // - 2 // - 5 - $this->setCommentSettings('default_mode', COMMENT_MODE_FLAT, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', COMMENT_MODE_FLAT, 'Comment paging changed.'); $expected_pages = array( 1 => 5, // Page of comment 5 @@ -245,12 +245,12 @@ function testCommentNewPageIndicator() { $node = node_load($node->id()); foreach ($expected_pages as $new_replies => $expected_page) { - $returned = comment_new_page_count($node->get('comment')->comment_count, $new_replies, $node); + $returned = comment_new_page_count($node->get('comment')->comment_count, $new_replies, $node, 1, COMMENT_MODE_FLAT); $returned_page = is_array($returned) ? $returned['page'] : 0; $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->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Switched to threaded mode.'); + $this->setCommentFormatterSettings('default_mode', COMMENT_MODE_THREADED, 'Switched to threaded mode.'); $expected_pages = array( 1 => 5, // Page of comment 5 @@ -264,7 +264,7 @@ function testCommentNewPageIndicator() { \Drupal::entityManager()->getStorage('node')->resetCache(array($node->id())); $node = node_load($node->id()); foreach ($expected_pages as $new_replies => $expected_page) { - $returned = comment_new_page_count($node->get('comment')->comment_count, $new_replies, $node); + $returned = comment_new_page_count($node->get('comment')->comment_count, $new_replies, $node, 1, COMMENT_MODE_THREADED); $returned_page = is_array($returned) ? $returned['page'] : 0; $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))); } @@ -313,7 +313,7 @@ function testTwoPagers() { $this->setCommentForm(TRUE, $field_name); $this->setCommentSubject(TRUE, $field_name); $this->setCommentPreview(DRUPAL_OPTIONAL, $field_name); - $this->setCommentSettings('default_mode', COMMENT_MODE_FLAT, 'Comment paging changed.', $field_name); + $this->setCommentFormatterSettings('default_mode', COMMENT_MODE_FLAT, 'Comment paging changed.', $field_name); // 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 1de62da..aa7a4fb 100644 --- a/core/modules/comment/src/Tests/CommentPreviewTest.php +++ b/core/modules/comment/src/Tests/CommentPreviewTest.php @@ -40,7 +40,7 @@ function testCommentPreview() { $this->setCommentPreview(DRUPAL_OPTIONAL); $this->setCommentForm(TRUE); $this->setCommentSubject(TRUE); - $this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.'); $this->drupalLogout(); // Login as web user and add a signature and a user picture. @@ -83,7 +83,7 @@ function testCommentEditPreviewSave() { $this->setCommentPreview(DRUPAL_OPTIONAL); $this->setCommentForm(TRUE); $this->setCommentSubject(TRUE); - $this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.'); $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 6906b55..660459d 100644 --- a/core/modules/comment/src/Tests/CommentStatisticsTest.php +++ b/core/modules/comment/src/Tests/CommentStatisticsTest.php @@ -44,7 +44,7 @@ function testCommentNodeCommentStatistics() { $this->setCommentPreview(DRUPAL_DISABLED); $this->setCommentForm(TRUE); $this->setCommentSubject(FALSE); - $this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.'); $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 a93110b..6557524 100644 --- a/core/modules/comment/src/Tests/CommentTestBase.php +++ b/core/modules/comment/src/Tests/CommentTestBase.php @@ -79,6 +79,20 @@ function setUp() { // Create comment field on article. $this->container->get('comment.manager')->addDefaultField('node', 'article'); + // Set some display options for search results and print pages. + if (module_exists('book')) { + $display = entity_get_display('node', 'article', 'print'); + $display->removeComponent('comment'); + $display->save(); + } + if (module_exists('search')) { + foreach (array('search_result', 'search_index') as $view_mode) { + $display = entity_get_display('node', 'article', $view_mode); + $display->removeComponent('comment'); + $display->save(); + } + } + // Create a test node authored by the web user. $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->web_user->id())); } @@ -254,7 +268,7 @@ public function setCommentPreview($mode, $field_name = 'comment') { * Defaults to 'comment'. */ public function setCommentForm($enabled, $field_name = 'comment') { - $this->setCommentSettings('form_location', ($enabled ? COMMENT_FORM_BELOW : COMMENT_FORM_SEPARATE_PAGE), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.', $field_name); + $this->setCommentFormatterSettings('form_location', ($enabled ? COMMENT_FORM_BELOW : COMMENT_FORM_SEPARATE_PAGE), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.', $field_name); } /** @@ -280,7 +294,7 @@ function setCommentAnonymous($level) { * Defaults to 'comment'. */ public function setCommentsPerPage($number, $field_name = 'comment') { - $this->setCommentSettings('per_page', $number, format_string('Number of comments per page set to @number.', array('@number' => $number)), $field_name); + $this->setCommentFormatterSettings('per_page', $number, format_string('Number of comments per page set to @number.', array('@number' => $number)), $field_name); } /** @@ -305,6 +319,29 @@ public function setCommentSettings($name, $value, $message, $field_name = 'comme } /** + * Sets a comment settings variable for the article content type. + * + * @param string $name + * Name of variable. + * @param string $value + * Value of variable. + * @param string $message + * Status message to display. + * @param string $field_name + * (optional) Field name through which the comment should be posted. + * Defaults to 'comment'. + */ + public function setCommentFormatterSettings($name, $value, $message, $field_name = 'comment') { + $display = entity_get_display('node', 'article', 'full'); + $component = $display->getComponent($field_name); + $component['settings'][$name] = $value; + $display->setComponent($field_name, $component); + $display->save(); + // Display status message. + $this->pass($message); + } + + /** * Checks whether the commenter's contact information is displayed. * * @return boolean diff --git a/core/modules/comment/src/Tests/CommentThreadingTest.php b/core/modules/comment/src/Tests/CommentThreadingTest.php index f0d7703..ce7c309 100644 --- a/core/modules/comment/src/Tests/CommentThreadingTest.php +++ b/core/modules/comment/src/Tests/CommentThreadingTest.php @@ -28,7 +28,7 @@ function testCommentThreading() { $this->setCommentPreview(DRUPAL_DISABLED); $this->setCommentForm(TRUE); $this->setCommentSubject(TRUE); - $this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.'); $this->drupalLogout(); // Create a node. diff --git a/core/modules/forum/forum.module b/core/modules/forum/forum.module index af041fc..202badf 100644 --- a/core/modules/forum/forum.module +++ b/core/modules/forum/forum.module @@ -526,6 +526,8 @@ function forum_theme_suggestions_forums(array $variables) { */ function template_preprocess_forums(&$variables) { $variables['tid'] = $variables['term']->id(); + $display = entity_get_display('node', 'forum', 'full'); + $settings = $display->getComponent('comment_forum')['settings']; if ($variables['forums_defined'] = count($variables['forums']) || count($variables['parents'])) { if (!empty($variables['forums'])) { $variables['forums'] = array( @@ -592,7 +594,7 @@ function template_preprocess_forums(&$variables) { if ($topic->new_replies) { $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' => comment_new_page_count($topic->comment_count, $topic->new_replies, $topic, 'comment_node_forum'), 'fragment' => 'new')); + $variables['topics'][$id]->new_url = url('node/' . $topic->id(), array('query' => comment_new_page_count($topic->comment_count, $topic->new_replies, $topic, $settings['per_page'], $settings['default_mode'], 'comment_node_forum'), 'fragment' => 'new')); } // Build table rows from topics. diff --git a/core/profiles/standard/standard.install b/core/profiles/standard/standard.install index b9d1c02..60b6c1d 100644 --- a/core/profiles/standard/standard.install +++ b/core/profiles/standard/standard.install @@ -4,6 +4,7 @@ * Install, update and uninstall functions for the standard installation profile. */ +use Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; /** @@ -19,6 +20,26 @@ function standard_install() { // Add comment field to article node type. \Drupal::service('comment.manager')->addDefaultField('node', 'article', 'comment', CommentItemInterface::OPEN); + // 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(); // Allow visitor account creation with administrative approval. $user_settings = \Drupal::config('user.settings');