I'm trying to create a Entity Reference autocomplete field against users. Instead of using user names, I'd like to use another field in the user entity ("field_full_name") to search against. (I've already solved the issue of altering the returned user to display the correct field name - using hook_user_format_name_alter)

I did manage to get user searches working by user name by changing "target_type: node" to "target_type: user", but I'm a bit baffled as to how:

selection_handler: default
selection_settings: { }

are intended to work if I want to have a non-standard user search.

I did manage to get a manual form element to work via the views_autocomplete_api module, but it felt like quite a hack. Ideally I'd like to be able to use a entity reference view to be able to pull up the data, but at this point I'm about ready to write my own selection handler.

I'm prepared to do the legwork on this problem and document the solution if it's left as an exercise to the reader, but I'd like to get nudged in the right direction. Can this be done with just YAML Form and views, or do I need to build on my own selection_handler, or is there another solution I'm not considering?

Thanks in advance!

Comments

TrevorBradley created an issue. See original summary.

jrockowitz’s picture

You should be able set up an Entity reference views display and use it. See https://www.ostraining.com/blog/drupal/entity-reference-view/

Are you using the YAML Form UI? If you use the UI it should correctly setup the selection handler and settings.

trevorbradley’s picture

Category: Support request » Feature request

I am using the YAML Form UI. I think I may see my confusion. I was using Entity Autocomplete, rather than Entity Select.

I see the "Reference Method" pulldown in the "Entity Reference Settings" pulldown in the "Entity Select" element, but not in "Entity Autocomplete" element. The entity reference view appears to work just fine in Entity Select.

"Entity Autocomplete" does not have an "Entity Reference Settings" pulldown. It also only appears possible to change what you're referring to using the "Custom Settings" section.

If I look at the Source (YAML) tab, and manually copy the entity_select parameters into entity_autocomplete's custom parameters... I can get it to work, and it works great!

  user_2:
    '#type': entity_autocomplete
    '#title': 'User 2'
    '#target_type': user
    '#selection_handler': views
    '#selection_settings':
      view:
        view_name: user_reference_2
        display_name: entity_reference_1
        arguments: {  }

It looks as if Entity Reference Settings is missing from Entity Autocomplete in the YAML Form UI. It may be time to flip this to a feature request.

trevorbradley’s picture

Title: Understanding entity reference "selection_handler", "selection_settings" » Entity Autocomplete missing Entity Reference Settings submenu in YAML Form UI
trevorbradley’s picture

Added corroborating screen shots. Entity Autocomplete doesn't have a "Entity Reference Settings" submenu, but Entity Select does.

jrockowitz’s picture

Status: Active » Needs review
StatusFileSize
new1.75 KB

  • jrockowitz committed a04bf91 on 8.x-1.x
    Issue #2810199 by jrockowitz, TrevorBradley: Entity Autocomplete missing...
jrockowitz’s picture

Status: Needs review » Fixed

Fixed! Please download the latest dev release.

trevorbradley’s picture

Tested. Looks good to me. Nice work jrockowitz!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.