Wondering if it's possible to change the output that I'm getting from ElementAPI. I'm using the API to preload assets, and this is the output I'm getting.
{
"data": [
{
"title": "Hero",
"url": "assets/hero.jpg"
},
{
"title": "Profile",
"url": "assets/profile.jpg"
}
]
}
This is great, and pretty much exactly what I need, but I'm wondering if there is a way to change the output so that, rather than using "data":
I can use "manifest":
which is what my preloader needs. Ideally, my code should look like this:
{
manifest: [
{
"title": "Hero",
"url": "assets/hero.jpg"
},
{
"title": "Profile",
"url": "assets/profile.jpg"
}
]
}
Here also is my elementapi.php
file:
namespace Craft;
return [
'endpoints' => [
'assets/data/assets.json' => [
'paginate' => false,
'elementType' => ElementType::Asset,
'criteria' => [
'limit' => null,
],
'transformer' => function(AssetFileModel $asset) {
return [
'id' => $asset->title,
'url' => $asset->url,
];
},
],
]
];
If there isn't a way to do this in the plugin, is there a way to get the endpoint file and change it some other way?
Thanks!
data
key is provided by the plugin. You could modify the source to make all returned elements appear under amanifest
key, but you will have to do some additional work to allow it to be configurable by each endpoint, individually. Check out Fractal's internals (github.com/craftcms/element-api/tree/v1/elementapi/vendor/…) for how you might update those keys!