3

i'm building a web services and now i'm writing the documentation for the mobile developer.

When i want to create/modify an entity reference Services 3 ask a stucture like this

"field_organization": {
    "und":[{
        "target_id":"Organization name (69)"
    }]
  }

for a field like this

dsm() output

but, there is another way to give only the nid without the node title? It could be more handy.

I read something like

"field_organization":[{
     "target_id":{
                "target_id":"[nid:69]"
          }
   }]

or like

   "field_organization": {
       "und":[{
             "target_id":{
                 "target_id":"[nid:69]"
               }
           }]
     }

on the official documentation but it didn't work.

Can someone know a method to give only the nid of a Entity reference field?

3
  • Did you make any progress on this problem? I'm currently stuck here too. Oct 4, 2012 at 9:46
  • No, I'm sorry. I think the only way to inser an Entity Reference is the 'NAME (NID)' way.
    – Shyghar
    Oct 4, 2012 at 16:35
  • Raised this as a bug here drupal.org/node/2376065. This issue also screws updaintg nodes that already have values stored only as node ids, because you cannot resubmit them, and get "406 (Not Acceptable : There are no entities matching..."
    – giorgio79
    Nov 16, 2014 at 5:54

4 Answers 4

4

I found the solution! The required data structure depends on the input widget, since Services uses the form submit handlers (which may be a major design flaw). For "Autocomplete/Tagging" the label (id) thingy is necessary. But if you switch to checkboxes it reads "field_reference": { 1:1, 2:2, ... ,nid:nid} and for select box "field_reference": [1,2,3 ... nid].

The REST-API depends on the widgets the frontend uses ... but at least it works. I guess its upside is that REST-posts are validated just as normal form inputs are. I'll also have a look at the approach implemented http://drupal.org/project/restws

2

I tried Philipp's way but no luck. In my case (widget type is select and services-7.x-3.3)

    "field_reference": {"und": "977"}

works. Where 977 is a nid.

1
  • 1
    This worked for me when my widget was checkboxes / radio buttons. Apr 19, 2014 at 2:31
1

I was using select list as a widget in order to get the service working but due to a big number of nodes being referenced in the entity reference field (~9000) I had timeout problems. I couldn't even open the edit node form.

So, I switched to the autocomplete field which loads way faster. To overcome the 'NAME (NID)' problem I realised that the NAME part doesn't have to be the correct one. Only the NID matters. So, sendind something like 'Dummyname (123)' with 123 being the nid of a node titled 'My node' will actually save the correct reference.

0

I had a similar problem using an entity reference field with the checkbox/radio-button widget. Ultimately, the issue in my particular scenario was that I was using the Field Group module to hide all administrative fields on a node type (using a form alter, set access to false for all fields within a specified field group). To get the services POST working correctly, I had to take the entity reference field out of the field group. The JSON that worked after that was:

{
 ...

 "field_entity_ref":{"und":"nid"}

 ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.