Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Retrieve First PAR Node from Content Page

I am iterating through all child pages to display their titles and links. But I also need to display the first paragraph node, if it exists.

For example, wow would I retrieve the first PAR node from the following content page?

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

I thought Page class getProperties().get() method would work, but I only see examples returning attributes within jcr:content, not any 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 or another CQ-specific tag, or is this a job for java functions?

like image 907
justacoder Avatar asked Dec 12 '22 16:12

justacoder


1 Answers

You would have to iterate through the child nodes of the child page.

Get the first node with resource type parsys. Once you have that node you can get its path and include 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 on the current page the first parsys component on the child pages. I have not tested this, so there may be some modifications that need to be made to make it work.

like image 189
kfaerber Avatar answered Dec 20 '22 11:12

kfaerber