craftcms / element-api

Create a JSON API/Feed for your elements in Craft.
MIT License
498 stars 57 forks source link

uri field is empty when requesting entry image from S3 volume #101

Closed djurdjen closed 5 years ago

djurdjen commented 5 years ago

I'm requesting the image from an entry that has been stored with S3. The code below:

Asset::find()->relatedTo($entry['id'])->asArray()->all();

returns:

id: "28",
fieldLayoutId: null,
uid: "ded42611-d967-4ad5-8649-1d49d4efb256",
enabled: "1",
archived: "0",
dateCreated: "2019-03-13 12:16:58",
dateUpdated: "2019-03-13 12:16:58",
slug: null,
uri: null,
enabledForSite: "1",
volumeId: "2",
folderId: "4",
filename: "stock002.png"

The uri field is null somehow. Is this a bug, am I doing something wrong or is there a fix for this already?

brandonkelly commented 5 years ago

uri is a special property that is only used by element types that have Craft-managed URLs, including entries and categories.

To get an asset’s URI, use path or getPath() instead:

'transformer' => function(Asset $asset) {
    return [
        'path' => $asset->path,
    ];
},