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