0

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.

2
  • 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.
    – Kevin
    Oct 22, 2022 at 17:13
  • 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.
    – TomP
    Oct 22, 2022 at 18:20

1 Answer 1

0

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
6
  • 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.
    – TomP
    Oct 23, 2022 at 16:13
  • 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.
    – Giuseppe
    Oct 25, 2022 at 7:19
  • 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.
    – TomP
    Oct 25, 2022 at 18:36
  • 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
    – Giuseppe
    Oct 25, 2022 at 19:07
  • 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
    Oct 25, 2022 at 22:46

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.