0

I have an audio file that is stored as Media of type Audio.

Now I want to get the link to that file that is usually displayed to site users via jsonapi.

For example, when the media entity is rendered, this is the link:

<audio controls="" controlslist="nodownload"> <source src="/sites/default/files/coolstuff/audio/apples-bananas-pears.mp3" type="audio/mpeg"> </audio>

This /sites/default/files/coolstuff/audio/apples-bananas-pears.mp3 path is what I want to get from jsonapi.

However, when I look at the output of /jsonapi/media/audio, all the URLs look like this:

http://mysite.com/jsonapi/media/audio/d741b59c-9617-4a24-a2e8-99005a76507b?resourceVersion=id%3A31"

None of them have the public-facing URI.

The documentation gives this example for a "article media entity reference field_image url, uri by including references":

http://example.com/jsonapi/node/article/{{article_uuid}}?include=field_image&fields[file--file]=uri,url

I have media entity references, so I tried the same on my site:

http://mysite.com/jsonapi/node/MY_CONTENT_TYPE?include=field_ref_media_audio&fields[file--file]=uri,url

However, I do not get the regular media URI, just the jsonapi one.

How can I get the media public file URI (/sites/default/files/coolstuff/audio/apples-bananas-pears.mp3)?

1 Answer 1

0

Here's the correct path:

http://mysite.com/jsonapi/node/MY_CONTENT_TYPE?include=field_ref_media_audio.field_media_audio_file&fields[file--file]=uri,url

This results in a value for uri such as:

"value": "public:/sites/default/files/coolstuff/audio/apples-bananas-pears.mp3",
2
  • are you aware of anyway to flatten this out a bit? This means my front-end needs to loop included for the field_media.uuid, then loop its relationships for the its file--file.uuid, then back up across included to find file-file.attributes.uri.url
    – E.A.T
    Aug 31, 2021 at 4:48
  • @E.A.T No, I don't know a way to simplify this. There is the JSON:API Resources module to define your own custom resource if you want to write all the logic yourself on the backend. Sep 1, 2021 at 5:42

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.