Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Original Report:
+++ b/core/modules/comment/comment.module
@@ -700,6 +700,7 @@ function template_preprocess_comment(&$variables) {
+ $variables['author_id'] = $comment->getOwnerId();
+++ b/core/modules/comment/templates/comment.html.twig
@@ -64,7 +64,16 @@
+ not author_id ? 'by-anonymous',
This perpetuates the "user ID zero is the anonymous user" check which IMHO makes no sense.This should've used $comment->getOwner->isAnonymous().
Proposed resolution
Remaining tasks
User interface changes
API changes
Follow-up to #2329783: Move comment classes from preprocess to templates
Comment | File | Size | Author |
---|---|---|---|
#17 | comments_to_check-2372909-17.patch | 1.1 KB | Tom Verhaeghe |
#17 | interdiff-13-17.txt | 1.14 KB | Tom Verhaeghe |
Comments
Comment #1
Wim LeersComment #2
Tom Verhaeghe CreditAttribution: Tom Verhaeghe commentedComment #3
Tom Verhaeghe CreditAttribution: Tom Verhaeghe commentedAdded new variable 'author_anonymous' and updated twig templates to use that in both the comments module and bartik.
Comment #4
Wim LeersWouldn't
author_is_anonymous
be better?Comment #5
Tom Verhaeghe CreditAttribution: Tom Verhaeghe commentedConventionally that would be better, I also didn't update the twig documentation. I'll fix that ...
Comment #6
Tom Verhaeghe CreditAttribution: Tom Verhaeghe commentedComment #7
Wim LeersThanks! Looks great.
Comment #8
alexpottCan we not use
comment.getOwner().isAnonymous()
in twig instead of a new variable?Comment #9
joelpittet@alexpott Yes we can. We can also use:
comment.owner.anonymous ? 'by-anonymous'
or any combo that reads the best.@Tom Verhaeghe what are your thoughts here, any preference?
Comment #10
Wim Leers#8: oh, I didn't know that was possible, that's even better :)
Comment #11
joelpittetFrom our slides:)
http://drupaltwig.github.io/ThemeSystemBadCamp2014/#/5
The more people that know this magic/syntax sugar the better
Comment #12
Wim Leers#11: Great, thanks! :) That was very helpful :)
Comment #13
Tom Verhaeghe CreditAttribution: Tom Verhaeghe commentedChanged the way to check if the author is anonymous.
PS: Is it possible that calling methods on objects is done this way
comment.getOwner().isAnonymous()
instead ofcomment->getOwner()->isAnonymous()
as shown in the slides?Comment #14
Wim LeersAssigning to Joël for review.
Comment #15
joelpittet@Tom Verhaeghe sorry our slides could be clearer.
{{ comment.owner.anonymous }}
will get converted to PHP eventually. When it does, each "attribute" (the thing after the dot, Twig's language for what we usually refer to as a member variable or property) will be converted to PHP in the following ways:In that order of checking until it finds the correct PHP 'thing'.
In this specific case it will do the following (theory and rough equivalent):
Here's an example of all the things that work:
Then visit
http://localhost/magic
https://gist.github.com/c5a20ddd1621e4d07e88
So I personally recommend the calls without the method calls, though #13 will technically work and is fine:)
Comment #16
joelpittetLong story short, #13 is good as-is and should work beautifully though I'd prefer the slightly simpler syntax.
Comment #17
Tom Verhaeghe CreditAttribution: Tom Verhaeghe commentedOkay, thanks for explaining. It's clear to me now :-)
I changed the syntax in this patch.
Comment #18
Wim LeersRTBC per #16.
(That's quite a bit of magic though.)
Comment #19
alexpottReclassifying as a bug since
Sounds like one.
This issue is a minor bug fix, and doesn't include any disruptive changes, and is in the unfrozen category (templates) so it is allowed per https://www.drupal.org/core/beta-changes. Committed 010cfbb and pushed to 8.0.x. Thanks!