Fetch alt-text for SVG image | Community
Skip to main content
Level 4
Jun 30, 2023 4:38 PM UTC
Solved

Fetch alt-text for SVG image

I have a backend Sling model which fetches the asset path and then fetches the description from the asset and I display the same in HTL using helper class. It is working for all image types but not for svg images. What maybe the issue here?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Tanika02
Tanika02Accepted solution
Level 7
Jun 30, 2023 8:30 PM UTC

Hello @arindam6600  - 

 

First, validate that you do have the jcr:description/dc:description associated with the image.

 

In AEM, the description property for PNG images is typically stored directly under the image resource node, while for SVG images, the description property is often stored in a separate rendition node. 

 

To address this, you can modify the Sling model a bit to handle the specific storage location of the description property for SVG images.

 

 

import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ValueMap; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; @Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class SvgImageModel { private final Resource resource; public SvgImageModel(Resource resource) { this.resource = resource; } public String getDescription() { ValueMap properties = resource.getValueMap(); String description = properties.get("jcr:description", String.class); if (description == null && resource.getParent() != null) { Resource parentResource = resource.getParent(); ValueMap parentProperties = parentResource.getValueMap(); description = parentProperties.get("jcr:description", String.class); } return description; } }

 

3 replies

Tanika02
Tanika02Accepted solution
Level 7
Jun 30, 2023 8:30 PM UTC

Hello @arindam6600  - 

 

First, validate that you do have the jcr:description/dc:description associated with the image.

 

In AEM, the description property for PNG images is typically stored directly under the image resource node, while for SVG images, the description property is often stored in a separate rendition node. 

 

To address this, you can modify the Sling model a bit to handle the specific storage location of the description property for SVG images.

 

 

import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ValueMap; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; @Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class SvgImageModel { private final Resource resource; public SvgImageModel(Resource resource) { this.resource = resource; } public String getDescription() { ValueMap properties = resource.getValueMap(); String description = properties.get("jcr:description", String.class); if (description == null && resource.getParent() != null) { Resource parentResource = resource.getParent(); ValueMap parentProperties = parentResource.getValueMap(); description = parentProperties.get("jcr:description", String.class); } return description; } }

 

Translate
Level 4
Jun 30, 2023 9:35 PM UTC

Yes, I do have the dc:description associated with the image. I'll modify my code as per your snippet and check if it works.

Translate
EstebanBustamante
New Participant
Jun 30, 2023 4:50 PM UTC

If that's your SlingModel you should able to debug and check what is the issue, i don't think it is related to the assetType, but maybe the asset does not have the property you are searching for? 

 

Just in case I am attaching how you can debug your sling model: https://aemgeeks.com/aem-debugging/how-to-start-aem-in-debug-mode/

Translate
Esteban Bustamante