Querying alt tags for inline images in 11ty site with Sanity media library plugin
21 replies
Last updated: Mar 19, 2021
F
Good morning kind Sanity people.
I have been striving to figure out how to display inline images in block content with responsive sizes and alt tags for an 11ty site. I now have the srcset working after reverse engineering
https://github.com/brob/eleventy-plugin-sanity-image . Now I need help to understand how I can query the alt tag for the image from inside my serializer function. I'm using the excellent Sanity media library plugin (https://www.sanity.io/plugins/sanity-plugin-media-library ) to set the alt tags. My serializer function is using @sanity/image-url to query Sanity to build the image urls. How can I query the alt tag I set in the media library?
I have been striving to figure out how to display inline images in block content with responsive sizes and alt tags for an 11ty site. I now have the srcset working after reverse engineering
https://github.com/brob/eleventy-plugin-sanity-image . Now I need help to understand how I can query the alt tag for the image from inside my serializer function. I'm using the excellent Sanity media library plugin (https://www.sanity.io/plugins/sanity-plugin-media-library ) to set the alt tags. My serializer function is using @sanity/image-url to query Sanity to build the image urls. How can I query the alt tag I set in the media library?
Mar 19, 2021, 11:01 AM
F
So I've got it working with an async function but it's clear that I should be materializing the asset fields in the main query, rather than querying for the alt tag individually for each inline image.
My schema has a page document type that contains an array which in turn contains objects.
One of the objects is named 'content' and is comprised of a single block content field. Can anyone help me figure out a groq query to materialise the alt field I need for the inline image in this block content?
I discovered that the media library stores the alt field below sanity.imageAsset
Any help gratefully received as always
😉
My schema has a page document type that contains an array which in turn contains objects.
One of the objects is named 'content' and is comprised of a single block content field. Can anyone help me figure out a groq query to materialise the alt field I need for the inline image in this block content?
I discovered that the media library stores the alt field below sanity.imageAsset
Any help gratefully received as always
😉
Mar 19, 2021, 3:01 PM
F
*[_type == "page" && title != ""] {
title,
slug,
headerBlocks,
mainBlocks[],
...,
_type == "content" => {
...,
blocktext[]{
...,
children[]{
...,
_type == "image" => {
"alt": @.imageAsset->alt,
}
}
}
},
}
Mar 19, 2021, 6:06 PM
F
Not giving an error, but not materializing the alt field still. Could you have a look for me
user A
?Mar 19, 2021, 6:07 PM
F
Are you getting results back as deep in your query as
children[]?
Mar 19, 2021, 6:48 PM
F
Thanks for the reply. Yes. I'm getting all the fields back but not the alt.
Mar 19, 2021, 6:49 PM
F
Is it okay to nest those _type == "" clauses?
Mar 19, 2021, 6:50 PM
F
I think it should be fine. If you replace
"alt": @.imageAsset->alt,with
..., what do you get back?
Mar 19, 2021, 6:51 PM
F
I seem to get the same result
Mar 19, 2021, 6:55 PM
F
Do any items of the
childrenarray have the type of image?
Mar 19, 2021, 6:56 PM
F
There is no children array in the results. This might be the issue.
Mar 19, 2021, 6:57 PM
F
I can see mainblocks -> 0 -> blocktext -> 0,1,2,3,4,5,6,7,8
Mar 19, 2021, 6:59 PM
F
Sounds like it, yes. I might try commenting out all the ellipses to limit noise, then bring them back in once you’re getting your alt data.
Are any of the
Are any of the
blocktextitems
children[]?
Mar 19, 2021, 6:59 PM
F
No, they are numbered 0-8
Mar 19, 2021, 7:00 PM
F
8 holds the image
Mar 19, 2021, 7:00 PM
K
okay
Mar 19, 2021, 7:01 PM
F
It’s tough to say without seeing the query result, but maybe remove
children[]{and its corresponding
}.
Mar 19, 2021, 7:01 PM
F
Okay. I'll keep trying. I discovered vision so it should be possible. At least I know I'm not completely off the track now, thanks.
Mar 19, 2021, 7:02 PM
K
Another approach I might use myself is to remove the entire projection (i.e., get all the data) then Inspect the JSON and trace out the path.
Mar 19, 2021, 7:02 PM
F
Feel free to post your schema and/or query results (redacting anything private) if you try a bit more and are still stuck.
Mar 19, 2021, 7:04 PM
K
Thanks I will. I'm really pleased to have got the serializer function working. Getting the alt text in is the icing on the cake!
Mar 19, 2021, 7:05 PM
F
I applaud your tenacity. We’ll get there! 🙂
Mar 19, 2021, 7:13 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.