1

This node has a comment by the user #1, who is the node author, and a comment by an anonymous user. I want to add an extra CSS class to comments by the node author.

The variables in Bartik's comment.tpl.php include $author and $comment-by-node-author.

What is the correct syntax for an if statement saying if this comment is by the node author (or by user #1) apply the class example and just print the comment as usual when the author is somebody else?

0

2 Answers 2

2

Drupal applies a CSS class to comments created from the user who is the author of the node (comment-by-node-author), but since you want to add a custom CSS class when the user who created the comment is either the node author or the user #1, I would use the following code.

function mytheme_preprocess_comment(&$variables) {
  if (($variables['comment']->uid == $variables['node']->uid) || $variables['comment']->uid == 1) {
    $variables['classes_array'][] = 'example';
  }
}

Using that code in your theme, the comment will have your custom CSS class when the user who created the comment is the same user who created the node, or when the user who created the comment is the user #1.

5
  • I'm not sure but in the way the question was redacted before you edited it, I understood Juc1 wanted to put an extra class for node author, not either user #1 or author. Let him tell us. ;)
    – Kojo
    Commented May 5, 2017 at 17:04
  • The first revision of the question says if this comment is by the node author (or by user id 1) apply the class "example" but if the comment is by anyone else just print the comment as usual ie without the extra class. After that, there is just my edit, which I think doesn't change the meaning of the question.
    – apaderno
    Commented May 5, 2017 at 17:13
  • You're right. Both your english and your knowledge are much better than mine !
    – Kojo
    Commented May 5, 2017 at 17:16
  • Oh no, neither of them are better. I just focused on a little part of the question, without being any better.
    – apaderno
    Commented May 5, 2017 at 17:36
  • @Kojo yes I meant different cases eg website 1 if the comment author is the node author, website 2 if the comment author is uid 1 - but kiamlaluno's code is helpful in showing possibilities. For another use case suppose I want to target comment authors with the role "editor" - I tried swapping uid for rid in kiamlaluno's code but rid does not seem to work. If rid does not work is there some other way to say "if the comment author has the role id 5"?
    – Juc1
    Commented May 9, 2017 at 16:26
2

If (what I'm not sure) understand your question correctly, you want to add an extra class if the comment was posted by the node author.

As explained in Bartik documentation:

 /*   - $classes: String of classes that can be used to style contextually through
  *   CSS. It can be manipulated through the variable $classes_array from
  *   preprocess functions. The default values can be one or more of the following:
  *   - comment: The current template type, i.e., "theming hook".
  *   - comment-by-anonymous: Comment by an unregistered user.
  *   - comment-by-node-author: Comment by the author of the parent node.
  */

There are already specific classes for comments posted :

  • by node author : .comment-by-node-author
  • by anonymous : .comment-by-anonymous

And you can see that in your node markup :

<div class="comment comment-by-node-author clearfix" about="/comment/2#comment-2" typeof="sioc:Post sioct:Comment">

/* ... */

<div class="comment comment-by-anonymous clearfix" about="/comment/3#comment-3" typeof="sioc:Post sioct:Comment">
1
  • I missed this and yes this works fine for this use case, thank you, but I was also hoping to address more uses case, see my comment below.
    – Juc1
    Commented May 9, 2017 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.