Hi All,
What all the ootb objects available in granite rendercondition.
I have browsed and few of the blog mentioned requestPathinfo is available so like this What other ootb objects available in granite rendercondition to use in component dialog.
해결되었습니다! 솔루션으로 이동.
토픽은 커뮤니티 콘텐츠를 분류하여 관련성 있는 콘텐츠를 찾는 데 도움이 됩니다.
hi @AmitSi18,
The granite render condition is marked with "@Model," indicating that it is a Sling Model. You can utilise all the available injectors and the out-of-the-box (OOTB) global objects as outlined in the documentation. Here are some examples demonstrating how different Java objects are injected into the implementation:
https://ankanghosh-webdev.medium.com/granite-render-condition-in-aem-64d32f03a7d1
https://jpsoares.medium.com/aem-granite-render-conditions-438c804b1e5a
조회 수
답글
좋아요 수
Hi @AmitSi18 ,
Like you are using requestPathInfo there are many ootb objects in aem which you can access In AEM. You can access all the objects which are injected in a sling model. So there are many objects like SlingHttpServletRequest, Resource, ValueMap, ResourceResolver, Session, and Authorizable for user and request context. You can also access RequestPathInfo for URL details, WCMMode for authoring modes, and Page, PageManager for page-related context. Additionally, Component, ComponentContext, and Bindings are available to control field visibility dynamically based on user roles, request parameters, and authoring mode.
조회 수
답글
좋아요 수
Hi @AmitSi18 ,
In Granite RenderCondition (used in AEM dialogs to conditionally show/hide fields), the underlying implementation uses Sling Models, meaning you have access to all Sling injectable objects that apply in the context of a dialog rendering request.
Complete list of OOTB objects that you can inject into a Granite RenderCondition class using Sling Models:
1. SlingHttpServletRequest (@Self)
- Current HTTP request object.
- Use it to access request parameters, headers, attributes, etc.
2. Resource (@Self)
- The resource node for the dialog field.
- You can read properties directly from this node.
3. ValueMap (@Via("resource") @ValueMapValue)
- Used to get dialog-specific properties like any custom configurations defined in the dialog node.
4. ResourceResolver (@Inject)
- Allows you to access the repository, traverse resources, etc.
5. RequestPathInfo (@Inject)
- Helps you extract selectors, suffix, extension, etc. from the URL.
6. Page (@ScriptVariable)
- The current page in context.
- Helpful to check path, template, or hierarchy-based conditions.
7. PageManager (@ScriptVariable)
- Used to work with pages — get parent, children, etc.
8. WCMMode (@ScriptVariable)
- Tells you the current authoring mode (EDIT, PREVIEW, etc.).
- Useful when you want to show/hide a field only in authoring mode.
9. ComponentContext (@ScriptVariable)
- Gives context about the component being rendered.
10. SlingBindings (@Inject)
- Contains all the request bindings like currentNode, resource, pageManager, etc.
- You can access the whole context via bindings.
11. Authorizable (@Inject)
- Represents the current user.
- Use it if you want to show/hide fields based on user roles or groups.
12. Session (@Inject)
- The JCR session associated with the request.
- Usually not required for simple conditions, but available if needed.
Example: Custom RenderCondition Java Class
@Model(adaptables = SlingHttpServletRequest.class)
public class MyRenderCondition implements RenderCondition {
@Self
private SlingHttpServletRequest request;
@Self
private Resource resource;
@Inject
private RequestPathInfo requestPathInfo;
@ScriptVariable
private Page currentPage;
@ScriptVariable
private WCMMode wcmMode;
private RenderCondition condition;
@PostConstruct
protected void init() {
boolean show = currentPage.getPath().startsWith("/content/mysite") &&
wcmMode == WCMMode.EDIT &&
requestPathInfo.getSelectorString() != null;
condition = new SimpleRenderCondition(show);
}
@Override
public boolean check() {
return condition.check();
}
}
If you want to debug or log all available objects, you can use:
Bindings bindings = (Bindings) request.getAttribute(SlingBindings.class.getName());
bindings.keySet().forEach(k -> log.info("Binding key: {}", k));
This helps you discover any other available context variables dynamically during request.
Regards,
Amit
조회 수
답글
좋아요 수
@giuseppebag @ShivamKumar @AmitVishwakarma
I am just trying to understand this.
I have not written and custom servlet or sling model for requestPathInfo but I am still able to use it in rendercondition expression(refer below code). But other object are not available like resource, request etc... mentioned by @AmitVishwakarma
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
jcr:primaryType="nt:unstructured"
jcr:title="Properties"
sling:resourceType="cq/gui/components/authoring/dialog" xmlns:granite="http://www.adobe.com/jcr/granite/1.0">
<content
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/fixedcolumns">
<items jcr:primaryType="nt:unstructured">
<column
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/container">
<items jcr:primaryType="nt:unstructured">
<text
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
fieldLabel="Text"
name="./text"/>
<myTextField
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
fieldDescription="This is a conditional text show hide text field"
fieldLabel="My Text Field"
name="./myTextField">
<granite:rendercondition
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/renderconditions/simple"
expression="${granite:containsIgnoreCase(requestPathInfo.suffix, '/en/home-page')}"/>
</myTextField>>
</items>
</column>
</items>
</content>
</jcr:root>
@Model(adaptables = Resource.class)
public class HelloWorldModel {
@ValueMapValue(name=PROPERTY_RESOURCE_TYPE, injectionStrategy=InjectionStrategy.OPTIONAL)
@Default(values="No resourceType")
protected String resourceType;
@SlingObject
private Resource currentResource;
@SlingObject
private ResourceResolver resourceResolver;
private String message;
@ValueMapValue(injectionStrategy=InjectionStrategy.OPTIONAL)
private String text;
@PostConstruct
protected void init() {
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
String currentPagePath = Optional.ofNullable(pageManager)
.map(pm -> pm.getContainingPage(currentResource))
.map(Page::getPath).orElse("");
message = "Hello World!\n"
+ "Resource type is: " + resourceType + "\n"
+ "Current page is: " + currentPagePath + "\n";
}
public String getMessage() {
return message;
}
}
조회 수
답글
좋아요 수