I have a structure called "locations" that contains two entry types:
- State
- Location
I'd like to access specific states at the URL locations/ny.json
, for example, and get a JSON object with all the location entries nested under that state.
The entries are already properly nested for HTML output, but I'm trying to get the JSON to match.
This is my elementsapi.php
file right now:
<?php
namespace Craft;
return [
'endpoints' => [
'locations.json' => [
'elementType' => 'Entry',
'criteria' => [
'section' => 'locations',
'type' => 'location'
],
'transformer' => function(EntryModel $entry) {
return [
'title' => $entry->title,
'url' => $entry->url,
'latitude' => $entry->latitude,
'longitude' => $entry->longitude,
'jsonUrl' => UrlHelper::getUrl("locations/{$entry->id}.json")
];
},
],
'locations/<slug:{slug}>.json' => function($slug) {
return [
'elementType' => 'Entry',
'criteria' => [
'section' => 'locations',
'type' => 'state',
'slug' => $slug
],
'transformer' => function(EntryModel $entry) {
return [
'title' => $entry->title,
'url' => $entry->url,
'latitude' => $entry->latitude,
'longitude' => $entry->longitude,
'jsonUrl' => UrlHelper::getUrl("locations/{$entry->id}.json")
];
},
];
},
]
];
The second endpoint is currently returning a single state entry, but I would like it to return all locations that are a descendant of that state.
Is this possible using a criteria param such as descendantOf
?
What criteria params are available?