Expand my Community achievements bar.

Get Image from page properties

Avatar

Level 4

Hi,
I want to have a image that i uploaded in  the page properties of the child pages in parent page
Below is my code snippet:

  List<ExistingPagesModel> pagesList = new ArrayList<>();
        Resource parentResource = resourceResolver.getResource("/content/x/us/en/home/test");

        if (parentResource != null) {
            Iterator<Resource> childResources = parentResource.listChildren();
            log.info("childResources: {}");

            while (childResources.hasNext()) {
                Resource childResource = childResources.next();
                log.info("Processing child resource: {}", childResource.getPath());

                // Get property values from child resource
                childTitle = childResource.getValueMap().get("jcr:content/jcr:title", String.class);
                childDescription = childResource.getValueMap().get("jcr:content/jcr:description", String.class);
                childImage = childResource.getValueMap().get("cq:featuredimage/fileReference", String.class);

                // childImage = getChildImageFromResource(childResource);

                log.info("Child Title: {}", childTitle);
                log.info("Child Description: {}", childDescription);
                log.info("childImage: {}", childImage);
                if (childTitle != null) {
                    // Create an ExistingPagesModel object for each child page
                    ExistingPagesModel pageInfo = childResource.adaptTo(ExistingPagesModel.class);
                    if (pageInfo != null) {
                        pageInfo.setChildTitle(childTitle);
                        pageInfo.setChildDescription(childDescription);
                        pageInfo.setChildImage(childImage);
                        pagesList.add(pageInfo);
                    }
                }
            }
        }
        return pagesList;

Can somebody help me with that?
4 Replies

Avatar

Community Advisor

@tatrived You are getting the fileReference URL of the child resource' image and what is the issue you are facing? 

please check this one too

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/use-featured-image-from-pa...

 

 

Avatar

Level 4

In the above code that i have provided i am not getting childImage 
yes i have gone through that article and used in same way it is mentioned in there
Let me know if you have any other solution


Thanks

Avatar

Community Advisor

@tatrived : Can you check what is this statement printing. Specially in a scenario when your parent page is having multiple child pages. 
 log.info("childImage: {}"childImage);
How are you retrieving pagesList -> pageInfo -> childImage .If you can share snippet, it would help visualize how you are reading this information.


Best way right now would be to debug with 1 child page under a parent and see if you get the image url.


Please let me know if it helps.

thanks.

Avatar

Community Advisor

@tatrived : Were you able to solve for this issue? Please let me know if you need any pointers.