Need to access JCR Child Nodes after CQ:Include | Community
Skip to main content
bob_smith1
Level 2
October 16, 2015
Solved

Need to access JCR Child Nodes after CQ:Include

  • October 16, 2015
  • 3 replies
  • 2803 views

I have a component that uses <cq:include> to add multiple child components inside a main component. However, <cq:include> doesn't actually add the child components in the JCR under the parent.

Because of this the parent node, currentNode.getNodes() returns 0 and looking at CRXDE there are no child nodes.  Is there a way to programmatically add child components into the main component so that the nodes will actually appear in the JCR?

I need to access these nodes because I want to be able to create, delete, move, and update the child nodes before any authored content is entered in.
 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by ANKUR_CHAUHAN

Hey,

There is a simple approach for doing this. Just go to your parent component & add a cq:template node with jcr:primaryType  "nt:unstructured". under this node add one more node of primaryType nt:unstructured for your child component.

it will looks like-

<parentComponent>

-----cq:template 

------- <childCompoonent>

<child component > name must be same as you used in your <cq:include path="" > tag path property.

at this node set sling:resourceType with a value of </apps/your/component/path>.

No code change required after doing this as when you drop parent component then child component node will be created under parent node.

 

Happy Coding

3 replies

smacdonald2008
Level 10
October 16, 2015

From within you component - get the parent node. Then pass the Node to an OSGi custom service:

https://helpx.adobe.com/experience-manager/using/passing_nodes.html

Now that you have the node in a Java custom service - you can use the JCR API to add child nodes to it, add props, etc. 

See this article to learn how to use JCR API to add nodes:

https://helpx.adobe.com/experience-manager/using/programmatically-accessing-cq-content-using.html

See this article to learn how to use the JCR API from within an OSGi bundle:

https://helpx.adobe.com/experience-manager/using/querying-experience-manager-data-using1.html

SO from your custom AEM service that manipulates the JCR - you have a lot of flexibility. 

ANKUR_CHAUHAN
ANKUR_CHAUHANAccepted solution
Level 2
October 16, 2015

Hey,

There is a simple approach for doing this. Just go to your parent component & add a cq:template node with jcr:primaryType  "nt:unstructured". under this node add one more node of primaryType nt:unstructured for your child component.

it will looks like-

<parentComponent>

-----cq:template 

------- <childCompoonent>

<child component > name must be same as you used in your <cq:include path="" > tag path property.

at this node set sling:resourceType with a value of </apps/your/component/path>.

No code change required after doing this as when you drop parent component then child component node will be created under parent node.

 

Happy Coding

Level 5
October 16, 2015

I had the same situation in Slider Component. I used the custom tag to create a node with little trick. Below is the snippet :

<c:forEach var="grid" items="${gridCollection}" varStatus="theCount"> <c:if test = "${grid.value eq 'grid1'}"> <div class="slide"> <c:forEach var="cssclass" items="${grid1}" varStatus="theCount"> <createcss1:createcssclass1 path="${grid.key}" nodename="${cssclass}" /> <cq:include path="${grid.key}/${cssclass}" resourceType = "mysite/components/content/gridfeature" /> </c:forEach> </div> </c:if> </c:forEach>