Earlier/1.x versions of JSONAPI seemed to work well with dynamic_entity_reference but after 2.x upgrade it stopped. Because 2.x looks for a ResourceType via a statically defined target_type on an entity in its definition while the target_type is not yet defined/or unable to infer for a dynamic_entity_reference field before the content entity is actually created.
I don't think it is something we can fix in the dynamic_entity_reference module but add few lines of more code how JSONAPI could look for a ResourceType of a dynamic field.
Here is the patch. I am sure it needs some improvement but seems to be working for me. Any suggestions will be helpful
Thanks
| Comment | File | Size | Author |
|---|---|---|---|
| #28 | 3027430-28.patch | 8.35 KB | gradywring |
| #27 | 3027430-27.patch | 8.33 KB | gradywring |
| #25 | interdiff-3027430-23-24.txt | 706 bytes | garphy |
| #24 | 3027430-24.patch | 8.12 KB | garphy |
| #15 | jsonapi_DER_normalization_support-8.x-2.4.patch | 7.37 KB | abrar_arshad |
Issue fork drupal-3027430
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
abrar_arshad commentedComment #3
abrar_arshad commentedComment #4
wim leersI'm surprised that A) this broke, B) nobody reported that all this time, especially since DER author @jibran has been quite active! Will look at this tomorrow.
Thank you so much for the patch, that's super helpful! 👍 🙏
Comment #5
jibranhaha, there is
DynamicEntityReferenceItemNormalizerin DER and I thought that would be enough but it only supports hal.I'm happy to fix it in DER which we can as per #2926507-39: Discourage @FieldType-level normalizers, encourage @DataType-level normalizers, to strengthen the API-First ecosystem. @Wim Leers, do you think it's worth waiting for core to fix #2926507? And as an interim fix, we can update JSON:API? Personally, I'd like to see
Drupal\Core\Entity\Plugin\DataType\EntityReferencenormalizer implemented first so that DER can extend that.Comment #6
wim leersWe discussed this during yesterday's API-First Initiative meeting.
I indeed forgot that DER never worked in JSON:API.
If we want DER to work in JSON:API, it will need to provide a
@DataType-level normalizer. To let it coexist with its current@FieldType-level normalizer (\Drupal\dynamic_entity_reference\Normalizer\DynamicEntityReferenceItemNormalizer), it could restrict that@DataType-level normalizer to just theapi_jsonformat for now.Then in the future, once the
@FieldType-level normalizer becomes obsolete even in core, i.e. after core's@FieldType-level entity reference normalizer (\Drupal\hal\Normalizer\EntityReferenceItemNormalizer, which is HAL-only, because DER only supports HAL today) has been removed/deprecated in favor of a@DataType-level normalizer, that restriction toapi_jsoncould be lifted.That's reasonable. But there's no issue yet to do that (I could create that), but more importantly there are lots of high priorities. I'm not sure it'd happen before 8.7, although I'd gladly help to make that happen of course. So if you choose to wait for that, it could end up taking a while.
Note that this would also help solve #2844946: Add DynamicEntityReferenceItemNormalizer for serialization module.
Comment #7
wim leersJust marked #3027430: Dynamic Entity Reference normalization support as a duplicate of this. People are asking about this.
Comment #8
steveworley commentedThanks for pointing this out @wim.
I understand that its just a normaliser that isn't picking this up and serialising correctly, I still think the modules implementation for discerning these fields might be too greedy given the error I was receiving. For reference the error is:
"detail": "`field_ref_dynamic` is not a valid relationship field name. Possible values: field_ref_dynamic."When including a DER field the output says that you should be able to use the field. All you would need to do to replicate this is:
- Define a field that extends DataReferenceTargetDefinition
- Don't add target_type in the field
This triggers the following flow:
- getRelatableResourceTypesByField will return your field but it will be an empty array
- resolveInternalIncludePath checks to see if the key returned is empty
I think it's just a case that getRelatableResourceTypesByField should do more checking and resolveInternalIncludePath should maybe rely on the getRelatableResourceTypesByField to check for the field. If my field doesn't have the right configuration it shouldn't show that I can request it.
Comment #9
wim leersAha! That makes a a ton of sense. Reopening #3029090: #3029090-4: Dynamic Entity Reference: support discovering relatable resource types.
Comment #10
wim leersI also referenced the wrong issue in #7 😅🤘
Comment #11
wim leersThis issue is about DER support minus the infrastructure support (that is covered by #3029090: Dynamic Entity Reference: support discovering relatable resource types).
Comment #12
grimreaperHello,
Is there a version for Drupal Core of the patch?
I want to test if with this patch there will be a "related" link on DER relationship.
Comment #13
wim leersIt should be easy to apply this patch using
patch -p1 < patchfilefrom thecore/modules/jsonapidirectory.Comment #14
jibranComment #15
abrar_arshad commentedHere is the patch for 8.x-2.4 until we have a clean fix
Comment #16
abrar_arshad commentedPatch for core: 8.7.x
Comment #17
benjy commentedPatch in #16 works for me, what's the way forward here?
Comment #18
wim leersSee #8. This is postponed on #3029090-12: Dynamic Entity Reference: support discovering relatable resource types.
Comment #19
benjy commentedI re-rolled #16 against 8.7.7 as i'm using this patch and needed to upgrade core. Hopefully it's of use to someone else.
Comment #20
wim leers#3029090: Dynamic Entity Reference: support discovering relatable resource types is itself postponed, so bumping.
Comment #21
wim leersComment #22
garphyComment #23
garphyRerolled on 8.8.x
Comment #24
garphyPrevious patch throw an Error if the request try to include a non-existent relationship because there's no actual value to try to guess of which type it is.
Protecting this so that the "right" HTTP 400 response can be returned.
Comment #25
garphyComment #27
gradywring commented@garphy Thanks for your patch! I found one small bug in it around checking for an empty
$target_typein IncludeResolver. The$target_typeneeds to be reset after each iteration if the field is dynamic. I'm including a patch.Comment #28
gradywring commentedHere's an updated patch based on @garphy's work to allow multiple resource types to be discovered from a DER field for possible includes in a jsonapi request.
Comment #32
ptmkenny commentedComment #34
wim leers#3057545: ResourceTypeRepository wrongly assumes that all entity reference fields have the setting "target_type" landed!
Comment #35
mcanada commentedis this still relevant for
???