1

I have an entity called "lawyers."

And another entity refers to lawyers.

The problem is that when searching the reference field with the autocompletion system many repeated names appear:

Pablo
Pablo
Pablo
Pablo

I need the reference field to be able to show the surnames of that person so that it turns out to be

Pablo Martínez
Paglo Gutirerrez
Pablo Iglesias
Pablo López

how can I do this?

Javier
  • 395
  • 3
  • 18

1 Answers1

0

You will have to create a Entity Reference View to use as handler for doing the autocomplete lookup. Then you can add additional fields (such as last name) to the autocomplete results. This article outlines that process well enough:

https://www.cmsminds.com/blog/entity-reference-entity-reference-view-in-drupal-8/

If the field is a base field and is not available on the Manage form Display page, you will have to modify the entity class Lawyer::baseFieldDefinitions function. Specifically, you need to change the handler and set the form display settings. In your BaseFieldDefinition::create call:

->setSetting('handler', 'default')

Needs to change to this:

->setSetting('handler', 'views')
->setSetting('handler_settings', [
  'view' => [
    'view_name' => 'name_of_entity_reference_view',
    'display_name' => 'name_of_view_display',
  ],
])
->setDisplayOptions('form', [
  'type' => 'entity_reference_autocomplete',
  'weight' => 2,
  'settings' => [
    'match_operator' => 'CONTAINS',
    'size' => '60',
    'autocomplete_type' => 'tags',
    'placeholder' => '',
  ],
])

Alternatively, if you want to make base fields available in the UI, you can use this line to make the field available in the form display settings ui (and then export your form display settings as config:

->setDisplayConfigurable('form', TRUE);