I'm using the Element API plugin to return data from an entry that also contains an image asset. Is there a way to combine the two so I can get the asset URL alongside the entry fields?

Attached below is an approximation of my line of thinking.

        'elementType' => 'Entry',
        'criteria' => ['section' => 'wax'],

        'transformer' => function(EntryModel $entry) {
            return [
                'excerpt' => $entry->excerpt,
                'heroImage' => $asset->heroImage.url // Something like this?
  • So the asset field is a custom field on the entry?
    – Brad Bell
    Jan 14 '16 at 21:23

Yes - you can access it exactly the same way you do in the templates; you just need to use PHP syntax (e.g. -> for accessing sub-properties rather than .).

'transformer' => function(EntryModel $entry) {
    // Begin defining the response data for this entry
    $data = [
        'excerpt' => $entry->excerpt,

    // Get the first asset in a "heroImage" Assets field (if there is one)
    $heroImage = $entry->heroImage->first();

    if ($heroImage) {
        $data['heroImage'] = $heroImage->url;

    return $data;
  • 1
    Hi Brandon, works a charm. Thanks, and thanks for the stellar software!
    – A Ron
    Jan 14 '16 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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