From e3e70445ca0c2aacae89dffec26d80eb392471a3 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Sat, 1 Nov 2014 14:22:09 +0000
Subject: [PATCH] Issue #2329783 by lauriii, jamesquinton | davidhernandez:
 Move comment classes from preprocess to templates.

---
 core/modules/comment/comment.module           | 21 +------------------
 .../comment/templates/comment.html.twig       | 13 ++++++++++--
 .../themes/bartik/templates/comment.html.twig | 13 ++++++++++--
 3 files changed, 23 insertions(+), 24 deletions(-)

diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module
index bdf53bf7db7b..0ee6f4c3f0d8 100644
--- a/core/modules/comment/comment.module
+++ b/core/modules/comment/comment.module
@@ -698,6 +698,7 @@ function template_preprocess_comment(&$variables) {
     '#account' => $account,
   );
   $variables['author'] = drupal_render($username);
+  $variables['author_id'] = $comment->getOwnerId();
   $variables['new_indicator_timestamp'] = $comment->getChangedTime();
   $variables['created'] = format_date($comment->getCreatedTime());
   // Avoid calling format_date() twice on the same timestamp.
@@ -790,28 +791,8 @@ function template_preprocess_comment(&$variables) {
     $variables['status'] = $comment->isPublished() ? 'published' : 'unpublished';
   }
 
-  // Gather comment classes.
-  $variables['attributes']['class'][] = 'comment';
-  // 'published' class is not needed, it is either 'preview' or 'unpublished'.
-  if ($variables['status'] != 'published') {
-    $variables['attributes']['class'][] = $variables['status'];
-  }
-  if (!$comment->getOwnerId()) {
-    $variables['attributes']['class'][] = 'by-anonymous';
-  }
-  else {
-    // @todo Use $entity->getAuthorId() after https://drupal.org/node/2078387
-    if ($commented_entity instanceof EntityOwnerInterface && $comment->getOwnerId() == $commented_entity->getOwnerId()) {
-      $variables['attributes']['class'][] = 'by-' . $commented_entity->getEntityTypeId() . '-author';
-    }
-  }
-  // Add clearfix class.
-  $variables['attributes']['class'][] = 'clearfix';
-
   // Add comment author user ID. Necessary for the comment-by-viewer library.
   $variables['attributes']['data-comment-user-id'] = $comment->getOwnerId();
-
-  $variables['content_attributes']['class'][] = 'content';
 }
 
 /**
diff --git a/core/modules/comment/templates/comment.html.twig b/core/modules/comment/templates/comment.html.twig
index 801f9a6c5844..24dbd079627b 100644
--- a/core/modules/comment/templates/comment.html.twig
+++ b/core/modules/comment/templates/comment.html.twig
@@ -66,7 +66,16 @@
  * @ingroup themeable
  */
 #}
-<article{{ attributes }}>
+{%
+  set classes = [
+    'comment',
+    status != 'published' ? status,
+    not author_id ? 'by-anonymous',
+    author_id and author_id == commented_entity.getOwnerId() ? 'by-' ~ commented_entity.getEntityTypeId() ~ '-author',
+    'clearfix',
+  ]
+%}
+<article{{ attributes.addClass(classes) }}>
   {% if title %}
     {{ title_prefix }}
   {% endif %}
@@ -100,7 +109,7 @@
     {{ permalink }}
   </footer>
 
-  <div{{ content_attributes }}>
+  <div{{ content_attributes.addClass('content') }}>
     {{ content|without('links') }}
 
     {% if signature %}
diff --git a/core/themes/bartik/templates/comment.html.twig b/core/themes/bartik/templates/comment.html.twig
index c5c0a7a905bd..0fa199146bda 100644
--- a/core/themes/bartik/templates/comment.html.twig
+++ b/core/themes/bartik/templates/comment.html.twig
@@ -64,7 +64,16 @@
  * @see template_preprocess_comment()
  */
 #}
-<article class="{{ attributes.class }} clearfix" role="article"{{ attributes|without('class', 'role') }}>
+{%
+  set classes = [
+    'comment',
+    status != 'published' ? status,
+    not author_id ? 'by-anonymous',
+    author_id and author_id == commented_entity.getOwnerId() ? 'by-' ~ commented_entity.getEntityTypeId() ~ '-author',
+    'clearfix',
+  ]
+%}
+<article role="article"{{ attributes.addClass(classes)|without('role') }}>
 
   <header class="comment-header">
     <div class="attribution">
@@ -110,7 +119,7 @@
       {{ title_suffix }}
     {% endif %}
 
-    <div{{ content_attributes }}>
+    <div{{ content_attributes.addClass('content') }}>
       {{ content|without('links') }}
     </div> <!-- /.content -->
 
-- 
GitLab