0

I'm using the Element API and would like to return content based on it's handle. The two handles I am trying to return are called appLogo and appVideo:

'api/featured-apps.json' => [
  'elementType' => 'Entry',
  'criteria' => [
    'section' => 'apps',
    'appFeatured' => '1',
  ],
  'transformer' => function (EntryModel $entry) {
    return [
      'title' => $entry->title,
      'logo' => $entry->appLogo,
      'video' => $entry->appVideo,
    ];
  }
],

This does not work, do I need to access images/videos in a different way? Ideally, I'd like the relative path returned for both logo and video.

1

I ended up doing it like this:

'api/featured-apps.json' => [
  'elementType' => 'Entry',
  'criteria' => [
    'section' => 'apps',
    'appFeatured' => '1',
  ],
  'transformer' => function (EntryModel $entry) { 
    $logos = [];
    foreach ($entry->appLogo as $image) {
      $logos[] = $image->url;
    }

    $videos = [];
    foreach ($entry->appVideo as $video) {
      $videos[] = $video->url;
    }

    return [
      'title' => $entry->title,
      'logo' => $logos,
      'video' => $videos,
    ];
  }
],
| improve this answer | |
0

Assuming both appLogo and appVideo are Asset fields, they will always return an array, even if there is only one asset selected.

'logo' => $entry->appLogo->first(),
'video' => $entry->appVideo->first(),

Should do what you're looking for.

| improve this answer | |

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.