How to get the first PAR Node on the content page

I repeat all the child pages to display their names and links. But I also need to display the first paragraph of the node, if it exists.

For example, wow would I get the first PAR node from the next content page?

/content /foo /jcr:content /title /par <- need this one /par /image 

I thought that no classlow getProperties().get() would work, but I only see examples returning the attributes inside jcr: content, and not the child nodes below it.

  ArrayList aChildren = new ArrayList(); String listroot = properties.get("listRoot", currentPage.getPath()); Page rootPage = pageManager.getPage(listroot); if (rootPage != null) { Iterator<Page> children = rootPage.listChildren(new PageFilter(request)); while (children.hasNext()) { Page child = children.next(); out.println( child.getTitle() + "<br>" ); //Output first PAR tag of this page here } } 

Can this be done with either another tag specific to CQ, or is this a job for java functions?

+4
source share
4 answers

You will have to iterate over the child nodes of the child page.

Get the first node with resource type parsins. Once you have this node, you can get its path and enable it on the current page.

 Resource childResource = resourceResolver.getResource(child.getPath()); Node childNode = childResource.adaptTo(Node.class); Node jcrContent = childNode.getNode("jcr:content"); NodeIterator childrenNodes = jcrContent.getNodes(); while(childrenNodes.hasNext()){ Node next = childrenNodes.nextNode(); String resourceType = next.getProperty("sling:resourceType").getString(); if(resourceType.equals("foundation/components/parsys")){ %><cq:include path="<%= next.getPath() %>" resourceType="foundation/components/parsys" /><% break; } } 

This will embed the first parsys component on the child pages on the current page. I have not tested this, so there may be some changes that need to be made to make it work.

+8
source

You can also try:

 <%@page session="false" import="com.day.cq.wcm.foundation.Paragraph, com.day.cq.wcm.foundation.ParagraphSystem"%> <% ParagraphSystem parSys = ParagraphSystem.create(resource, slingRequest); for (Paragraph par: parSys.paragraphs()){ 

With this, you can iterate through the parsys nodes that are under the current resource.

+2
source

Each node in the CQ5 repository can be represented as a Resource . You can get the resource using the following code

 //resolver being instance of org.apache.sling.api.resource.ResourceResolver Resource paraResource = resolver.getResource("path of the paragraph"); 

Then you can manipulate on the resource

+1
source

If you are trying to link to parsys from one page to another, I would use a component. This component takes the path to the component anywhere on your site and displays it on the page of your choice.

+1
source

Source: https://habr.com/ru/post/1469279/


All Articles