Score:1

How to get product variation media file url using jsonapi?

us flag

I'm using the following jsonapi GET request to retrieve product variations for a product:

/jsonapi/commerce_product/default/0ea927c7-b62d-45c9-8bec-dc3d16215276/variations?include=field_images

However, the response does not include the file url for the media:

{
"type": "media--image",
"id": "6af50149-ae35-4a3b-802d-b21c5d863235",
"links": {
"self": {
"href": "http://myurl/en/jsonapi/media/image/6af50149-ae35-4a3b-802d-b21c5d863235?resourceVersion=id%3A4"
}
},
"attributes": {
"drupal_internal__mid": 4,
"drupal_internal__vid": 4,
"langcode": "en",
"revision_created": "2021-09-03T08:03:04+00:00",
"revision_log_message": null,
"status": true,
"name": "image-940375_1920.jpg",
"created": "2021-09-03T08:02:55+00:00",
"changed": "2021-09-03T08:02:55+00:00",
"default_langcode": true,
"revision_translation_affected": true,
"metatag": null
},
"relationships": {}
},

How do I add the media file url to the request?

Score:1
us flag

I figured it out:

/variations?include=field_images.field_media_image&fields[file--file]=uri,url
cn flag
If you need the links to image styles, check out the [Consumer Image Styles](https://www.drupal.org/project/consumer_image_styles) or [JSON:API Image Styles](https://www.drupal.org/project/jsonapi_image_styles) modules.
iamfredrik avatar
us flag
The JSON:API Image Styles module crashed my site and the Consumer Image Styles is a bit too complex for my use case.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.