justacoder
justacoder

Reputation: 2704

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?

Upvotes: 4

Views: 10006

Answers (4)

kfaerber
kfaerber

Reputation: 1327

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.

Upvotes: 9

atgar
atgar

Reputation: 216

You could also try with this:

<%@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 current resource.

Upvotes: 2

Woodifer
Woodifer

Reputation: 1019

If you are trying to reference the parsys from one page to another, I would use the out of the box reference component. This component accepts a path to a component any where on your site, and displays it on the page of your choice.

Upvotes: 1

Bikash Rath
Bikash Rath

Reputation: 159

Every node in CQ5 repository can be represented as a Resource. You can get the resource by using 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

Upvotes: 1

Related Questions