ResourceTypeRepository wrongly assumes that all entity reference fields have the setting "target_type"

Created on 28 May 2019, over 5 years ago
Updated 4 July 2023, over 1 year ago

Problem/Motivation

\Drupal\jsonapi\ResourceType\ResourceTypeRepository::getRelatableResourceTypesFromFieldDefinition() directly reads the setting target_type from the field definition, however not all reference fields have this setting. A concrete example is the dynamic_entity_reference module, which allows for referencing multiple entity types and it lacks the setting target_type. See https://git.drupalcode.org/project/dynamic_entity_reference/blob/8.x-2.x...

Proposed resolution

Define a new interface, with a method named getReferenceableBundles(FieldDefinitionInterface $definition), which is then used to retrieve the entity types and bundles being referenced by that particular item class in a single API.

This is a pathway to effectively solving #3262385: Add an API for general entity reference field types β†’ by allowing code to introspect the field item class and determine if it implements this new interface.

Remaining tasks

Review and possibly some new tests?

User interface changes

None.

API changes

New interface.

Data model changes

None.

Release notes snippet

TBD: Draft Change Records needed.

Original IS snippets

Option 1:
Add a new method getReferenceableEntityTypes() on the interface \Drupal\Core\Field\EntityReferenceFieldItemListInterface(), which could be overwritten by \Drupal\dynamic_entity_reference\Plugin\Field\FieldType\DynamicEntityReferenceFieldItemList().

jsonapi could then call the new method instead of assuming the setting target_type always exists when there is a property of the type \Drupal\Core\TypedData\DataReferenceTargetDefinition.

Option 2:
A more scalable and independent option might be an approach of adding the referenceable entity types configuration to the DataReferenceTargetDefinition property. DynamicEntityReferenceItem defines two such properties and would have to do it for both, but this should not be a problem or maybe only one of them could list the referenceable entity types configuration.

jsonapi could then retrieve the referenceable entity types configuration directly from the DataReferenceTargetDefinition properties instead of assuming there is a field definition setting target_type.

------

Option 1 does not work because the item list might be used by multiple item implementations.
Option 2 does not work because the properties are retrieved based on the field storage definition and not on the field definition for bundle fields.

This led to Option 3 (copied to proposed solution, above.)

πŸ› Bug report
Status

Fixed

Version

11.0 πŸ”₯

Component
JSON APIΒ  β†’

Last updated 6 days ago

Created by

πŸ‡©πŸ‡ͺGermany hchonov πŸ‡ͺπŸ‡ΊπŸ‡©πŸ‡ͺπŸ‡§πŸ‡¬

Live updates comments and jobs are added and updated live.
  • Contributed project blocker

    It denotes an issue that prevents porting of a contributed project to the stable version of Drupal due to missing APIs, regressions, and so on.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024