submitting to the REST server with JSON, which includes an entityreference field, does not create the content on the new node.

Having done a raft of testing, it appears that the data is reaches services, but then does not get processed.

The JSON is:

{
"title":"Test Node Y",
"type":"element",
"body":{"und":[{"value":"Some text","format":"plain_text"}]},
"field_productentityref":{"und":[{"target_id":{"target_id":"[target_id:14480]","target_type":"node"}}]}
}

Using http://drupal.org/node/1388924 as a reference for how to format the JSON to populate the entity reference.

Attached are watchdog entries associated with a query (based on the above). As you can see the data, including the entityreference is consistently there, but services is not putting this info into the entityreference field on the node create.

It could be that the query for field_productentityref is incorrect, but we have tried an exhaustive list of possible formats.

Is there a bug in there?

Comments

marcingy’s picture

Category: bug » support

Moving to support request because this is likely a case of the FAPI data structure being incorrect as services simply plugins into FAPI.

gmak’s picture

Some further testing may help to sort this one out.

If I execute the following php code:

<?php
$node = new stdClass();
$node->title = "YOUR TITLE";
$node->type = "element";
$node->field_productentityref[$node->language][]['target_id'] = 14480;
$node->field_productentityref[$node->language][]['target_type'] = "node";
node_submit($node);
node_save($node);
?>

A node is created, with a title, but again the entityreference field is not populated.

The data from the node_save operation shows:

stdClass Object
(
    [title] => YOUR TITLE
    [type] => element
    [field_productentityref] => Array
        (
            [] => Array
                (
                    [0] => Array
                        (
                            [target_id] => 14480
                        )

                    [1] => Array
                        (
                            [target_type] => node
                        )

                )

        )

    [created] => 1329520968
    [validated] => 1
)
mwidner’s picture

I can confirm this bug. I'm able to post properly formatted JSON with an entity reference; the system accepts the data, but doesn't populate the entity references in the new node.

azicchetti’s picture

If your field is using the autocomplete widget, you just have to change the target_id format:

field_yourentityreference:{"und":[{"target_id":"anything (183)"}]}

The form handler probably gets (I've not checked yet in the code) the id with a regexp, but you must supply a fake title in order to work.

If the field is using another widget, say, the checkboxes, I think the target_id must be supplied in yet another way.

ygerasimov’s picture

Status: Active » Fixed

Services do drupal_form_submit to the node form. So I propose you to debug the values in $form_state on normal form submission and build the same structure in your services call. @azicchetti has point about the autocomplete widget. I don't know which widget do you use.

Please reopen this issue if you still face these problems.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.