Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

Accessing Component Style using sightly

toimrank
Level 3
Level 3

Hi All,

 

I have a business use case where I want to access component style system authored in editable template policy. I looked for an API and nothing similar available.

 

There is a property same in my content component node with cq:styleId. usiing this style id I want to access my style name authored in component style system as template policy.

 

Thanks in Advance.

1 Accepted Solution
Vijayalakshmi_S
Correct answer by
Community Advisor
Community Advisor

Hi @toimrank,

We can write a query to get the style node (configured as policy) with the Id from the component node available under respective page content. 
(Style node in policy will have details of style label, style class and style id - Sample screenshot for reference below)
Sample query:

path=/conf/yourproject/settings/wcm/policies
type=nt:unstructured
1_property=cq:styleId
1_property.1_value=1590089010715
1_property.2_value=1590089575644
p.limit=-1

Where

  • path can be refined to specific component policy under main"policies" path based on the need
  • property.value predicate to be framed dynamically from the component node available under the desired content path(cq:styleIds - String[])
    For how many ever style Ids you have, frame property.value predicates accordingly.
  • Returned query result set will be the highlighted node and hence access to style label and class.

To be implemented in Sling Models or WCMUsePojo and expose the style class via getters for access in sightly.

Screenshot for reference:

stylepolicy.png

View solution in original post

7 Replies
Vijayalakshmi_S
Correct answer by
Community Advisor
Community Advisor

Hi @toimrank,

We can write a query to get the style node (configured as policy) with the Id from the component node available under respective page content. 
(Style node in policy will have details of style label, style class and style id - Sample screenshot for reference below)
Sample query:

path=/conf/yourproject/settings/wcm/policies
type=nt:unstructured
1_property=cq:styleId
1_property.1_value=1590089010715
1_property.2_value=1590089575644
p.limit=-1

Where

  • path can be refined to specific component policy under main"policies" path based on the need
  • property.value predicate to be framed dynamically from the component node available under the desired content path(cq:styleIds - String[])
    For how many ever style Ids you have, frame property.value predicates accordingly.
  • Returned query result set will be the highlighted node and hence access to style label and class.

To be implemented in Sling Models or WCMUsePojo and expose the style class via getters for access in sightly.

Screenshot for reference:

stylepolicy.png

View solution in original post

toimrank
Level 3
Level 3
Thanks for you comments. I am moreover looking for some OOTB sightly Utility API function to get styles using Style Id.
Vijayalakshmi_S
Community Advisor
Community Advisor

Hi @toimrank,

I don't think there is a straight forward utility/sightly implicit objects for getting Style Id/CSS name configured via Style System.

Reason being Styles Tab available as part of Policy is part of the design configuration of a component.

In other words, Styles tab has container resource with Coral UI 3 multifield - Like any other dialog field resource that is part of Design dialog of a component.

 

As a whole, we can get to the policy applied for a specific content resource which is via ContentPolicyManager API as @divanshug96  suggested. 

ContentPolicyManager cntPolicyMgr = rescResolver.adaptTo(ContentPolicyManager.class);
ContentPolicy cntPolicy = cntPolicyMgr.getPolicy(currentResource);
String cntPolicyPath = cntPolicy.getPath(); // This will return the policy path from /conf  

However to get to the style class by this way, need further manipulation as "cq:styleGroups" is child node of policy path.

divanshug96
Level 1
Level 1

Hi toimrank 

 

Please use the following to retrieve policy manager

ContentPolicyManager policyManager = resourceResolver.adaptTo(ContentPolicyManager.class);

ValueMap policyProperties = contentPolicy.getProperties();

 

toimrank
Level 3
Level 3
Thanks. This will be returning the values at policy node. Is there any way to get the styles using "cq:styleId" from ContentPolicyManager ? or there is any other API for it ?