I have a custom entity defined by hook_entity_info(). The entity is working fine. I added an entity reference field to a node bundle that references that custom entity. I'm finding that the autocomplete widget to select an entity of the custom type doesn't work properly. It shows a list of entities of that custom type that don't match what I'm typing.

I found that you have to create your own selection handler for this to work properly. I'll post an answer below.

Accepted answer

In order for the autocomplete to work, you have to extend EntityReferenceSelectionHandlerGeneric. The steps are simple:

  1. Create a file named: EntityReferenceSelectionHandlerGeneric_ENTITYTYPE.inc and put it inside the module that creates the custom entity, inside a folder such as plugins. Replace ENTITYTYPE with the name of your custom entity. In my case, _student, as in EntityReferenceSelectionHandlerGeneric_student.inc
  2. In that file create a class that extends EntityReferenceSelectionHandlerGeneric, as in:
class EntityReferenceSelectionHandlerGeneric_student extends EntityReferenceSelectionHandlerGeneric{
  protected $label_key = 'name';
}
  1. Add this class to your module's autoload function, as in
/**
 * Implements hook_autoload_info().
 */
function student_autoload_info() {
  return array(
    // Entity reference selection plugin.
    'EntityReferenceSelectionHandlerGeneric_student' => 'plugins/EntityReferenceSelectionHandlerGeneric_student.inc',
  );
}
  1. Clear caches.

In the class above, the trick is to provide a value for the property $label_key. This value must match the value for ['entity_keys']['label'] in your entity info.

Comments

In order for the autocomplete to work, you have to extend EntityReferenceSelectionHandlerGeneric. The steps are simple:

  1. Create a file named: EntityReferenceSelectionHandlerGeneric_ENTITYTYPE.inc and put it inside the module that creates the custom entity, inside a folder such as plugins. Replace ENTITYTYPE with the name of your custom entity. In my case, _student, as in EntityReferenceSelectionHandlerGeneric_student.inc
  2. In that file create a class that extends EntityReferenceSelectionHandlerGeneric, as in:
class EntityReferenceSelectionHandlerGeneric_student extends EntityReferenceSelectionHandlerGeneric{
  protected $label_key = 'name';
}
  1. Add this class to your module's autoload function, as in
/**
 * Implements hook_autoload_info().
 */
function student_autoload_info() {
  return array(
    // Entity reference selection plugin.
    'EntityReferenceSelectionHandlerGeneric_student' => 'plugins/EntityReferenceSelectionHandlerGeneric_student.inc',
  );
}
  1. Clear caches.

In the class above, the trick is to provide a value for the property $label_key. This value must match the value for ['entity_keys']['label'] in your entity info.

Does the Views selection handler already work or do you have to extend that too?

Does the Views selection handler already work or do you have to extend that too?

I would assume it does, but I haven't tried that. I'll test that later. BTW, this is the same behavior as in D7, even thought the generic class is different and that property doesn't exist in the current D7 version.

Herb, I've tested both the Autocomplete and the Selection widgets using the selection mode "View" and a ER View of the custom entity. Both widgets work fine out of the box. The only one that requires the steps I posted above is the Autocomplete when the Entity selection mode is "Simple" (not "Views: XX").