diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index 64aa6bd..0df2dcc 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -32,6 +32,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 dbe3bee..ccf0c94 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -244,7 +244,7 @@ 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 diff --git a/core/modules/comment/js/node-new-comments-link.js b/core/modules/comment/js/node-new-comments-link.js index e1e10b2..6324fda 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'; @@ -121,7 +124,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/CommentForm.php b/core/modules/comment/src/CommentForm.php index 2c99bf8..9894312 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -383,8 +383,9 @@ public function save(array $form, FormStateInterface $form_state) { } $query = array(); // Find the current display page for this comment. - $field_definition = $this->entityManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$field_name]; - $page = $this->entityManager->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page')); + $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']); if ($page > 0) { $query['page'] = $page; } diff --git a/core/modules/comment/src/CommentManager.php b/core/modules/comment/src/CommentManager.php index 54dc128..5296dea 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; @@ -198,6 +199,12 @@ public function addDefaultField($entity_type, $bundle, $field_name = 'comment', 'label' => 'above', 'type' => 'comment_default', 'weight' => 20, + 'settings' => array( + 'per_page' => 50, + 'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED, + 'form_location' => CommentItemInterface::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/CommentStorage.php b/core/modules/comment/src/CommentStorage.php index 714b4c1..0dbe492 100644 --- a/core/modules/comment/src/CommentStorage.php +++ b/core/modules/comment/src/CommentStorage.php @@ -131,15 +131,13 @@ public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $div /** * {@inheritdoc} */ - public function getNewCommentPageNumber($total_comments, $new_comments, ContentEntityInterface $entity, $field_name = 'comment') { - $instance = $entity->getFieldDefinition($field_name); - $comments_per_page = $instance->getSetting('per_page'); + public function getNewCommentPageNumber($total_comments, $new_comments, ContentEntityInterface $entity, $mode = CommentManagerInterface::COMMENT_MODE_THREADED, $comments_per_page = 50, $field_name = 'comment') { if ($total_comments <= $comments_per_page) { // Only one page of comments. $count = 0; } - elseif ($instance->getSetting('default_mode') == CommentManagerInterface::COMMENT_MODE_FLAT) { + elseif ($mode == CommentManagerInterface::COMMENT_MODE_FLAT) { // Flat comments. $count = $total_comments - $new_comments; } diff --git a/core/modules/comment/src/CommentStorageInterface.php b/core/modules/comment/src/CommentStorageInterface.php index d3f15a9..21adaf0 100644 --- a/core/modules/comment/src/CommentStorageInterface.php +++ b/core/modules/comment/src/CommentStorageInterface.php @@ -48,13 +48,18 @@ public function getMaxThreadPerThread(CommentInterface $comment); * The number of new comments that the entity has. * @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). + * @param int $comments_per_page + * (optional) The amount of comments to display per page. Defaults to 50. * @param string $field_name * The field name on the entity to which comments are attached. * * @return array|null * The page number where first new comment appears. (First page returns 0.) */ - public function getNewCommentPageNumber($total_comments, $new_comments, ContentEntityInterface $entity, $field_name = 'comment'); + public function getNewCommentPageNumber($total_comments, $new_comments, ContentEntityInterface $entity, $mode = CommentManagerInterface::COMMENT_MODE_THREADED, $comments_per_page = 50, $field_name = 'comment'); /** * Gets the display ordinal or page number for a comment. diff --git a/core/modules/comment/src/CommentViewBuilder.php b/core/modules/comment/src/CommentViewBuilder.php index cd36bc4..bb49582 100644 --- a/core/modules/comment/src/CommentViewBuilder.php +++ b/core/modules/comment/src/CommentViewBuilder.php @@ -338,7 +338,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); + ->getNewCommentPageNumber($entity->{$field_name}->comment_count, $new, $entity, $context['default_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 af415d0..2ce45eb 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; @@ -111,10 +112,10 @@ public function commentPermalink(Request $request, CommentInterface $comment) { if (!$entity->access('view')) { throw new AccessDeniedHttpException(); } - $field_definition = $this->entityManager()->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()]; - + $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, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page')); + $page = $this->entityManager->getStorage('comment')->getDisplayOrdinal($comment, $display_settings['settings']['default_mode'], $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); @@ -277,18 +278,20 @@ 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(CommentManagerInterface::COMMENT_MODE_THREADED, CommentManagerInterface::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 = $this->commentManager->getCountNewComments($node); $page_number = $this->entityManager()->getStorage('comment') - ->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node); + ->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node, $default_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/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php index aa41d12..3346cc2 100644 --- a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php +++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php @@ -12,12 +12,13 @@ use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\Core\Entity\EntityViewBuilderInterface; use Drupal\Core\Entity\EntityFormBuilderInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Field\FieldItemListInterface; -use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Session\AccountInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FormatterBase; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Session\AccountInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -38,11 +39,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' => CommentManagerInterface::COMMENT_MODE_THREADED, + 'per_page' => 50, + 'form_location' => CommentItemInterface::FORM_BELOW, ) + parent::defaultSettings(); } @@ -75,6 +100,20 @@ public static function defaultSettings() { protected $entityFormBuilder; /** + * The module handler service. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** + * The comment manager service. + * + * @var \Drupal\comment\CommentManagerInterface + */ + protected $commentManager; + + /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { @@ -89,7 +128,9 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('current_user'), $container->get('entity.manager')->getStorage('comment'), $container->get('entity.manager')->getViewBuilder('comment'), - $container->get('entity.form_builder') + $container->get('entity.form_builder'), + $container->get('module_handler'), + $container->get('comment.manager') ); } @@ -118,13 +159,19 @@ public static function create(ContainerInterface $container, array $configuratio * The comment view builder. * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder * The entity form builder. + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler service. + * @param \Drupal\comment\CommentManagerInterface $comment_manager + * The comment manager service. */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, CommentStorageInterface $comment_storage, EntityViewBuilderInterface $comment_view_builder, EntityFormBuilderInterface $entity_form_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, EntityFormBuilderInterface $entity_form_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->entityFormBuilder = $entity_form_builder; + $this->moduleHandler = $module_handler; + $this->commentManager = $comment_manager; } /** @@ -144,7 +191,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 @@ -152,9 +198,7 @@ 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']; - $comments = $this->storage->loadThread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id')); + $comments = $this->storage->loadThread($entity, $field_name, $this->getSetting('default_mode'), $this->getSetting('per_page'), $this->getSetting('pager_id')); if ($comments) { comment_prepare_thread($comments); $build = $this->viewBuilder->viewMultiple($comments); @@ -168,7 +212,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'] == CommentItemInterface::FORM_BELOW && $this->viewMode != 'print') { + if ($status == CommentItemInterface::OPEN && $this->getSetting('form_location') == CommentItemInterface::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 @@ -207,9 +251,10 @@ public function viewElements(FieldItemListInterface $items) { $elements[] = $output + array( '#comment_type' => $this->getFieldSetting('comment_type'), - '#comment_display_mode' => $this->getFieldSetting('default_mode'), + '#comment_display_mode' => $this->getSetting('default_mode'), 'comments' => array(), 'comment_form' => array(), + 'links' => $this->buildLinks($items), ); } @@ -221,6 +266,38 @@ public function viewElements(FieldItemListInterface $items) { */ public function settingsForm(array $form, FormStateInterface $form_state) { $element = array(); + $element['default_mode'] = array( + '#type' => 'checkbox', + '#title' => t('Threading'), + '#default_value' => $this->getSetting('default_mode'), + '#description' => t('Show comment replies in a threaded list.'), + ); + $element['show_links'] = array( + '#type' => 'select', + '#title' => t('Show links'), + '#default_value' => $this->getSetting('show_links'), + '#description' => t('Show comment links.'), + '#options' => array( + static::LINKS_NONE => $this->t('None'), + static::LINKS_PAGE => $this->t('Default links'), + static::LINKS_RSS => $this->t('RSS links'), + static::LINKS_TEASER => $this->t('Teaser links'), + ), + ); + $element['per_page'] = array( + '#type' => 'number', + '#title' => t('Comments per page'), + '#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'), @@ -235,13 +312,180 @@ public function settingsForm(array $form, FormStateInterface $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)) { + $build['comment__' . $field_name] = array( + '#theme' => 'links__entity__comment__' . $field_name, + '#links' => $links, + '#attributes' => array('class' => array('links', 'inline')), + ); + if ($link_style == static::LINKS_TEASER && $this->moduleHandler->moduleExists('history') && $this->currentUser->isAuthenticated()) { + if ($entity->getEntityTypeId() == 'node') { + $build['comment__' . $field_name]['#attached']['library'][] = 'comment/drupal.node-new-comments-link'; + } + + // Embed the metadata for the "X new comments" link (if any) on this + // node. + $build['comment__' . $field_name]['#post_render_cache']['history_attach_timestamp'] = array( + array($entity->getEntityTypeId() . '_id' => $entity->id()), + ); + $build['comment__' . $field_name]['#post_render_cache']['Drupal\comment\CommentViewBuilder::attachNewCommentsLinkMetadata'] = array( + array( + 'entity_type' => $entity->getEntityTypeId(), + 'entity_id' => $entity->id(), + 'field_name' => $field_name, + 'per_page' => $this->getSetting('per_page'), + 'default_mode' => $this->getSetting('default_mode'), + ), + ); + } + return $build; + } } - return array(); } } diff --git a/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php b/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php index b54564a..a3bd962 100644 --- a/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php +++ b/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php @@ -42,9 +42,6 @@ public static function defaultSettings() { */ public static function defaultInstanceSettings() { return array( - 'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED, - 'per_page' => 50, - 'form_location' => CommentItemInterface::FORM_BELOW, 'anonymous' => COMMENT_ANONYMOUS_MAYNOT_CONTACT, 'preview' => DRUPAL_OPTIONAL, ) + parent::defaultInstanceSettings(); @@ -117,21 +114,6 @@ public function instanceSettingsForm(array $form, FormStateInterface $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'), @@ -143,11 +125,6 @@ public function instanceSettingsForm(array $form, FormStateInterface $form_state ), '#access' => $anonymous_user->hasPermission('post comments'), ); - $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 748291b..4027602 100644 --- a/core/modules/comment/src/Plugin/views/field/NodeNewComments.php +++ b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php @@ -90,6 +90,21 @@ public function buildOptionsForm(&$form, FormStateInterface $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) { 'type' => $this->getValue($values, 'type'), )); $page_number = \Drupal::entityManager()->getStorage('comment') - ->getNewCommentPageNumber($this->getValue($values, 'comment_count'), $this->getValue($values), $node); + ->getNewCommentPageNumber($this->getValue($values, 'comment_count'), $this->getValue($values), $node, $this->options['default_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 94e423c..410a4fe 100644 --- a/core/modules/comment/src/Tests/CommentInterfaceTest.php +++ b/core/modules/comment/src/Tests/CommentInterfaceTest.php @@ -27,7 +27,7 @@ function testCommentInterface() { $this->setCommentPreview(DRUPAL_DISABLED); $this->setCommentForm(TRUE); $this->setCommentSubject(FALSE); - $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::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 00484b6..b937beb 100644 --- a/core/modules/comment/src/Tests/CommentLinksTest.php +++ b/core/modules/comment/src/Tests/CommentLinksTest.php @@ -7,10 +7,11 @@ namespace Drupal\comment\Tests; +use Drupal\comment\CommentInterface; +use Drupal\comment\CommentManagerInterface; +use Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\Core\Language\LanguageInterface; -use Drupal\comment\CommentInterface; -use Drupal\entity\Entity\EntityViewDisplay; /** * Basic comment links tests to ensure markup present. @@ -43,6 +44,28 @@ class CommentLinksTest extends CommentTestBase { public static $modules = array('views'); /** + * {@inheritdoc} + */ + public function setUp() { + parent::setUp(); + // Set comment field to use the link formatter on article teasers. + entity_get_display('node', 'article', 'teaser') + ->setComponent('comment', array( + 'label' => 'hidden', + 'type' => 'comment_links', + 'weight' => 20, + 'settings' => array( + 'per_page' => 50, + 'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED, + 'form_location' => CommentItemInterface::FORM_BELOW, + 'show_links' => CommentDefaultFormatter::LINKS_TEASER, + ), + )) + ->save(); + } + + + /** * Tests that comment links are output and can be hidden. */ public function testCommentLinks() { @@ -76,7 +99,7 @@ public function testCommentLinks() { $this->comment = $comment; // Change comment settings. - $this->setCommentSettings('form_location', CommentItemInterface::FORM_BELOW, 'Set comment form location'); + $this->setCommentFormatterSettings('form_location', CommentItemInterface::FORM_BELOW, 'Set comment form location'); $this->setCommentAnonymous(TRUE); $this->node->comment = CommentItemInterface::OPEN; $this->node->save(); diff --git a/core/modules/comment/src/Tests/CommentNewIndicatorTest.php b/core/modules/comment/src/Tests/CommentNewIndicatorTest.php index 59c21ff..e53ad1e 100644 --- a/core/modules/comment/src/Tests/CommentNewIndicatorTest.php +++ b/core/modules/comment/src/Tests/CommentNewIndicatorTest.php @@ -109,6 +109,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 4c50180..e0cec66 100644 --- a/core/modules/comment/src/Tests/CommentNodeAccessTest.php +++ b/core/modules/comment/src/Tests/CommentNodeAccessTest.php @@ -54,7 +54,7 @@ function testThreadedCommentView() { $this->setCommentPreview(DRUPAL_DISABLED); $this->setCommentForm(TRUE); $this->setCommentSubject(TRUE); - $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::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 9853d1c..fb809cc 100644 --- a/core/modules/comment/src/Tests/CommentPagerTest.php +++ b/core/modules/comment/src/Tests/CommentPagerTest.php @@ -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->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::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. @@ -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->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.'); + $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::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.'); @@ -133,7 +133,7 @@ function testCommentOrderingThreading() { // - 2 // - 5 - $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.'); $expected_order = array( 0, @@ -147,7 +147,7 @@ function testCommentOrderingThreading() { $this->drupalGet('node/' . $node->id()); $this->assertCommentOrder($comments, $expected_order); - $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.'); + $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.'); $expected_order = array( 0, @@ -228,7 +228,7 @@ function testCommentNewPageIndicator() { // - 2 // - 5 - $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.'); $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); + ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, CommentManagerInterface::COMMENT_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->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.'); + $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_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); + ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, CommentManagerInterface::COMMENT_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->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.', $field_name); + $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::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 8d188c3..7a5c251 100644 --- a/core/modules/comment/src/Tests/CommentPreviewTest.php +++ b/core/modules/comment/src/Tests/CommentPreviewTest.php @@ -36,7 +36,7 @@ function testCommentPreview() { $this->setCommentPreview(DRUPAL_OPTIONAL); $this->setCommentForm(TRUE); $this->setCommentSubject(TRUE); - $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); $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->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::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 6b7bf01..a6495a7 100644 --- a/core/modules/comment/src/Tests/CommentStatisticsTest.php +++ b/core/modules/comment/src/Tests/CommentStatisticsTest.php @@ -41,7 +41,7 @@ function testCommentNodeCommentStatistics() { $this->setCommentPreview(DRUPAL_DISABLED); $this->setCommentForm(TRUE); $this->setCommentSubject(FALSE); - $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::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 7a0b506..5d044cf 100644 --- a/core/modules/comment/src/Tests/CommentTestBase.php +++ b/core/modules/comment/src/Tests/CommentTestBase.php @@ -81,6 +81,20 @@ protected 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())); } @@ -266,7 +280,7 @@ public function setCommentPreview($mode, $field_name = 'comment') { * Defaults to 'comment'. */ public function setCommentForm($enabled, $field_name = 'comment') { - $this->setCommentSettings('form_location', ($enabled ? CommentItemInterface::FORM_BELOW : CommentItemInterface::FORM_SEPARATE_PAGE), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.', $field_name); + $this->setCommentFormatterSettings('form_location', ($enabled ? CommentItemInterface::FORM_BELOW : CommentItemInterface::COMMENT_FORM_SEPARATE_PAGE), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.', $field_name); } /** @@ -292,7 +306,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); } /** @@ -317,6 +331,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 476e3c9..ee82be8 100644 --- a/core/modules/comment/src/Tests/CommentThreadingTest.php +++ b/core/modules/comment/src/Tests/CommentThreadingTest.php @@ -24,7 +24,7 @@ function testCommentThreading() { $this->setCommentPreview(DRUPAL_DISABLED); $this->setCommentForm(TRUE); $this->setCommentSubject(TRUE); - $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); + $this->setCommentFormatterSettings('default_mode', CommentManagerInterface::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 039c305..2656f0e 100644 --- a/core/modules/forum/forum.module +++ b/core/modules/forum/forum.module @@ -506,6 +506,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( @@ -572,7 +574,7 @@ function template_preprocess_forums(&$variables) { if ($topic->new_replies) { $page_number = \Drupal::entityManager()->getStorage('comment') - ->getNewCommentPageNumber($topic->comment_count, $topic->new_replies, $topic, 'comment_forum'); + ->getNewCommentPageNumber($topic->comment_count, $topic->new_replies, $topic, $settings['default_mode'], $settings['per_page'], 'comment_node_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 c2b5582..647a406 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; /** @@ -25,6 +26,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(); // Hide the comment field in the rss view mode. entity_get_display('node', 'article', 'rss')