I came accross this when trying to reference a custom entity without a bundle key present using a contrib module (entityreference_view_mode).
When trying to use the autocomplete function, DefaultSelection::buildEntityQuery is called.
When trying to use the autocomplete function, I got following error:
PHP message: Uncaught PHP Exception Drupal\\Core\\Entity\\Query\\QueryException: "'' not found
I traced it down to this piece of code in Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection.
if (is_array($configuration['target_bundles'])) {
// If 'target_bundles' is an empty array, no bundle is referenceable,
// force the query to never return anything and bail out early.
if ($configuration['target_bundles'] === []) {
$query->condition($entity_type->getKey('id'), NULL, '=');
return $query;
}
else {
$query->condition($entity_type->getKey('bundle'), $configuration['target_bundles'], 'IN');
}
}
$configuration['target_bundles'] contained a value (perhaps this is an issue with the contrib module). But because my custom entity contained no bundle key in the entity keys, the query caused an error.
I have fixed this by checking if the entity has a bundle key present, and only then adding the condition, in the patch attached.
if (is_array($configuration['target_bundles'])) {
// If 'target_bundles' is an empty array, no bundle is referenceable,
// force the query to never return anything and bail out early.
if ($configuration['target_bundles'] === []) {
$query->condition($entity_type->getKey('id'), NULL, '=');
return $query;
}
$bundleKey = $entity_type->getKey('bundle');
if ($bundleKey) {
$query->condition($bundleKey, $configuration['target_bundles'], 'IN');
}
}