diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 03c32db..59e7d8d 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -134,9 +134,9 @@ function comment_theme() { 'render element' => 'elements', 'template' => 'comment', ), - 'comment_wrapper' => array( - 'render element' => 'content', - 'template' => 'comment-wrapper', + 'field__comment' => array( + 'base hook' => 'field', + 'template' => 'field--comment', ), ); } @@ -1207,31 +1207,37 @@ function template_preprocess_comment(&$variables) { } /** - * Prepares variables for comment wrapper templates. + * Prepares variables for comment field templates. * - * Default template: comment-wrapper.html.twig. + * Default template: field--comment.html.twig. * * @param array $variables * An associative array containing: - * - content: An associative array containing render arrays for the list of + * - element: An associative array containing render arrays for the list of * comments, and the comment form. Array keys: comments, comment_form. + * + * @todo Rename to template_preprocess_field__comment() once + * https://www.drupal.org/node/939462 is resolved. */ -function template_preprocess_comment_wrapper(&$variables) { - // Provide contextual information. - $variables['entity'] = $variables['content']['#entity']; - $variables['display_mode'] = $variables['content']['#display_mode']; - - // The comment form is optional and may not exist. - $variables['content'] += array('comment_form' => array()); - - $variables['attributes']['id'] = 'comments'; - - // Add a comment wrapper class. - $variables['attributes']['class'][] = 'comment-wrapper'; - - // Create separate variables for the comments and comment form. - $variables['comments'] = $variables['content']['comments']; - $variables['form'] = $variables['content']['comment_form']; +function comment_preprocess_field(&$variables) { + $element = $variables['element']; + if ($element['#field_type'] == 'comment') { + // Provide contextual information. + $variables['comment_display_mode'] = $element[0]['#comment_display_mode']; + $variables['comment_type'] = $element[0]['#comment_type']; + + // Adjust a comment field attributes. + $variables['attributes']['class'][] = 'comment-wrapper'; + $variables['title_attributes']['class'][] = 'title'; + + // Append additional attributes (eg. RDFa) from the first field item. + $variables['attributes'] += $variables['item_attributes'][0]->storage(); + + // Create separate variables for the comments and comment form. + $variables['comments'] = $element[0]['comments']; + $variables['comment_form'] = $element[0]['comment_form']; + $variables['content_attributes']['class'] = array('title', 'comment-form__title'); + } } /** diff --git a/core/modules/comment/src/CommentManager.php b/core/modules/comment/src/CommentManager.php index 1bed270..548e2df 100644 --- a/core/modules/comment/src/CommentManager.php +++ b/core/modules/comment/src/CommentManager.php @@ -175,7 +175,7 @@ public function addDefaultField($entity_type, $bundle, $field_name = 'comment', // Make sure the instance doesn't already exist. if (!array_key_exists($field_name, $this->entityManager->getFieldDefinitions($entity_type, $bundle))) { $instance = $this->entityManager->getStorage('field_instance_config')->create(array( - 'label' => 'Comment settings', + 'label' => 'Comments', 'description' => '', 'field_name' => $field_name, 'entity_type' => $entity_type, @@ -212,7 +212,7 @@ public function addDefaultField($entity_type, $bundle, $field_name = 'comment', // Set default to display comment list. entity_get_display($entity_type, $bundle, 'default') ->setComponent($field_name, array( - 'label' => 'hidden', + 'label' => 'above', 'type' => 'comment_default', 'weight' => 20, )) diff --git a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php index 4597761..c7a0f11 100644 --- a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php +++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php @@ -218,9 +218,8 @@ 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'), + '#comment_type' => $this->getFieldSetting('comment_type'), + '#comment_display_mode' => $this->getFieldSetting('default_mode'), 'comments' => array(), 'comment_form' => array(), ); diff --git a/core/modules/comment/src/Plugin/Field/FieldWidget/CommentWidget.php b/core/modules/comment/src/Plugin/Field/FieldWidget/CommentWidget.php index 4b8de0a..f9fdf73 100644 --- a/core/modules/comment/src/Plugin/Field/FieldWidget/CommentWidget.php +++ b/core/modules/comment/src/Plugin/Field/FieldWidget/CommentWidget.php @@ -65,6 +65,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // Get default value from the field instance. $field_default_values = $this->fieldDefinition->getDefaultValue($entity); + // Override widget title to be helpful for end users. + $element['#title'] = $this->t('Comment settings'); + $element += array( '#type' => 'details', // Open the details when the selected value is different to the stored diff --git a/core/modules/comment/src/Tests/CommentNonNodeTest.php b/core/modules/comment/src/Tests/CommentNonNodeTest.php index 3bdeb72..eabafce 100644 --- a/core/modules/comment/src/Tests/CommentNonNodeTest.php +++ b/core/modules/comment/src/Tests/CommentNonNodeTest.php @@ -238,7 +238,7 @@ function testCommentFunctionality() { $this->drupalLogin($limited_user); // Test that default field exists. $this->drupalGet('entity_test/structure/entity_test/fields'); - $this->assertText(t('Comment settings')); + $this->assertText(t('Comments')); $this->assertLinkByHref('entity_test/structure/entity_test/fields/entity_test.entity_test.comment'); // Test widget hidden option is not visible when there's no comments. $this->drupalGet('entity_test/structure/entity_test/entity-test/fields/entity_test.entity_test.comment'); diff --git a/core/modules/comment/templates/comment-wrapper.html.twig b/core/modules/comment/templates/comment-wrapper.html.twig deleted file mode 100644 index 52bb6b1..0000000 --- a/core/modules/comment/templates/comment-wrapper.html.twig +++ /dev/null @@ -1,52 +0,0 @@ -{# -/** - * @file - * Default theme implementation for a comments container. - * - * Available variables: - * - comments: List of comments rendered through comment.html.twig. - * - form: The 'Add new comment' form. - * - content: The content-related elements for the comment display. Use - * 'content' to print them all, or print a subset such as - * 'content.comment_form'. - * - attributes: Remaining HTML attributes for the containing element. - * It includes the 'class' information, which includes: - * - comment-wrapper: The current template type, i.e., "theming hook". - * - title_prefix: Additional output populated by modules, intended to be - * displayed in front of the main title tag that appears in the template. - * - title_suffix: Additional title output populated by modules, intended to - * be displayed after the main title tag that appears in the template. - * - * The following variables are provided for contextual information. - * - entity: The entity to which the comments belong. - * - display_mode: The display mode for the comment listing, flat or threaded. - * The constants below show the possible values and should be used for - * comparison, as in the following example: - * @code - * {% if display_mode is constant('COMMENT_MODE_THREADED') %} - *

{{ 'These comments are displayed in a threaded list.'|t }}

- * {% endif %} - * @endcode - * - COMMENT_MODE_FLAT - * - COMMENT_MODE_THREADED - * - * @see template_preprocess_comment_wrapper() - * - * @ingroup themeable - */ -#} - - {% if comments and (entity.entityType != 'node' or entity.bundle != 'forum') %} - {{ title_prefix }} -

{{ 'Comments'|t }}

- {{ title_suffix }} - {% endif %} - - {{ comments }} - - {% if form %} -

{{ 'Add new comment'|t }}

- {{ form }} - {% endif %} - - diff --git a/core/modules/comment/templates/field--comment.html.twig b/core/modules/comment/templates/field--comment.html.twig new file mode 100644 index 0000000..9aae572 --- /dev/null +++ b/core/modules/comment/templates/field--comment.html.twig @@ -0,0 +1,39 @@ +{# +/** + * @file + * Default theme override for comment fields. + * + * Available variables: + * - attributes: HTML attributes for the containing element. + * - label_hidden: Whether to show the field label or not. + * - title_attributes: HTML attributes for the title. + * - label: The label for the field. + * - title_prefix: Additional output populated by modules, intended to be + * displayed in front of the main title tag that appears in the template. + * - title_suffix: Additional title output populated by modules, intended to + * be displayed after the main title tag that appears in the template. + * - comments: List of comments rendered through comment.html.twig. + * - content_attributes: HTML attributes for the form title. + * - comment_form: The 'Add new comment' form. + * - comment_display_mode: Is the comments are threaded. + * - comment_type: The comment type bundle ID for the comment field. + * + * @see template_preprocess_field() + * @see comment_preprocess_field() + */ +#} + + {% if comments and not label_hidden %} + {{ title_prefix }} + {{ label }} + {{ title_suffix }} + {% endif %} + + {{ comments }} + + {% if comment_form %} + {{ 'Add new comment'|t }} + {{ comment_form }} + {% endif %} + + diff --git a/core/modules/forum/forum.install b/core/modules/forum/forum.install index 2b3d14f..6effde2 100644 --- a/core/modules/forum/forum.install +++ b/core/modules/forum/forum.install @@ -87,6 +87,14 @@ function forum_install() { )); if (empty($fields)) { Drupal::service('comment.manager')->addDefaultField('node', 'forum', 'comment_forum', CommentItemInterface::OPEN, 'comment_forum'); + // Hide label for comment field. + entity_get_display('node', 'forum', 'default') + ->setComponent('comment_forum', array( + 'label' => 'hidden', + 'type' => 'comment_default', + 'weight' => 20, + )) + ->save(); } } } diff --git a/core/modules/migrate_drupal/config/install/migrate.migration.d6_comment_field_instance.yml b/core/modules/migrate_drupal/config/install/migrate.migration.d6_comment_field_instance.yml index 6bc938b..27b4646 100644 --- a/core/modules/migrate_drupal/config/install/migrate.migration.d6_comment_field_instance.yml +++ b/core/modules/migrate_drupal/config/install/migrate.migration.d6_comment_field_instance.yml @@ -4,7 +4,7 @@ source: plugin: d6_comment_variable constants: entity_type: node - label: Comment settings + label: Comments required: true process: entity_type: 'constants/entity_type' diff --git a/core/modules/migrate_drupal/src/Tests/d6/MigrateCommentVariableDisplayBase.php b/core/modules/migrate_drupal/src/Tests/d6/MigrateCommentVariableDisplayBase.php index ac5c9c7..fc5deb4 100644 --- a/core/modules/migrate_drupal/src/Tests/d6/MigrateCommentVariableDisplayBase.php +++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateCommentVariableDisplayBase.php @@ -58,7 +58,7 @@ protected function setUp() { foreach ($this->types as $type) { entity_create('node_type', array('type' => $type))->save(); entity_create('field_instance_config', array( - 'label' => 'Comment settings', + 'label' => 'Comments', 'description' => '', 'field_name' => 'comment', 'entity_type' => 'node',