Accessing Component Level Policy Using PolicyManager

Avatar

Avatar
Validate 1
Level 2
titusrk
Level 2

Likes

2 likes

Total Posts

16 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile

Avatar
Validate 1
Level 2
titusrk
Level 2

Likes

2 likes

Total Posts

16 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile
titusrk
Level 2

31-01-2021

AEM version : 6.5

I have created a proxy component for the OOTB Carousel Component. Now I have defined a custom parameter or a field in the cq:design_dialog. ( as shown in image given below )

 

titusrk_1-1612099933512.png

 Now i have authored the aforementioned parameters by editing the component policy as shown below

titusrk_2-1612100084330.png


Now after saving this , Its stored as follows ( Please refer below image )

titusrk_3-1612100377051.png

How do i access this particular property programmatically ?

I did created a sling model as given below And i am trying to access the same in Sightly

public class CarouselPolicy {

    @SlingObject
    private ResourceResolver resourceResolver;

    /** The resource. */
    @Self
    private Resource resource;

    private int carouselItemLimit;

    private static final Logger LOG = LoggerFactory.getLogger(CarouselPolicy.class);

    @PostConstruct
    public void init(){
        ContentPolicyManager policyManager = resourceResolver.adaptTo(ContentPolicyManager.class);
        if (policyManager != null) {
            ContentPolicy contentPolicy = policyManager.getPolicy(resource);
            if (contentPolicy != null) {
                LOG.trace("carouselItemLimit  "+carouselItemLimit);
                carouselItemLimit= (Integer) contentPolicy.getProperties().get("carouselItemLimit");
            }
        }
    }

    /**
     * @Return carouselItemLimit
     */
    public int getCarouselItemLimit() {
        return carouselItemLimit;
    }
}

 

The contentPolicy is coming as Null. Is this the correct approach ? What am i missing here ?  

 



Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Validate 1
MVP
Umesh_Thakur
MVP

Likes

147 likes

Total Posts

157 posts

Correct reply

53 solutions
Top badges earned
Validate 1
Applaud 25
Ignite 3
Ignite 1
Give Back 5
View profile

Avatar
Validate 1
MVP
Umesh_Thakur
MVP

Likes

147 likes

Total Posts

157 posts

Correct reply

53 solutions
Top badges earned
Validate 1
Applaud 25
Ignite 3
Ignite 1
Give Back 5
View profile
Umesh_Thakur
MVP

31-01-2021

I suspect that, there are some issues with the ResourceResolver And Resource. If you are getting resouceResolver from @slingObject then it seems your model is adaptables to request then in this case it seems your resource is not returnign the proper resource that you need.

so better to check the path of the resource with the help of log.

If you have proper resource then you can get the Valuemap of it and get the required field.

Hope this will help.

 

Umesh Thakur

Answers (1)

Answers (1)

Avatar

Avatar
Ignite 1
MVP
SureshDhulipudi
MVP

Likes

163 likes

Total Posts

174 posts

Correct reply

49 solutions
Top badges earned
Ignite 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile

Avatar
Ignite 1
MVP
SureshDhulipudi
MVP

Likes

163 likes

Total Posts

174 posts

Correct reply

49 solutions
Top badges earned
Ignite 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile
SureshDhulipudi
MVP

31-01-2021


what adapter are you passing on your Sling Model.
adapters = {Carousel.class)