Wednesday, March 13, 2013

JSON for Drupal select list and location


Background

I'm using PhoneGap and DrupalGap to create my first Android app called "2 For Deals" which helps you track and locate deals in stores that are "2 For"  some dollar value.  Location of the item, as well as expiration of the deal are tracked.  Have a gander (with screenshots): https://play.google.com/store/apps/details?id=com.twofordeals

Tech Talk

DrupalGap provides a JavaScript library for your PhoneGap app, as well as a Drupal module which provides services for accessing data through the Drupal site/data.  Great stuff if you want to combine the two.

JSON

Here is the JSON I used to create a new node which includes the following fields:

  • field_cost: A Decimal field type using the text field widget
  • field_select:  A "List (text)" field type using the "Select list" widget.  Notice the difference between this and some of the other fields.  No index is used.
  • A simple Location determined by users' GPS.  This is the location for the node, where 1 and only 1 is allowed.  Not sure if this is correct, but I had to provide at a minimum the locations and location fields.
  • Body:  standard body field used in Drupal


{
"node": {
"type": "my-content-type",
"title": "This is my new title ",
"language": "und",
"field_cost": {
"und": {
"0": {
"value": "5"
}
}
},
"field_select": {
"und": {
"value": "key-from-drupal for the select"
}
},
"locations": [{
"locpick": {
"user_latitude": "41.98464",
"user_longitude": "-91.673859"
}
}],
"location": {
"locpick": {
"user_latitude": "41.98464",
"user_longitude": "-91.673859"
}
},
"body": {
"und": {
"0": {
"value": "This is the body of my node"
}
}
}
}
}

For blogging purposes, some of the fields are made more generic. The location is one that exists, but points to the middle of a river.  You won't find me there.  :)

I'm posting this because different pieces caused me different headaches at times.  For example on the mobile app, I wanted to collect as little data as possible about the location, so I needed to find the sweet spot of how to create the location on the server side with as few pieces of data as possible.

TODO:  I will eventually be changing the Location to a node reference.  Which means I will need to find the location(s) closest to the users location, display a list, then submit using that reference.  Fun!!!

I hope this helps someone.  Enjoy!

No comments:

Post a Comment