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

Issue fork drupal-3027430

Command icon 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

abrar_arshad created an issue. See original summary.

abrar_arshad’s picture

Title: dynamic_entity_reference support in JSAONAPI » dynamic_entity_reference support in jSONAPI
abrar_arshad’s picture

Title: dynamic_entity_reference support in jSONAPI » dynamic_entity_reference support in JSONAPI
wim leers’s picture

Title: dynamic_entity_reference support in JSONAPI » dynamic_entity_reference support
Issue tags: +API-First Initiative

I'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! 👍 🙏

jibran’s picture

I'm surprised that A) this broke, B) nobody reported that all this time

haha, there is DynamicEntityReferenceItemNormalizer in 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\EntityReference normalizer implemented first so that DER can extend that.

wim leers’s picture

We 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 the api_json format 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 to api_json could be lifted.

Personally, I'd like to see Drupal\Core\Entity\Plugin\DataType\EntityReference normalizer implemented first so that DER can extend that.

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.

wim leers’s picture

Just marked #3027430: Dynamic Entity Reference normalization support as a duplicate of this. People are asking about this.

steveworley’s picture

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

wim leers’s picture

Aha! That makes a a ton of sense. Reopening #3029090: #3029090-4: Dynamic Entity Reference: support discovering relatable resource types.

wim leers’s picture

wim leers’s picture

Title: dynamic_entity_reference support » Dynamic Entity Reference normalization support

This issue is about DER support minus the infrastructure support (that is covered by #3029090: Dynamic Entity Reference: support discovering relatable resource types).

grimreaper’s picture

Hello,

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.

wim leers’s picture

It should be easy to apply this patch using patch -p1 < patchfile from the core/modules/jsonapi directory.

jibran’s picture

Issue tags: +DER issue
abrar_arshad’s picture

Here is the patch for 8.x-2.4 until we have a clean fix

abrar_arshad’s picture

StatusFileSize
new7.99 KB

Patch for core: 8.7.x

benjy’s picture

Patch in #16 works for me, what's the way forward here?

wim leers’s picture

Title: Dynamic Entity Reference normalization support » [PP-1] Dynamic Entity Reference normalization support
Status: Needs review » Postponed
benjy’s picture

StatusFileSize
new8 KB

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

wim leers’s picture

Project: JSON:API » Drupal core
Version: 8.x-2.0 » 8.8.x-dev
Component: Code » jsonapi.module
wim leers’s picture

Title: [PP-1] Dynamic Entity Reference normalization support » [PP-2] Dynamic Entity Reference normalization support
garphy’s picture

Issue tags: +Needs reroll
garphy’s picture

Issue tags: -Needs reroll
StatusFileSize
new8.05 KB

Rerolled on 8.8.x

garphy’s picture

StatusFileSize
new8.12 KB

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

garphy’s picture

StatusFileSize
new706 bytes

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

gradywring’s picture

StatusFileSize
new8.33 KB

@garphy Thanks for your patch! I found one small bug in it around checking for an empty $target_type in IncludeResolver. The $target_typeneeds to be reset after each iteration if the field is dynamic. I'm including a patch.

gradywring’s picture

StatusFileSize
new8.35 KB

Here'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.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ptmkenny’s picture

Version: 9.4.x-dev » 10.1.x-dev
Assigned: abrar_arshad » Unassigned

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

wim leers’s picture

Title: [PP-2] Dynamic Entity Reference normalization support » Dynamic Entity Reference normalization support
Status: Postponed » Active
Issue tags: -
mcanada’s picture

is this still relevant for

Machine name: jsonapi
Version: 10.4.8

???

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.