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

  • June 6, 2024
  • 1 reply
  • 2121 views

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

 

 

 

Best answer by h_kataria

Can you try adapting from request:

@Model(adaptables = SlingHttpServletRequest.class)

1 reply

h_kataria
Community Advisor
h_katariaCommunity AdvisorAccepted solution
Community Advisor
June 7, 2024

Can you try adapting from request:

@Model(adaptables = SlingHttpServletRequest.class)
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?

 

 

 

 

h_kataria
Community Advisor
Community Advisor
June 7, 2024