Fetch alt-text for SVG image | Community
Skip to main content
Level 4
June 30, 2023
Solved

Fetch alt-text for SVG image

  • June 30, 2023
  • 2 replies
  • 964 views

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.
Best answer by Tanika02

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; } }

 

2 replies

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
June 30, 2023

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/

Esteban Bustamante
Tanika02
Tanika02Accepted solution
Level 7
June 30, 2023

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; } }

 

Level 4
June 30, 2023

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.