2

I have a number of registered users on my drupal 8 website who some of them are "active" and the others are "blocked".

I want to add a select list field inside the article content type displaying only user with the status= active (Easy job!)

Note: I have added a field to the user page (field_full_name) and I want to display the users using their "Full Name" and not their "Usernames". (Still an easy Job!)

So I did the below:

  • Create a new view to show "users"

  • Create an "entity reference" display for the view.

  • Delete the "username" field created by default

  • Add the Full name field to the view (field_full_name)

  • Add the filter field "User: User status (= Yes)"

  • Inside the Settings of the "Entity Reference" View, check the "User: Full Name" as the search field.

  • Save the view.

  • Add the view as a user reference field to the article content type by selecting the Reference method: Views: Filter by a entity reference view and for the View used to select the entities: Select the created view above.

Now, when you add a new article /node/add/article, if you try to select a user from the dropdown select list, you will notice that all users are displayed using their usernames and not their Full Names.

In other words, I like to have something like this in the select list:

  • Full Name 1.

  • Full Name 2.

  • Full Name 3.

  • Full Name 4.

but actually I am receiving this:

  • Username 1.

  • Username 2.

  • Username 3.

  • Username 4.

Also I noticed the same behaviour when I tried to create an "entity reference" view displaying all the groups that the current logged in user is a member of, by doing the below steps:

  • Create a new view and set the view to show: "Group content" of type: "All".

  • Add relationship: "The group containing the entity".

  • Add relationship: "Relates to the user entity that the group content represents".

  • Add a contextual filter: "User ID" and when the filter value is not available select: "User ID from logged in user"

  • Delete any available field created by default.

  • Add the field: "Title" (to show the title of the group)

  • Create an "entity reference" display for the view.

  • Inside the Settings of the "Entity Reference" View, check the "Group: Title" as the search field.

Now, add this view as an "entity reference" field to any content type for example "Article" and when you will add a new article /node/add/article, if you try to select a Group from the dropdown select list, you will notice also that all Groups are displayed using the username of the current logged in user and not by their Group titles.

In other words, lets say the current looged in username "Admin1" is a member of 3 groups titled: (Group Title 1, Group Title 2 and Group Title 3)

you will see that the entity reference select list field is displaying:

  • Admin1

  • Admin1

  • Admin1

Instead of:

  • Group Title 1

  • Group Title 2

  • Group Title 3

What am i doing wrong ?!

Is there something wrong with the "entity reference view" display ?!

any help ?

It is worth mentioning that if you just duplicate the "Entity reference" display to a "page" display and you visit that page, you will indeed see a list of users displayed using their Full Name or list of Groups displayed using their group titles

Thank you in advance,

2
  • I have the same issue. I try show / prefix the content type of a parent entity, but it only shows the Title or Name of the entity. I did notice that I can search on the content type but it won't be shown in the list.
    – ñull
    Dec 20, 2018 at 12:35
  • This may not be an actual answer to your questions but it worked for me. Instead of trying to change the way the select list works, I made a hook on hook_user_save and changed the display name to be the fullname instead of username. this way it will display the result you want without having to create custom views.
    – Kimimaruu
    Dec 20, 2018 at 13:02

1 Answer 1

1

You could use the realname module (which now has a D8 release). Or, you could alter the user name as displayed in Drupal:

function HOOK_user_format_name_alter(&$name, $account) { // Load the full user account. $account = \Drupal\user\Entity\User::load($account->id()); // Get the full name from field_name. $full_name = $account->get('field_name')->value; // If there's a value, set it as the new $name. if (!empty($full_name)) { $name = $full_name; } }

1
  • Thank you so much for suggesting the realname module. I had been struggling to render Full Names by linking to an Entity Reference. Feb 8, 2021 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.