I am using an Entity Reference field to refer to users in another content type. The default widgets for the field use the User Name as the label for the dropdown, but I'd like to change that to another field from the user profile entity (ideally combining 'first name' and 'last name' fields). I've looked at Entity Reference Views, but it isn't released and I could not get it to work based on available docs.
2 Answers
Entityrefence 7.x-1.x-dev allows you to use results from a View in the select box, which will allow you to do what you need.
- create/edit a view, add a new display of type "Entity Reference" to it, setting properly the fields you want to be shown;
- edit the entityreference field, find "entity selection mode", set it to "views: filter by an entity reference view" then select the proper "view used to select the entities" option created on previous step.
-
This sounded like it might work, and the current dev version of entityreference indeed does have a "Entity Reference" display type exposed in views, but the functionality unfortunately seems broken at the moment. Adding a display of this type creates an empty screen. Aug 3, 2012 at 20:10
-
It has taken me a while to understand how to build this Entity Reference within D7. If it helps anyone, I will describe how I think it can be built here.
In my case, I am creating a Project (node entity) and relating it to a Primary Investigator PI (profile2 entity). It is a 2-way relationship, but we will be adding PI to the Project (instead of adding a Project to the Profile, at least initially). So, Project content type will contain an Entity Reference to Profile.
The least confusing way, in my opinion, is to create the view for the entity first. Add an Entity Reference display to any existing or new view (any best practice advice here?). The Format should be Entity Reference List and show Inline fields. The Filter criteria should list the type of Entity you are referring to - in my case Profile. Add more Fields that you would want to appear for the user to select from - in my case Last Name and First Name. Then go back to the Settings for Entity Reference List and add those items to the Search fields list. If desired, set up Sort Criteria for your list, which should now show up in your Preview. Save this View.
In the Project content type, add a new field referring to the PI profile [label: prof_ref_pi, field type: Entity Reference, Widget: Select list (have not been able to get Autocomplete to work as of yet)] and Save. Target type in my case is Profile. Choose Mode: Views: Filter by an entity reference view (I could not figure out how to get the Simple filter to show field values but Views does work). Select the Entity Reference display in the View you created earlier, and Save. Save your Content Type.
Go to Content and create/edit a piece of content - in my case, Project. The reference field there should have a drop-down with the View information you specified.
To use in a View, for example to show a Project and its PI, choose to create the view on a Content of Projects using Fields and add the Relationship Entity Reference - in my case, project_ref_pi. Then, in Fields, choose from the Projects fields and those items from the Profile fields using the relationship.