3
'api/looks/list' => [
        'criteria' => ['section' => 'looks'],
        'transformer' => function(EntryModel $entry) {

                //print_r($entry->getAncestors());
                // $model = $entry->content->attributeConfigs["featuredImage"]["model"];
                // echo 'class type: ' . get_class($model);
                // print_r($model);

            return [
                'title' => $entry->title,
                'id' => $entry->id,
                'url' => $entry->url,
                'jsonUrl' => UrlHelper::getUrl("looks/{$entry->id}.json"),
                'kachelgroesse' => $entry->kachelgroesse,
                'category' => $entry->category,
                'image' => $entry->image
            ];
        },
    ],

This is my elementapi.php file and it returns:

{
  "data": [
    {
      "title": "SHINY AND RED LIPS",
      "id": "281",
      "url": "http://craft.dev:8888/looks/shiny-and-red-lips",
      "jsonUrl": "http://craft.dev:8888/looks/281.json",
      "kachelgroesse": {
        "0": {
          "label": "mittel",
          "value": "mittel",
          "selected": true
        }
      },
      "category": {
        "0": {
          "label": "Make-Up",
          "value": "Make-Up",
          "selected": true
        }
      },
      "image": {}
    }
  ],
}

There is no image returning. I am requesting the list of all entries and their image/asset location from the API.

3 Answers 3

3

This should work for the images and you can easily adapt to do your categories & kachelgroesse if needed (not tested)...

'api/looks/list' => [
    'elementType' => 'Entry',
    'criteria' => ['section' => 'looks'],
    'transformer' => function(EntryModel $entry) {
        $images = [];
        foreach ($entry->featuredImage as $image) {
        $images[] = $image->url;
        }
        return [
        'title' => $entry->title,
        'id' => $entry->id,
        'url' => $entry->url,
        'jsonUrl' => UrlHelper::getUrl("looks/{$entry->id}.json"),
        'kachelgroesse' => $entry->kachelgroesse,
        'category' => $entry->category,
        'image' => $images
        ];
    },
],
8
  • this is example from element api docs at github... somehow doesnt work for me, i wonder why... :< finally i just dump whole json on channel endpoint with some other plugin. i would really like to filter the json though.
    – goran
    Nov 26, 2015 at 18:44
  • also my kachelgroesse is fine btw...even though nested returns all data.
    – goran
    Nov 26, 2015 at 18:46
  • At his point, if you want to "filter" things, you have 2 options. Create another endpoint from your API with the the correct criteria before the json is created so you get only the data you need OR whatever you are building on top of your API needs the functionality of filtering json payloads.
    – Oli
    Nov 27, 2015 at 9:51
  • what i do now is create endpoint like this: {% header "Content-Type: application/json" %} {% set looks = craft.entries.section('looks').find('') %} {{ looks | json_expand | raw }} in the index.html of the channel template folder, but it returns all json data. Where exactly can i filter it before data arrives there?
    – goran
    Dec 3, 2015 at 12:19
  • I'm sorry, I have not used json_expand... I'd recommend troubleshooting elementAPI still. It's so simple that you must be making a simple mistake somewhere for it not to work. Anyways, I'm a bit confused though, what are using your API for? When you say filter, are you talking about defining the fields for your payload or filtering which entries you are seeing returned?
    – Oli
    Dec 4, 2015 at 17:03
1

A similar question regarding images was answered in this ticket I believe: https://craftcms.stackexchange.com/a/13236/4433

0

You can do something like this:

'transformer' => function(craft\elements\Entry $entry) {
        $image = $entry->photos->one();

    return [
        'image' => $image ? $image->url : null,
    ];
},

More info here.

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.