0

i m working on a piece of cq component, what i need is to retrieve an image filereference path and render the image on the page.

e.g this is my page structure as shows on picture. enter image description here

say i m current on index page, i know how to retrieve index page's children page and get their value map of jcr:content, and get all the properties out from jcr:content...

but i dont know how to retrieve it's jcr:content/image node-> the image, and how to retrieve it's filereference property...

here is my code, it crashes...

<%
     boolean includeTitle = properties.get("includeTitle", false);
     boolean includeImage = properties.get("includeImage", false);
     boolean includeSubTitle = properties.get("includeSubTitle", false);
     boolean includeDescription = properties.get("includeDescription", false);
     String type = currentStyle.get("type", "plain");
%>


<%
    Iterator<Page> currentPageChildren = currentPage.listChildren();
    while(currentPageChildren.hasNext()){
        Page childPage = currentPageChildren.next();
        ValueMap childPageProperties = childPage.getProperties();

        //trying to retrieve the image node
        Node imageNode = childPage.getContentResource("image").adaptTo(Node.class);

        String childPageTitle = childPageProperties.get("jcr:title", String.class);
        String childPageSubTitle = childPageProperties.get("subtitle", String.class);
        String childPageDescription = childPageProperties.get("jcr:description", String.class);
        %>
        <div>
        <% if (includeTitle) { %>
               <p><%= childPageTitle%></p>
        <% }

        if (includeSubTitle) { %>
               <p><%= childPageSubTitle%></p>
        <% }

        if (includeDescription) { %>
               <p><%= childPageDescription%></p>
        <% } %>

        //test to print image's filereference path in string on page
        <p><%=imageNode.getProperty("fileReference") %></p>

        </div>
        <%
    }
%>

please help me with some code example, thx

sefirosu
  • 2,558
  • 7
  • 44
  • 69

1 Answers1

2

I suggest using the Image class, which takes care of null checks. I think in your code you stumble over a page where imageNode is null or has no property fileReference.

Code snippet:

String fileReference = "";
Resource imgRes = childPage.getContentResource("image");
if (imgRes != null) {
    Image image = new Image(imgRes);
    fileReference = image.getFileReference();
}
Thomas
  • 6,325
  • 4
  • 30
  • 65