Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Fetch alt-text for SVG image

Avatar

Level 5

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?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

 

View solution in original post

3 Replies

Avatar

Community Advisor

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

Avatar

Correct answer by
Community Advisor

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

 

Avatar

Level 5

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.