Problem/Motivation

Discovered via #2977848: Update to JSON API 1.20.

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

Having multiple vocabularies causes $relatable_resource_types['parent'] to have multiple resource types. But why?

Turns out that the parent field definition does NOT restrict the parent to be in the same bundle. That means that the Politics term from the Tags vocabulary could have BMW from the Cars vocabulary as its parent term. This of course makes zero sense.

Proposed resolution

  1. Work around this in the JSON API module.
  2. Fix it in Drupal core.

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

Term entity type's parent field definition is now more precise.

Comments

Wim Leers created an issue. See original summary.

wim leers’s picture

wim leers’s picture

Status: Active » Needs review
StatusFileSize
new1.78 KB

Failing regression test.

wim leers’s picture

wim leers’s picture

+++ b/jsonapi.module
@@ -57,3 +57,21 @@ function jsonapi_entity_base_field_info(EntityTypeInterface $entity_type) {
+ * Implements hook_entity_base_field_info().

Nit: s/base/bundle/

gabesullice’s picture

Status: Needs review » Reviewed & tested by the community

RTBC on the assumption #4 will fail.

gabesullice’s picture

Status: Reviewed & tested by the community » Needs work

#4 didn't fail :_(

wim leers’s picture

Status: Needs work » Needs review

Ah, right, because the parent field is completely broken in 8.5.x. Only in 8.6.x (since #2543726: Make $term->parent behave like any other entity reference field, to fix REST and Migrate support and de-customize its Views integration) does it work sanely.

#4 should only fail on 8.6.x. (And #5 should pass.) Queued test against 8.6. Now there are the expected test failures :)

gabesullice’s picture

Status: Needs review » Reviewed & tested by the community

Makes sense. #4 does fail against 8.6.

wim leers’s picture

Status: Reviewed & tested by the community » Fixed
StatusFileSize
new698 bytes

Fixed CS violation on commit.

  • Wim Leers committed e44cfd9 on 8.x-1.x
    Issue #2977879 by Wim Leers, gabesullice: Regression in #2940339: when...
  • Wim Leers committed 94e2778 on 8.x-2.x
    Issue #2977879 by Wim Leers, gabesullice: Regression in #2940339: when...

  • gabesullice committed 52baa89 on 8.x-1.x
    Issue #2980593 by Wim Leers, gabesullice: Follow-up for #2977879: remove...
  • gabesullice committed 8f9be78 on 8.x-2.x
    Issue #2980593 by Wim Leers, gabesullice: Follow-up for #2977879: remove...

Status: Fixed » Closed (fixed)

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

rpayanm’s picture

Project: JSON:API » Drupal core
Version: 8.x-1.19 » 8.9.x-dev
Component: Code » jsonapi.module

Moving to Drupal core's issue queue.

I'm working on https://www.drupal.org/project/drupal/issues/3122113