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');
}
}
| Comment | File | Size | Author |
|---|---|---|---|
| #32 | interdiff_25-32.txt | 3.03 KB | niklan |
| #32 | core-3071803-32.patch | 2.97 KB | niklan |
| #25 | interdiff_19-25.txt | 638 bytes | niklan |
| #25 | core-3071803-25.patch | 5.66 KB | niklan |
| #21 | core-3071803-19-test-only.patch | 3.11 KB | niklan |
Comments
Comment #2
mheip commentedComment #3
mheip commentedComment #4
mheip commentedComment #6
mheip commentedComment #7
joachim commentedGood catch, but this needs tests.
IIRC there is a bundle-less entity type defined in the test modules?
Using hasKey() would be better here.
Nitpick: variable should be in snake case.
Comment #14
a.dmitriiev commentedI think the problem is a bit deeper. Configuration value
$configuration['target_bundles']should not be an array at all.There is a comment just above this condition
So actually the problem is that
$configuration['target_bundles']is array instead ofNULLI traced this problem down to
buildConfigurationFormmethod:So the form sets the target_bundles as array if the entity doesn't support bundles, but instead it should use
NULLComment #15
a.dmitriiev commentedComment #16
smustgrave commentedMoving back to NW for the tests.
Comment #17
niklanI don't think the problem is in array. The value for target_bundles is set to NULL if it's an empty array in
\Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::validateConfigurationFormAlso,
::buildEntityQuery()only tries to getbundlekey only iftarget_bundlesis an array and not an empty one. The only way to reproduce this bug, intentionally providing at least one bundle for bundleless entity.I'm attaching a patch with test that shows that everything is works properly with
target_bundles:[],NULLand without value at all with User entity, which has no bundle key.@mheip since you are mentioning a custom entity, maybe it had bundle key when you installed it? Are you sure that the installed entity schema for your custom entity doesn't have a
bundlekey? Maybe a field, that is using references, was configured when your entity was having a bundle key?Comment #18
andypostTest only patch doesn't catch
Comment #19
niklanAs a workaround, to make debug this problem easier, we can throw an exception in case when
target_bundlesare provided, but entity doesn't support bundles.The test-only patch shows the OP problem, but this is unexpected. Most likely, the entity schema is broken in that case or a field widget is misconfigured.
Comment #20
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #21
niklanRe-upload fixed test only patch.
Comment #23
niklanTest in #21 shows exact problem from OP
Drupal\Core\Entity\Query\QueryException: '' not foundbut it intentionally provides invalid information. A patch from #19 proposes to throw an exception in that case with explanation.
Comment #24
smustgrave commentedRemoving tests tag as they were added.
By adding throw new UnsupportedEntityTypeDefinitionException($message); the SelectionInterface docs will have to be updated to include a @throws tag.
Comment #25
niklanAdded @throws PHPDoc.
Btw, I'm not sure that exception itself is suitable in that case and properly chosen.
Comment #26
smustgrave commentedThanks!
Comment #28
niklanSeems like this error was caused by other code changes and patch + tests are working now. Switch back to RTBC
Comment #29
larowlanFrom #17
So it sounds to me that this might be a support request rather than a bug. Or failing that something where we can improve developer experience, which would make it a task.
Can you confirm this is the case - i.e. it can only be reproduced by doing something unexpected.
Also, I note the test is using the `default` plugin for the user entity, which is not really how this works in core, it uses the `default:user` plugin, which has different logic.
The default plugin doesn't technically exist, because
Drupal\Core\Entity\Plugin\Derivative\DefaultSelectionDeriverprovides a `default:{entity_type_id}` version for every entity type.Comment #30
niklan@larowlan As proposed in #3071803-25: DefaultSelection::buildEntityQuery requires entities to have bundle key., we can throw an exception in that cases. But still, it's not clear, should we do it or not, because this sounds like a very edge case scenario.
Anyway, I can update tests to use
default:userhandler, but it doesn't change the tests results and outcome, because the fix is applied inDefaultSelectionand will affect all derivatives. So I leave them for now, before there will be a decision what to do.Comment #31
larowlan@Niklan I think we should just add the exception, without the test.
A DX improvement is worth adding, but we don't need the overhead of the test.
Flagging as a task.
Comment #32
niklanThere is a patch without test.
I still not sure that exception type was chosen properly. This needs a review.
Comment #33
smustgrave commentedIf the question is if UnsupportedEntityTypeDefinitionException is a good, I think so? Will see what the committer says.
Comment #34
larowlanCan we get a change record here - as we're throwing a new exception.
Fine to self RTBC after that.
Comment #35
smustgrave commentedCreated CR.
Comment #36
niklanI've updated a little bit CR to make it clear that it only throws an exception when entity doesn't have bundle support (entity key) and bundles passed in settings.
Comment #37
larowlanComment #39
larowlanCommitted to 10.1.x and published the change record.
Thanks folks