1

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!

1
  • If I understand the question, that data key is provided by the plugin. You could modify the source to make all returned elements appear under a manifest 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! Commented May 16, 2017 at 23:39

1 Answer 1

2

Update:

Looks like changing the 'data' key is a bit more difficult I suspected. We just released Element API 1.3.0 that exposes the internal $resourceKey necessary to change that value.

In your elementapi.php config file, something like this should work:

UPDATE: This is the final code that I used, and it works perfectly.

return [
    'defaults' => [
        'resourceKey' => 'manifest'
    ],

    'endpoints' => [
        'assets/data/assets.json' => [
            'paginate' => false,
            'elementType' => ElementType::Asset,

            'transformer' => function(AssetFileModel $asset) {
                return [
                    'id' => $asset->title,
                    'src' => $asset->url,
                ];
            },
       ],
    ]
];

The output you're seeing is defined by your craft/config/elementapi.php config file.

You can read about that configuration file and all of the available settings in the Element API documentation at: https://github.com/craftcms/element-api/tree/v1#setup

6
  • Thanks. I'm pretty new to ElementAPI though, is there a way you could quickly help break it down for me? I'm picking up a little bit from this page, but not enough to make it work. Commented May 16, 2017 at 23:05
  • Can you edit the original question to include your elementapi.php file?
    – Brad Bell
    Commented May 16, 2017 at 23:13
  • Yes, absolutely. Done. Commented May 16, 2017 at 23:14
  • Updated answer.
    – Brad Bell
    Commented May 17, 2017 at 0:02
  • Can I have different 'resourceKey' values for a different endpoints? Commented Oct 17, 2019 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.