How can I dynamically set a CSS class on the wrapping decoration tag of an AEM6 Sightly ponent?
I cannot set this CSS class on the ponent as it depends on the instance of the ponent, and I can't set it on the resource as the resource can be rendered on multiple pages and the CSS class differs depending on which page it is on.
I've tried the following 3 techniques inside the JavaScript Use-API with no success:
ponentContext.getCssClassNames().add('test-class-1');
ponent.getHtmlTagAttributes().set('class', 'test-class-2');//throws an exception
currentNode.setProperty('cq:cssClass', 'test-class-3');
How can I dynamically set a CSS class on the wrapping decoration tag of an AEM6 Sightly ponent?
I cannot set this CSS class on the ponent as it depends on the instance of the ponent, and I can't set it on the resource as the resource can be rendered on multiple pages and the CSS class differs depending on which page it is on.
I've tried the following 3 techniques inside the JavaScript Use-API with no success:
ponentContext.getCssClassNames().add('test-class-1');
ponent.getHtmlTagAttributes().set('class', 'test-class-2');//throws an exception
currentNode.setProperty('cq:cssClass', 'test-class-3');
Share
Improve this question
asked Sep 29, 2014 at 14:25
Alasdair McLeayAlasdair McLeay
2,6524 gold badges33 silver badges51 bronze badges
2 Answers
Reset to default 9The decoration tag is added by a filter (namely the WCMComponentFilter
) before the ponent is actually rendered, so it isn't possible to change it inside the ponent code. In order to use some logic to dynamically set a CSS class on this decorator, you need to create a custom filter, that will run before the WCMComponentFilter
and set appropriate properties to the IncludeOptions
attribute.
Following filter adds my-class
to all carousel ponents under /content/geometrixx/en
.
@SlingFilter(scope = SlingFilterScope.COMPONENT, order = -300)
public class DecoratorFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
boolean addClass = false;
if (request instanceof SlingHttpServletRequest) {
final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
final Resource resource = slingRequest.getResource();
final String resourceType = resource.getResourceType();
final String resourcePath = resource.getPath();
// put any custom logic here, eg.:
addClass = "foundation/ponents/carousel".equals(resourceType)
&& resourcePath.startsWith("/content/geometrixx/en");
}
if (addClass) {
final IncludeOptions options = IncludeOptions.getOptions(request, true);
options.getCssClassNames().add("my-class");
}
chain.doFilter(request, response);
}
The Correct way to do is using cq:htmlTag.
Create a unstructured node under your ponent with name cq:htmlTag
Add a property to it "class" of type "String" and the value being "myclass" the class you want to add to your ponent's wrapper/decorator div.
Refer links:
1) Wrapper Div Manipulation
2) Best link to understand this