Score:0

How to display entity reference field in User Profile Twig

ru flag

I asked this over on the Drupal.org forum, but I thought I'd give it a try here too.

I am using an taxonomy entity reference field to display author names on our site. I added a 'published_name' field and referenced it to the Author taxonomy. What I'd like to do is make a custom user profile twig that shows that entity reference field value on the author's profile page, including it's link, so when the displayed name is clicked, it shows all the books connected to that author. I made two different profile types using the Profile Module. Everyone gets a Public Profile, and the authors get a second one just for their custom information. The Writer Profile (machine name writer_profile) is the one with the field 'field_published_name'.

On the writer's profile page, In the manage display of admin/config/people/profile-types/manage/writer_profile, I added the Published name field, I set it as Format: Label; Link to the referenced entity. The field does show, and work when clicked on the User page, but I can't control where, and how, it looks without using a twig. I want to customize when, where, and how it would be displayed on the page.

I have tried in the user.html.twig:

{% for item in items %}
  {{ item.content['#user'].field_published_name.value }}
{% endfor %}

Also:

<article{{ attributes.addClass('profile') }}>
  {% if content %}
    {{- content.field_published_name -}}
  {% endif %}
</article>

On admin/config/people/accounts/display I have it set to show the 'Public Profile profiles' and 'Writer Profile profiles' format as Rendered entity.

So far it just comes up empty while showing the rest of the user's info on their profile page.

Kevin avatar
in flag
In this case the field can be set to display the 'Label' and linked to the entity. There are not enough details here, but it is otherwise fairly standard Drupal field rendering. In the user profile twig, {{ content.field_published_name }} is all that is needed, and it needs to not be hidden on Manage Display.
TomP avatar
ru flag
I used the Profile Module to make the two different profile types. On the writer's profile page, In the manage display of admin/config/people/profile-types/manage/writer_profile, I set it as Format: Label; Link to the referenced entity. I am trying to find a way to display that field in the user.html.twig that is being used to display the rest of the user's info. I tried your suggestion of {{ content.field_published_name }} and it still shows as empty. I added it like this: `<div>Author Name:<br> {{ content.field_published_name }} </div>` so I could see where it should be.
Score:0
br flag

The question is not really clear, at least for me.

If it is: "how I customize the display of an entity referenced via a entity reference field" you have at least two ways:

  1. On the child entity - taxonomy term - configure a view mode in the way you want. On the parent entity display mode choose "Rendered entity" as display mode of the field, and choose the display set up above. screenshot If necessary, you may override the twig of that "child entity" display to fine grain the customization

  2. In the twig of the parent entity you can access the "child" entity either with

{% set child_entity = content.field_reference.entity %}
{% set child_entity = drupal_entity('entity_type', entity_id') %} // using twig tweak module
TomP avatar
ru flag
Sorry for the confusion. I updated the original question in hopes of making it clearer. In the manage display of admin/config/people/profile-types/manage/writer_profile, I set it as Format: Label; Link to the referenced entity. I am trying to find a way to customize the way that field is displayed in the user.html.twig that is being used to display the rest of the user's info.
Giuseppe avatar
br flag
Have you tried to use `user` entity instead of `content`? There you should find _all_ the fields of the entity and not just the ones actually printed.
TomP avatar
ru flag
I tried user, and profile, instead of content, but neither worked. If I add the same field to the default /admin/config/people/accounts/fields, I can add it into the user.html.twig manually using: {{ content.field_published_name.0 }} This does work, but I'm trying to make it so only users with the role 'published writer' will be able to add to that field. Everything I read says this should work from the writer profile - and published name: {{ content.writer_profile.entity.field_published_name }} but it dosen't.
Giuseppe avatar
br flag
if you debug the user entity what you get? Because the path is different front the content. E.g. do `{{ dd(user)}}` and from there try to get the writer_profile or the field_published_name
TomP avatar
ru flag
I added {{ dd(user)}} to user.html.twig and just got a Twig\Error\SyntaxError. Is that what you asked me to try? Trying not to sound stupid.
TomP avatar
ru flag
I installed devel and when I look for the published name in the field info, I see it listed as profile.field_published_name. I even tried that and it didn't work. I don't think I'm doing something right to get the data when I use {{ dd(user)}} or others. I thought I just add that to the twig somewhere to display the data.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.