diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index b67825e..d875009 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -127,3 +127,30 @@ function comment_update_8001() { /** * @} End of "addtogroup updates-8.0.0-rc". */ + +/** + * Adds the new 'view_mode' setting to all view displays having showing the + * 'comment_default' formatter. + */ +function comment_update_8101() { + $config_factory = \Drupal::configFactory(); + // Iterate on all entity view displays. + foreach ($config_factory->listAll('core.entity_view_display.') as $name) { + $changed = FALSE; + $display = $config_factory->getEditable($name); + $components = $display->get('content') ?: []; + foreach ($components as $field_name => $component) { + if (isset($component['type']) && ($component['type'] == 'comment_default')) { + if (empty($display->get("content.{$field_name}.settings.view_mode"))) { + $display->set("content.{$field_name}.settings.view_mode", 'default'); + $changed = TRUE; + } + } + } + + if ($changed) { + $display->save(TRUE); + } + } + +} diff --git a/core/modules/comment/config/schema/comment.schema.yml b/core/modules/comment/config/schema/comment.schema.yml index d9f5a77..652ba99 100644 --- a/core/modules/comment/config/schema/comment.schema.yml +++ b/core/modules/comment/config/schema/comment.schema.yml @@ -4,6 +4,9 @@ field.formatter.settings.comment_default: type: mapping label: 'Comment display format settings' mapping: + view_mode: + type: string + label: 'The comment entity view mode to be used in this formatter ' pager_id: type: integer label: 'Pager ID' diff --git a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php index 64ff301..884e34e 100644 --- a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php +++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php @@ -8,6 +8,7 @@ namespace Drupal\comment\Plugin\Field\FieldFormatter; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; +use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityFormBuilderInterface; use Drupal\Core\Field\FieldItemListInterface; @@ -40,6 +41,7 @@ class CommentDefaultFormatter extends FormatterBase implements ContainerFactoryP */ public static function defaultSettings() { return array( + 'view_mode' => 'default', 'pager_id' => 0, ) + parent::defaultSettings(); } @@ -162,7 +164,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $comments_per_page = $comment_settings['per_page']; $comments = $this->storage->loadThread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id')); if ($comments) { - $build = $this->viewBuilder->viewMultiple($comments); + $build = $this->viewBuilder->viewMultiple($comments, $this->getSetting('view_mode')); $build['pager']['#type'] = 'pager'; if ($this->getSetting('pager_id')) { $build['pager']['#element'] = $this->getSetting('pager_id'); @@ -206,6 +208,13 @@ public function viewElements(FieldItemListInterface $items, $langcode) { */ public function settingsForm(array $form, FormStateInterface $form_state) { $element = array(); + $element['view_mode'] = array( + '#type' => 'select', + '#title' => $this->t('Comments view mode'), + '#description' => $this->t('Select the view mode used to show the list of comments.'), + '#default_value' => $this->getSetting('view_mode'), + '#options' => $this->entityManager->getViewModeOptionsByBundle('comment', $this->getFieldSetting('comment_type')), + ); $element['pager_id'] = array( '#type' => 'select', '#title' => $this->t('Pager ID'), @@ -229,4 +238,22 @@ public function settingsSummary() { return array(); } + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + $dependencies = parent::calculateDependencies(); + + if ($mode = $this->getSetting('view_mode')) { + if ($bundle = $this->getFieldSetting('comment_type')) { + /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */ + if ($display = EntityViewDisplay::load("comment.$bundle.$mode")) { + $dependencies[$display->getConfigDependencyKey()][] = $display->getConfigDependencyName(); + } + } + } + + return $dependencies; + } + } diff --git a/core/modules/comment/src/Tests/CommentInterfaceTest.php b/core/modules/comment/src/Tests/CommentInterfaceTest.php index 4b326a6..c547772 100644 --- a/core/modules/comment/src/Tests/CommentInterfaceTest.php +++ b/core/modules/comment/src/Tests/CommentInterfaceTest.php @@ -10,6 +10,9 @@ use Drupal\comment\CommentManagerInterface; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\comment\Entity\Comment; +use Drupal\Component\Utility\Unicode; +use Drupal\Core\Entity\Entity\EntityViewDisplay; +use Drupal\Core\Entity\Entity\EntityViewMode; use Drupal\user\RoleInterface; use Drupal\filter\Entity\FilterFormat; @@ -291,4 +294,54 @@ public function testAutoFilledHtmlSubject() { $this->assertEqual('(No subject)', Comment::load(2)->getSubject()); } + /** + * Tests the comment field formatter used with non-default comment entity view + * mode. + */ + public function testViewMode() { + $this->drupalLogin($this->webUser); + $this->drupalGet($this->node->toUrl()); + $comment_text = $this->randomMachineName(); + // Post a comment. + $this->postComment($this->node, $comment_text); + + // Comment displayed in 'default' display mode found and has body text. + $comment_element = $this->cssSelect('.comment-wrapper'); + $this->assertTrue(!empty($comment_element)); + $this->assertRaw('

' . $comment_text . '

'); + + // Create a new comment entity view mode. + $mode = Unicode::strtolower($this->randomMachineName()); + EntityViewMode::create([ + 'targetEntityType' => 'comment', + 'id' => "comment.$mode", + ])->save(); + // Create the corresponding entity view display for article node-type. Note + // that this new view display mode doesn't contain the comment body. + EntityViewDisplay::create([ + 'targetEntityType' => 'comment', + 'bundle' => 'comment', + 'mode' => $mode, + ])->setStatus(TRUE)->save(); + + /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $node_display */ + $node_display = EntityViewDisplay::load("node.article.default"); + $formatter = $node_display->getComponent('comment'); + // Change the node comment field formatter to use $mode mode instead of + // 'default' mode. + $formatter['settings']['view_mode'] = $mode; + $node_display + ->setComponent('comment', $formatter) + ->save(); + + // Reloading the node page to show the same node with its same comment but + // with a different display mode. + $this->drupalGet($this->node->toUrl()); + // The comment should exist but without the body text because we used $mode + // mode this time. + $comment_element = $this->cssSelect('.comment-wrapper'); + $this->assertTrue(!empty($comment_element)); + $this->assertNoRaw('

' . $comment_text . '

'); + } + } diff --git a/core/modules/comment/src/Tests/Update/CommentUpdateTest.php b/core/modules/comment/src/Tests/Update/CommentUpdateTest.php new file mode 100644 index 0000000..83ef05d --- /dev/null +++ b/core/modules/comment/src/Tests/Update/CommentUpdateTest.php @@ -0,0 +1,47 @@ +databaseDumpFiles = [ + __DIR__ . '/../../../../system/tests/fixtures/update/drupal-8-rc1.bare.standard.php.gz', + ]; + } + + /** + * Tests comment_update_8101(). + * + * @see comment_update_8101() + */ + public function testCommentUpdate8101() { + // Load the 'node.article.default' entity view display config, and check + // that component 'comment' does not contain the 'view_mode' setting. + $config = $this->config('core.entity_view_display.node.article.default'); + $this->assertNull($config->get('content.comment.settings.view_mode')); + + // Run updates. + $this->runUpdates(); + + // Reload the config, and check that 'view_mode' and has 'default' as value. + $config = $this->config('core.entity_view_display.node.article.default'); + $this->assertIdentical($config->get('content.comment.settings.view_mode'), 'default'); + } + +} diff --git a/core/modules/forum/config/install/core.entity_view_display.node.forum.default.yml b/core/modules/forum/config/install/core.entity_view_display.node.forum.default.yml index 39389a5..e9cc71d 100644 --- a/core/modules/forum/config/install/core.entity_view_display.node.forum.default.yml +++ b/core/modules/forum/config/install/core.entity_view_display.node.forum.default.yml @@ -26,6 +26,7 @@ content: type: comment_default weight: 20 settings: + view_mode: default pager_id: 0 third_party_settings: { } links: diff --git a/core/profiles/standard/config/install/core.entity_view_display.node.article.default.yml b/core/profiles/standard/config/install/core.entity_view_display.node.article.default.yml index e0d3782..605f494 100644 --- a/core/profiles/standard/config/install/core.entity_view_display.node.article.default.yml +++ b/core/profiles/standard/config/install/core.entity_view_display.node.article.default.yml @@ -23,13 +23,6 @@ content: settings: { } third_party_settings: { } label: hidden - comment: - label: above - type: comment_default - weight: 20 - settings: - pager_id: 0 - third_party_settings: { } field_image: type: image weight: -1 @@ -50,6 +43,7 @@ content: type: comment_default weight: 110 settings: + view_mode: default pager_id: 0 third_party_settings: { } links: