diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index b99a870..ee258b1 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -140,6 +140,7 @@ function comment_theme() { 'template' => 'comment', ), 'field__comment' => array( + 'base hook' => 'field', 'template' => 'field--comment', ), ); @@ -1210,30 +1211,31 @@ function template_preprocess_comment(&$variables) { /** * Prepares variables for comment field templates. * - * Default template: field__comment.html.twig. + * Default template: field--comment.html.twig. * * @param array $variables * An associative array containing: * - 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_field__comment(&$variables) { - // Provide contextual information. - $variables['entity'] = $variables['element'][0]['#entity']; - $variables['display_mode'] =$variables['element'][0]['#display_mode']; - - // Use field title for field label. - $variables['label'] = $variables['element']['#title']; - - // Add a comment wrapper class. - $variables['attributes']['class'][] = 'comment-wrapper'; - - // Create separate variables for the comments and comment form. - $variables['comments'] = $variables['element'][0]['comments']; - // The comment form is optional and may not exist. - $variables['comment_form'] = array(); - if (!empty($variables['element'][0]['comment_form'])) { - $variables['comment_form'] = $variables['element'][0]['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 special variables. + $variables['attributes']['class'][] = 'comment-wrapper'; + $variables['title_attributes']['class'][] = 'title'; + + // 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..579034f 100644 --- a/core/modules/comment/src/CommentManager.php +++ b/core/modules/comment/src/CommentManager.php @@ -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 89e6fe4..c7a0f11 100644 --- a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php +++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php @@ -218,16 +218,8 @@ public function viewElements(FieldItemListInterface $items) { } $elements[] = $output + array( - '#theme' => array( - 'field__comment__' . $entity->getEntityTypeId() . '__' . $items->getFieldDefinition()->getName() . '__' . $entity->bundle(), - 'field__comment__' . $entity->getEntityTypeId() . '__' . $items->getFieldDefinition()->getName(), - 'field__comment__' . $entity->getEntityTypeId() . '__' . $entity->bundle(), - 'field__comment__' . $entity->getEntityTypeId(), - 'field__comment', - 'field', - ), - '#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/templates/field--comment.html.twig b/core/modules/comment/templates/field--comment.html.twig index 9cbda14..7a2dbcb 100644 --- a/core/modules/comment/templates/field--comment.html.twig +++ b/core/modules/comment/templates/field--comment.html.twig @@ -5,26 +5,32 @@ * * Available variables: * - attributes: HTML attributes for the containing element. - * - label_hidden: Whether the field label has been set to hidden. - * - label_attributes: HTML attributes for the label. + * - label_hidden: Whether to show the field label or not. + * - title_attributes: HTML attributes for the title. * - label: The label for the field. - * - content_attributes: HTML attributes for the content. - * - items: List of all the field items. - * - item_attributes: HTML attributes for each item. + * - 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. * * @see template_preprocess_field() - * @see bartik_preprocess_field() + * @see comment_preprocess_field() */ #} - {% if not label_hidden %} -

{{ label }}

+ {% if comments and not label_hidden %} + {{ title_prefix }} + {{ label }} + {{ title_suffix }} {% endif %} {{ comments }} {% if comment_form %} -

{% trans %}Add new comment{% endtrans %}

+ {{ '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/system/system.module b/core/modules/system/system.module index 38c0500..3d67b65 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -710,6 +710,7 @@ function system_theme_suggestions_field(array $variables) { $element = $variables['element']; $suggestions[] = 'field__' . $element['#field_type']; + $suggestions[] = 'field__' . $element['#field_name']; $suggestions[] = 'field__' . $element['#entity_type'] . '__' . $element['#bundle']; $suggestions[] = 'field__' . $element['#entity_type'] . '__' . $element['#field_name']; $suggestions[] = 'field__' . $element['#entity_type'] . '__' . $element['#field_name'] . '__' . $element['#bundle'];