Jump to content

Repeater data returned as JSON


nomdejour
 Share

Recommended Posts

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

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`

 

 

  • Like 1
Link to comment
Share on other sites

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:

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...