Skip to content

Instantly share code, notes, and snippets.

@andypost
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andypost/f361d0b7b1f29a048b25 to your computer and use it in GitHub Desktop.
Save andypost/f361d0b7b1f29a048b25 to your computer and use it in GitHub Desktop.
diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module
index 3c1ee17..ee258b1 100644
--- a/core/modules/comment/comment.module
+++ b/core/modules/comment/comment.module
@@ -139,9 +139,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',
),
);
}
@@ -1209,31 +1209,34 @@ 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 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 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/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') %}
- * <h3>{{ 'These comments are displayed in a threaded list.'|t }}</h3>
- * {% endif %}
- * @endcode
- * - COMMENT_MODE_FLAT
- * - COMMENT_MODE_THREADED
- *
- * @see template_preprocess_comment_wrapper()
- *
- * @ingroup themeable
- */
-#}
-<section{{ attributes }}>
- {% if comments and (entity.entityType != 'node' or entity.bundle != 'forum') %}
- {{ title_prefix }}
- <h2 class="title">{{ 'Comments'|t }}</h2>
- {{ title_suffix }}
- {% endif %}
-
- {{ comments }}
-
- {% if form %}
- <h2 class="title comment-form__title">{{ 'Add new comment'|t }}</h2>
- {{ form }}
- {% endif %}
-
-</section>
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..7a2dbcb
--- /dev/null
+++ b/core/modules/comment/templates/field--comment.html.twig
@@ -0,0 +1,37 @@
+{#
+/**
+ * @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.
+ *
+ * @see template_preprocess_field()
+ * @see comment_preprocess_field()
+ */
+#}
+<section{{ attributes }}>
+ {% if comments and not label_hidden %}
+ {{ title_prefix }}
+ <h2{{ title_attributes }}>{{ label }}</h2>
+ {{ title_suffix }}
+ {% endif %}
+
+ {{ comments }}
+
+ {% if comment_form %}
+ <h2{{ content_attributes }}>{{ 'Add new comment'|t }}</h2>
+ {{ comment_form }}
+ {% endif %}
+
+</section>
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment