0

I'm having an issue where node_save() (inserting new node) ignores some fields in my JSON node. This happens with several content/widget types. I've posted this to Drupal services with no response.

I followed Tyler's post here recommending to do a GET to observe and mimic the JSON format returned - unfortunately I'm finding the format returned in a GET is not always the same format as expected in a node_save() POST. https://github.com/signalpoint/DrupalGap/issues/418

I also saved a node on our Drupal website and captured the format of the POST body, trying to translate that format to analogous JSON. No luck.

  1. Date time field, with start and end date, using Date (Unix timestamp) field type and pop-up calendar widget. Formats recommended in the post below does not save, only default value (now) is saved. Example is field_when_available below. Services module update date field

  2. Integer field types with Text field widget will not save. I've tried over 20 permutations of formats, posted here, no response. htt ps://www.dru pal.org/node/2870095 field_scrap_weight is an example below.

  3. Reference to another node will not save. Same link above describes using target_id, tried that, no success.

What will save are are term reference and List(text) content types from Radio button widgets. field_surplus_type and field_unprepared_or_prepared are examples.

I am posting to http://127.0.0.1:8083/?q=drupalgap/node.json

Below is one example, but I've tried literally hundreds of permutations with and without array brackets, with/without the "und" element, with/without quotes in the data value, etc.

I'm beginning to think there's a more global/systematic issue like a content-type setting.

{
  "type": "donation",
  "form_id": "donation_node_form",
  "language":"und",
  "field_unprepared_or_prepared": {    // saves OK (List text, checkbox/radio)
    "und": {
      "value": "unprepared"
    }
  },
  "field_surplus_type": {        // saves OK (Term ref, checkbox/radio)
    "und": [ 1, 3  ]
  },
  "field_scrap_weight": {        // NOT saved (integer, text field)
    "und": {"value": "1"}
  },
  "field_when_available": {      // NOT saved (Unix timestamp), popup calendar
    "und":[
      {
        "value": {
            "date": "05/29/2017",
            "time":"09:00pm"
        },
        "value2": {
            "date": "05/29/2017",
            "time":"09:00pm"
        },
        "timezone": "America\/New_York",
        "timezone_db": "UTC",
        "date_type": "datestamp"
      }]
  },
  "field_surplus_location": {     // NOT saved - node reference
    "und": {"target_id":"256"}
  },
  "body": {           // NOT saved (long text w/summary, text area widget)
    "und": {
      "value": "dd"
    }
  }
}

1 Answer 1

0

It's important to understand that the Services module is closely tied to the Drupal Forms API, meaning that the JSON delivered via POST/PUT is sometimes dependent on the Widget that is selected for the field(s) in question, so it won't always match what is delivered via GET.

That being said, I can see some problems with your JSON. Try these instead:

integer, text field

"field_scrap_weight": {
  "und": [ { "value": "1" } ] 
},

node reference

"field_surplus_location": {
  "und": [ { "target_id":"... (256)" } ]
},

long text w/summary, text area widget

"body": {
  "und": [ {
    "value": "dd"
  } ]
}

Often times it appears you are missing the wrapping [], this indicates the delta value for the field's item, i.e. it's an array so you can pile up extra values if your field is configured for more than 1 value.

The entity reference field can be any text, but the target id must be wrapped in the parenthesis.

The Date field is the trickiest and I would advise looking at the following answers:

If you're looking to POST/PUT exactly what is delivered via GET, then check out the Services Entity module and this extra configuration that can be added to jDrupal.

2
  • Thanks for the quick response. I've tried the formats you suggested, with no luck. In this post I tried brute force of 26 formats for one field (one of them being your suggestion), none result in the value being saved. drupal.org/node/2870095 I have another content type where a field using "Text Field" widget is saved successfully, so perhaps it's something with the definition of the fields in the content type itself. Thanks for the suggestion of Services Entity module, I may give that a try. Commented May 30, 2017 at 0:03
  • No problem, and I wish you luck in resolving the issue. Just remember that as you change the Widget on your field under the Manage fields tab, this can change the JSON that is expected during the POST/PUT. Commented May 30, 2017 at 2:50

Your Answer

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

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