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.
The documentation in the user.html.twig
template describes the use of a account
variable. However {{ dump(account) }}
returns no data. This is either a documentation or a code fault.
Current documentation:
* Available variables:
* - content: A list of content items. Use 'content' to print all content, or
* print a subset such as 'content.field_example'.
* - Field variables: For each field attached to the user a corresponding
* variable is defined; e.g., account.field_example has a variable
* 'field_example' defined. When needing to access a field's raw values,
* developers/themers are strongly encouraged to use these variables.
* Otherwise they will have to explicitly specify the desired field language,
* e.g. account.field_example.en, thus overriding any language negotiation
* rule that was previously applied.
* - attributes: HTML attributes for the container element.
Comment | File | Size | Author |
---|---|---|---|
#41 | user_html_twig-2428861-41.patch | 3.47 KB | travis-bradbury |
#41 | interdiff-2428861-39-41.txt | 384 bytes | travis-bradbury |
Comments
Comment #1
Sagar Ramgade CreditAttribution: Sagar Ramgade commentedHi,
Please test the patch attached, this will fix it.
Comment #3
Sutharsan CreditAttribution: Sutharsan commentedIf it was that easy... There is a
$variables['user']
, I did discover that and went down the rabbit hole with it, but no luck. Got confused (as in D7) by the seemingly inconsistent naming of 'account' and 'user' throughout core. The content entity methods like$node->field_foo->value
don't apply to user.{{ user.field_foo }}
does not work now.Comment #4
Sutharsan CreditAttribution: Sutharsan commentedIf the profile data is not available, I doubt whether we should load it in the preprocess since it is not used in the template at all. We could add this, but it would be a waste of resources:
$variables['account'] = User::load($variables['user']->id());
. If so, we are better off by removing the account info from the documentation.Comment #5
star-szrComment #6
star-szrI'd suggest a good next step here might be to figure out when this was lost, assuming it was there before :)
Comment #7
star-szrThis is slightly related to #2385243: Make core user fields available for twig templates, but mostly I think what we should do in this issue is update the documentation to use content.field_example rather than account.field_example (and so on).
Setting to active to reflect that we need a new patch/new approach.
Comment #8
star-szrComment #9
travis-bradbury CreditAttribution: travis-bradbury at Acro Commerce commentedI changed the documentation to use content as an example instead of account. However, I think some other changes might be necessary:
There is a user variable available with a variety of account related fields. Perhaps this information is what "account.field_example" was originally referring to?
There are no language-specific variables (eg. "content.member_for" exists, but not "content.member_for.en") -- unless I'm missing something.
Comment #10
travis-bradbury CreditAttribution: travis-bradbury at Acro Commerce commentedComment #11
leeotzu CreditAttribution: leeotzu as a volunteer and at Srijan | A Material+ Company commentedPlease change documentation from variable is defined; e.g., content has a variable to variable is defined; e.g., content.field_example has a variable.
Comment #12
leeotzu CreditAttribution: leeotzu as a volunteer and at Srijan | A Material+ Company commentedComment #13
yogen.prasad CreditAttribution: yogen.prasad commentedComment #14
jhodgdonThanks for the patch!
Hm....
I think this is still confusing. content.field_example is not a variable in itself -- it's a component of the content variable.
And it seems to be confusing cause (the site has a field called 'field_example' defined for user accounts) with effect (this Twig template's content variable has a component called field_example on it).
So... Can we make another attempt to rewrite this so it is clearer please?
Comment #15
star-szrOops, of course this issue is related to itself, don't need metadata for that ;)
Comment #16
neetu morwani CreditAttribution: neetu morwani commentedComment #17
neetu morwani CreditAttribution: neetu morwani commentedComment #18
neetu morwani CreditAttribution: neetu morwani commentedComment #20
Nitesh Sethia CreditAttribution: Nitesh Sethia as a volunteer commentedComment #22
deepakaryan1988Comment #23
deepakaryan1988Comment #25
Nitesh Sethia CreditAttribution: Nitesh Sethia as a volunteer commentedRerolled the patch.
Comment #26
dani3lr0se CreditAttribution: dani3lr0se as a volunteer commentedDownloaded and applied the patch with no issues. Not sure if it's necessary for documentation, but I also attached a screenshot. Looks good to me.
Comment #27
jhodgdonMy feedback on #14 was not addressed. Please take a look -- thanks!
Comment #28
travis-bradbury CreditAttribution: travis-bradbury at Acro Commerce commentedI changed it to no longer say that 'field_example' has a variable also called 'field_example' defined.
Comment #31
jhodgdonThanks! This is almost there... I still think it is a bit unclear. With the latest patch, it says:
For each field attached to the user a corresponding variable is defined; e.g., user has a variable 'field_example' defined.
How about changing this to:
For each field attached to the user entity, a corresponding variable is available. For example, if there is a field called 'field_example', this template will have a field_example variable available.
Or something like that?
Comment #32
travis-bradbury CreditAttribution: travis-bradbury at Acro Commerce commentedHow are these changes?
Comment #33
jhodgdonThanks for the new patch! Not sure it's correct though?
I think I'd better leave this for someone else to review, because I am really not sure what is in the template... but here are my thoughts (which might be wrong except the first point):
There's an extra space at the end of this line.
Um, is the variable in the template really called "user.field_example"? I thought it was just "field_example"?
So this is saying that if you use user.field_example you need to specify the language... so I think the point of this documentation is to say that there are special variables created that match the names of the fields (without user. prepended). Right?
Comment #34
star-szrI missed this when I commented here earlier, but very importantly these "field variables" on the top level no longer exist, so the documentation needs a bigger overhaul (or maybe even just removal).
See template_preprocess_user(). Mainly 'user' and 'content' which contains the fields. Some of the 'properties' can be accessed via user, not sure if the same language caveats apply to those. Potentially the docs could be moved to the 'user' variable and adapted to make sense there.
For debugging I recommend installing Kint from https://www.drupal.org/project/devel and adding
{{ kint() }}
to the template whose variables you're trying to figure out.Edit: Forgot to mention, for that to work uncomment the line about Twig debug in sites/default/services.yml and clear caches (or
drush cr
) :)Comment #35
jhodgdonIn that case I think there are no "field variables" and we should just remove that section of the docs?
Comment #36
star-szrI would be fine with that myself.
Comment #37
travis-bradbury CreditAttribution: travis-bradbury at Acro Commerce commentedWhat I think I see here is that all of the user's fields are actually available in
content
orcontent.member_for
but not in top-level variables likemember_for
orfield_example
.The
user
variable also appears to be unavailable since its members are all protected.So I would change the documentation to remove mention of variables
user
and 'Field Variables' and expand the description of thecontent
variable to say that a user's fields are available there.There are also variables like
logged_in
andis_admin
. Should we mention them in the documentation?Comment #38
star-szruser
was specifically added in #2385243: Make core user fields available for twig templates. Its properties may be protected but getter methods can be used to get at fields that aren't incontent
- see Available methods.logged_in
andis_admin
are generic variables available to all templates so don't need to be documented, see _template_preprocess_default_variables().So what I would suggest to move forward here is merging some of the docs from 'Field variables' to make the 'content' variable documentation better - like @tbradbury mentioned:
Comment #39
travis-bradbury CreditAttribution: travis-bradbury at Acro Commerce commentedComment #40
star-szrThis needs to stay, but only in the default user.html.twig.
Otherwise this looks quite reasonable to me, thanks @tbradbury!
Comment #41
travis-bradbury CreditAttribution: travis-bradbury at Acro Commerce commentedAh, thanks, didn't notice I killed that.
Comment #42
star-szrWorks for me, thanks!
Comment #43
alexpottThis documentation about language seems extremely relevant. Did anyone test a multilingual install. Also entity field access has a lot of magic so just inspecting the content variable might not be enough to prove
content.user_picture.en
or (for example)content.user_picture.fr
does not work.Comment #44
travis-bradbury CreditAttribution: travis-bradbury at Acro Commerce commentedI did some testing after installing multilingual modules.
{{ content.member_for }}
){{ content.member_for.en }}
and{{ content.member_for.fr }}
.Comment #45
star-szrThis is still the case, but it's now
content.field_example
instead offield_example
. I think a more appropriate test would beuser.field_example.en
,user
being the closest analog to the oldaccount
variable.Thanks @tbradbury!
Comment #46
travis-bradbury CreditAttribution: travis-bradbury at Acro Commerce commentedI think the patch still explains that a user's fields are available as
content.field_example
. It now says:- content: A list of content items. Use 'content' to print all content, or print a subset such as 'content.field_example'. Fields attached to a user such as 'user_picture' are available as 'content.user_picture'.
I tested
user.member_for
anduser.user_picture
, plus both with.en
and.fr
. None of them had any output. From what I can tell, you can't actually access anything inuser
except with its methods -user.getEmail()
and the like.Comment #47
star-szrOkay, then it sounds like we're covered. In general I think the documentation around the language variables was "don't use these" anyway. Thanks @tbradbury!
Comment #48
alexpottDocs are not frozen in beta. Committed d416fab and pushed to 8.0.x. Thanks!
I confirmed with @berdir that translated versions on fields can no longer be accessed this way.