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');
      }
    }

Comments

mheip created an issue. See original summary.

mheip’s picture

Version: 8.7.x-dev » 8.8.x-dev
StatusFileSize
new882 bytes
mheip’s picture

Issue summary: View changes
mheip’s picture

Status: Active » Needs review

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

mheip’s picture

Assigned: mheip » Unassigned
joachim’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Good catch, but this needs tests.

IIRC there is a bundle-less entity type defined in the test modules?

+++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php
@@ -445,8 +445,11 @@ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS')
+      $bundleKey = $entity_type->getKey('bundle');

Using hasKey() would be better here.

Nitpick: variable should be in snake case.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

a.dmitriiev’s picture

I 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

if (is_array($configuration['target_bundles'])) {
// If 'target_bundles' is NULL, all bundles are referenceable, no further
// conditions are needed.

So actually the problem is that $configuration['target_bundles'] is array instead of NULL

I traced this problem down to buildConfigurationForm method:

    if ($entity_type->hasKey('bundle')) {
      // ......
    }
    else {
      $form['target_bundles'] = [
        '#type' => 'value',
        '#value' => [],
      ];
    }

So the form sets the target_bundles as array if the entity doesn't support bundles, but instead it should use NULL

a.dmitriiev’s picture

Status: Needs work » Needs review
StatusFileSize
new635 bytes
new635 bytes
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Moving back to NW for the tests.

niklan’s picture

Status: Needs work » Needs review
StatusFileSize
new2.51 KB

I 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::validateConfigurationForm

    // If no checkboxes were checked for 'target_bundles', store NULL ("all
    // bundles are referenceable") rather than empty array ("no bundle is
    // referenceable" - typically happens when all referenceable bundles have
    // been deleted).
    if ($form_state->getValue(['settings', 'handler_settings', 'target_bundles']) === []) {
      $form_state->setValue(['settings', 'handler_settings', 'target_bundles'], NULL);
    }

Also, ::buildEntityQuery() only tries to get bundle key only if target_bundles is 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: [], NULL and 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 bundle key? Maybe a field, that is using references, was configured when your entity was having a bundle key?

andypost’s picture

Status: Needs review » Needs work

Test only patch doesn't catch

niklan’s picture

Status: Needs work » Needs review
StatusFileSize
new4.05 KB
new4.87 KB

As a workaround, to make debug this problem easier, we can throw an exception in case when target_bundles are 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.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new1.97 KB

The 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.

niklan’s picture

Status: Needs work » Needs review
StatusFileSize
new3.11 KB

Re-upload fixed test only patch.

Status: Needs review » Needs work

The last submitted patch, 21: core-3071803-19-test-only.patch, failed testing. View results

niklan’s picture

Status: Needs work » Needs review

Test in #21 shows exact problem from OP Drupal\Core\Entity\Query\QueryException: '' not found
but it intentionally provides invalid information. A patch from #19 proposes to throw an exception in that case with explanation.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests

Removing 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.

niklan’s picture

Status: Needs work » Needs review
StatusFileSize
new5.66 KB
new638 bytes

Added @throws PHPDoc.

Btw, I'm not sure that exception itself is suitable in that case and properly chosen.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 25: core-3071803-25.patch, failed testing. View results

niklan’s picture

Status: Needs work » Reviewed & tested by the community

Seems like this error was caused by other code changes and patch + tests are working now. Switch back to RTBC

larowlan’s picture

Status: Reviewed & tested by the community » Needs review

From #17

The only way to reproduce this bug, intentionally providing at least one bundle for bundleless entity.

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\DefaultSelectionDeriver provides a `default:{entity_type_id}` version for every entity type.

niklan’s picture

@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:user handler, but it doesn't change the tests results and outcome, because the fix is applied in DefaultSelection and will affect all derivatives. So I leave them for now, before there will be a decision what to do.

larowlan’s picture

Category: Bug report » Task
Status: Needs review » Needs work

@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.

niklan’s picture

Status: Needs work » Needs review
StatusFileSize
new2.97 KB
new3.03 KB

There is a patch without test.

I still not sure that exception type was chosen properly. This needs a review.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

If the question is if UnsupportedEntityTypeDefinitionException is a good, I think so? Will see what the committer says.

larowlan’s picture

Status: Reviewed & tested by the community » Needs review

Can we get a change record here - as we're throwing a new exception.

Fine to self RTBC after that.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Created CR.

niklan’s picture

I'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.

larowlan’s picture

Priority: Normal » Minor
Issue tags: +Bug Smash Initiative

  • larowlan committed 4c3625e8 on 10.1.x
    Issue #3071803 by Niklan, a.dmitriiev, mheip, smustgrave, joachim:...
larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 10.1.x and published the change record.

Thanks folks

Status: Fixed » Closed (fixed)

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