I have a custom content type which uses a user entity reference field to display user information. However, instead of just displaying a generic username on the final page after the reference to the user is made during node creation, what I'd like is to display the full name, which consists of the First, Middle, and Last Name fields from the respective user account being referenced. From what little I'm able to understand about view modes, a view mode could be created that allows this. Thing is, I have no idea how they're actually USED.

I've made a user view mode named "Full Name." Okay? Now what? How would I use this to achieve my final end result? Do I tap into that from something like a hook and once keyed off of it, just reduce the fields being displayed somehow? If so, which hook is it that I'd use in Drupal 9+?

Comments

johnpitcairn’s picture

No code should be needed.

Configure the fields you want in "manage display" for the user. You may need to enable the custom view mode (from the default view mode) before you can configure it separately. It will appear as a tab when enabled.

Then for your entity reference field in the parent entity manage display, choose "rendered entity" and select the appropriate user view mode.

wolf_22’s picture

Okay, after revisiting your instructions and looking at the "Disabled" section of the display mode I made, I was able to get it to work! Awesome! (Previously, I had only focused on hiding the label rather than moving the field down to that "Disabled" section of the display mode edit screen.)

Thanks a bunch, John! Working like a charm now!

johnpitcairn’s picture

Go to Manage display for your content type:
/admin/structure/types/manage/CONTENT_TYPE/display

At the bottom: Custom display settings - enable your new display mode for this content type.

Now you can configure it via the new tab at top:
/admin/structure/types/manage/CONTENT_TYPE/display/VIEW_MODE

wolf_22’s picture

Got it to work, John. I applied your instructions to the display mode over on the user account fields interface and then referenced that display mode over on my custom content type for the one field. Everything is working now. Thank you so much!