Can't create entity with custom field that holds an Entity Reference via JSON API

Created on 29 February 2024, 4 months ago
Updated 26 March 2024, 3 months ago

Problem/Motivation

Adding an Entity Reference field to a custom field, makes it impossible to create the parent entity when a value is provided for the reference field in the JSON:API payload.

I've tracked down this issue to the denormalization of the data provided for the entity reference in the JSON payload, which crashes at `docroot/core/modules/serialization/src/Normalizer/FieldNormalizer.php`, but before I dig further into actually implementing a solution, I wanted to post this here in case I'm missing something on how I'm supposed to send my payload.

Steps to reproduce

- Have a content type with a custom field.
- Make the custom field have at least an entity reference to some other content type.
- Try to create it via JSON:API

For this JSON API payload:

{
	"data": {
		"type": "node--something",
		"attributes": {
			"status": true,
			"title": "Test custom field entity reference",
			"field_ecommerce_custom": [
				{
					"reference_field": {
						"nid": [
							{
								"value": 1280071
							}
						]
					},
					"text_field": "text value"
				}
			]
		}
	}
}

Getting this error on the response:

"errors": [
		{
			"title": "Unprocessable Entity",
			"status": "422",
			"detail": "$context['target_instance'] must be set to denormalize with the FieldItemNormalizer",
			"links": {
				"via": {
					"href": "http:\/\/news.nbcu-multiverse-drupal.docksal.site\/api\/node\/curated_list"
				}
			},
			"source": {
				"file": "\/var\/www\/docroot\/core\/modules\/jsonapi\/src\/Controller\/EntityResource.php",
				"line": 866
			},

Note that I'm sending the nid in that format because it's how JSON API is returning it on a GET call. Trying the standard way of doing it as for standard entity reference fields, like so:

"data": {
	"type": "node--source",
	"id": "1234dumm-yuui-dgen-erat-ed1000000000"
}

Allows the node to be created, but the reference is not set at all.

Proposed resolution

Remaining tasks

tba

User interface changes

tba

API changes

tba

Data model changes

tba

πŸ› Bug report
Status

Closed: works as designed

Version

2.0

Component

Code

Created by

πŸ‡ͺπŸ‡ΈSpain slv_

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @slv_
  • πŸ‡ͺπŸ‡ΈSpain slv_
  • πŸ‡ΊπŸ‡ΈUnited States apmsooner

    Theres a custom normalizer that extends FieldItemNormalizer but I didn't override the denormalize function so assuming thats the issue if you want to take a look and offer a solution: https://git.drupalcode.org/project/custom_field/-/blob/2.0.x/src/Normali...

  • πŸ‡ΊπŸ‡ΈUnited States apmsooner

    Also noting theres a custom data type that handles the setValue() function. I'm honestly not real familiar with posting data via jsonapi but maybe this would serve some additional reference on how to structure: https://git.drupalcode.org/project/custom_field/-/blob/2.0.x/src/Plugin/...

  • πŸ‡ͺπŸ‡ΈSpain slv_

    Thanks! I've kept digging into it and found some workarounds. I'll update this once I've a clean version, as there were other issues I faced with the module for my use case. Not sure if it'll be generic enough to be a valid patch for the module, but at least it may serve to enrich documentation and give some guidance for others.

  • Status changed to Postponed 4 months ago
  • πŸ‡ΊπŸ‡ΈUnited States apmsooner

    Putting this on hold. There are some limitations in general with the JSON:API specification that likely just won't work without some support in Core to be able to extend field item normalizers to get relations at top level of field and to support meta in the relationships for identifying our sub-field properties. There's probably not a whole lot of core interest in accommodating some of these changes given the existing field paradigm and thus lack of understanding around the benefits of this module. Focusing on the GraphQL support task ( https://www.drupal.org/project/custom_field/issues/3423901 ✨ GraphQL Compose plugins integration Active ) is better use of time at this point.

  • Status changed to Closed: works as designed 3 months ago
  • πŸ‡ΊπŸ‡ΈUnited States apmsooner

    Can't fix this due to limitation with jsonapi spec requiring relationships at field level vs. property level. If someone needs this, the best solution is to either create a custom normalizer for your needs or a jsonapi enhancer to apply via jsonapi_extras module.

Production build 0.69.0 2024