Using @ScriptVariable to get styles from design dialog selections | Community
Skip to main content
Level 2
June 6, 2024
Solved

Using @ScriptVariable to get styles from design dialog selections

I am trying to get the styles assigned in the design dialog incorporated into my Sling model. I am getting null for the currentStyle

 

package com.cws.aem.core.models.v1.content;

import com.cws.aem.core.models.v1.ComponentModel;
import com.day.cq.wcm.api.designer.Style;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ImmersiveKeyStatsModel extends ComponentModel {
private static Logger LOGGER = LoggerFactory.getLogger(ImmersiveKeyStatsModel.class);

public final static String RESOURCE_TYPE = "cws/components/core/content/immersive-key-stats-component/v1/immersive-key-stats-component";

@Self
private Resource resource;

@ScriptVariable
protected Style currentStyle;


@PostConstruct
protected void init() {
LOGGER.debug("TestImmersiveKeyStats Model is initializing");

}

}

 

Here is a screenshot of my debug attempt using the JVM debugger

 

 

 

h_kataria
h_katariaAccepted solution
New Participant
June 7, 2024

Can you try adapting from request:

@Model(adaptables = SlingHttpServletRequest.class)

11 replies

h_kataria
h_katariaAccepted solution
New Participant
June 7, 2024

Can you try adapting from request:

@Model(adaptables = SlingHttpServletRequest.class)
Translate
Level 2
June 7, 2024

It returns an object now that I include the adaptables for the SlingHttpServletRequest.class

I guess now I'm a little confused because properties associated with style are not quite what I thought they would be. I did this again using using teaser component and I defined allowed styles

I then set the Background Style to JET

 

I see in the JVM debugger this value map for currentStyle. There is no reference I can see the the Style classes getting applied to the component. Am I misunderstanding how this @ScriptVariable annotation works? Is there another way to get the actual class assignments for a component set via design dialog?

 

 

 

 

Translate
h_kataria
New Participant
June 7, 2024

I think you want to retrieve the applied CSS classes, you might want to take a look at ComponentStyleInfo API https://developer.adobe.com/experience-manager/reference-materials/cloud-service/javadoc/com/adobe/cq/wcm/style/ComponentStyleInfo.html 
Sample : https://github.com/adobe/aem-core-wcm-components/blob/277b972c11a9d73d397e886aad0fe817294d2d08/bundles/core/src/main/java/com/adobe/cq/wcm/core/components/util/AbstractComponentImpl.java#L159 
You will find plenty other examples, just have a quick google search 🙂 

Hope this helps

Translate