#2940339: Port reference field support for non-empty entity reference fields not pointing to an entity from #2543726 added this:

    if ($target_entity === NULL) {
      $host_entity = $parent->getHostEntity();
      $relatable_resource_types = $resource_type_repository->get(
        $host_entity->getEntityTypeId(),
        $host_entity->bundle()
      )->getRelatableResourceTypes()[$parent->getPropertyName()];
      if (count($relatable_resource_types) !== 1) {
        throw new \RuntimeException('Relationships to virtual resources are possible only if a single resource type is relatable.');
      }

In certain circumstances, entity reference fields using the views reference handler causes $relatable_resource_types to be set as an array containing every bundle of the given entity type, causing the above RuntimeException to be thrown.

This has occurred in a case where the view in use by the handler did have a condition limiting the options to a single bundle.

Interestingly, the exception does not get thrown in all cases. So far, I've only seen it when normalizing a single entity for which the given field is empty. Selecting a value in the is field fixes the issue and it remains fixed after unsetting the value and saving the entity again.

For now I have worked around the issue by implementing hook_entity_bundle_field_info_alter() and setting the target_bundles handler setting for the given field / entity.

Interestingly, when the field does have a value, it is represented in the normalized output with a uuid for it's id, i.e. it does not appear to be a relationship to "virtual" resources.

Comments

PQ created an issue. See original summary.

pq’s picture

Issue summary: View changes

Fixes to issue summary.

wim leers’s picture

Issue tags: +API-First Initiative

Wow, impressively obscure edge case that you found there! 😃

Can you also reproduce this with JSON:API 2.x?

gabesullice’s picture

Status: Active » Postponed (maintainer needs more info)
pq’s picture

Ah yes, obscure edge cases are a speciality of mine 😃!

I'll try to get steps to reproduce on 2.x as soon as I get a spare mo.

wim leers’s picture

Ah yes, obscure edge cases are a speciality of mine 😃!

Hahahah 😂

I'm glad that we're both able to laugh about it at least!

And thank you so much for taking the time to report this in the first place, and looking into reproducing on 2.x when you find the time! 👍 🙏

wim leers’s picture

@PQ: any news? :)

pq’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)

Hi Wim,

I've tried reproducing on a bare install with 2.x-dev, 1.x-dev and 1.23 (the version installed on the site the issue exhibited on) without any luck so far. If it's ok with you, I'll close this as can't reproduce for now as it could well be down to a highly esoteric set of local conditions on the site in question. If anyone else encounters the same issue then it could be reopened.

wim leers’s picture

👍 Thanks!