nomdejour Posted November 25, 2016 Share Posted November 25, 2016 Hi all, I've just started playing around with PW and have set up a basic JSON response to return fields in JSON format. I can get basic field data fine, eg. title as strings but I'm having trouble with other field types. For instance the response for a repeater which has two text fields and an image field is: {"data":"1021,1022,1023","count":3,"parent_id":1020}} How can I pull out the content of these inner fields? Is there a normal pattern custom field types use to reference data? Link to comment Share on other sites More sharing options...
bmacnaughton Posted November 25, 2016 Share Posted November 25, 2016 The short answer is I don't know where the internals of the DB are documented. The long answer? Read on. I found that Repeaters were a bit odd compared with other compound (indirect, recursive, whatever you call them) fields. Most "data" fields in ProcessWire are JSON but repeaters are just a list of page numbers. If you add '[' + data + ']' you can JSON decode it as an array. In JavaScript I ended up doing this: data.split(',').map(text => parseInt(text)); for the same result. It's definitely overkill for what you're doing, but I wrote a deep ProcessWire-specific DB comparison program (in JavaScript) that handles standard field types other than Matrix (because we don't use that). It also handles the Padloper field types that we have implemented so far. If you're comfortable with JavaScript it is available here: https://github.com/bmacnaughton/pw-compare. My goal, when I'm not working on more immediate concerns, is to start outputting LostKobrakai's Migrations for each of the noted differences. The module you'd want to look at is `comparison/pagefields.js` 1 Link to comment Share on other sites More sharing options...
Robin S Posted November 25, 2016 Share Posted November 25, 2016 2 hours ago, nomdejour said: How can I pull out the content of these inner fields? Have you seen this module? http://modules.processwire.com/modules/pages2-json/ I think a hook to Pages2JSON::getValue could be used to return the fields from the Repeater child pages. Link to comment Share on other sites More sharing options...
microcipcip Posted November 25, 2016 Share Posted November 25, 2016 I wrote a class to get any ProcessWire field in the core. It is still incomplete but should work for what you need. First you need to set up an api/pages template as explained in this tutorial Then you can use this code for the pages template and this class for getting all fields. This will allow you to get the following: http://mysite.com/api/pages/1 to get all fields from the home page http://mysite.com/api/pages/1?children=1 to get all fields plus the children pages http://mysite.com/api/pages/1/template-name/ to get a list of children with the given template name There's much more but you can read the class comments to learn how to use it. Note that I am a noob PHP developers so there may be a lot of mistakes ;p Link to comment Share on other sites More sharing options...
adrian Posted November 25, 2016 Share Posted November 25, 2016 There is also: https://github.com/ryancramerdesign/ServicePages Also read from here (https://processwire.com/talk/topic/1654-pages-web-service-servicepages/?do=findComment&comment=132839) onwards to get it working with PW 3.x Link to comment Share on other sites More sharing options...
nomdejour Posted November 28, 2016 Author Share Posted November 28, 2016 Thanks everyone. My PHP is rusty but I'm going to try out all the suggested approaches. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now