Fetch Page title, description, thumbnail image of one page in Another page through dialog. | Community
Skip to main content
Level 4
Jul 18, 2022 12:00 PM UTC
Solved

Fetch Page title, description, thumbnail image of one page in Another page through dialog.

Hello all, 

I have created a component which should accept only page path through cq:dialog. When I select the page and click submit through dialog then it should print all the page properties like title, description& image or image url . Is it possible ??

 Here is my component dialog:

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
DEBAL_DAS
DEBAL_DASAccepted solution
New Participant
Jul 19, 2022 9:59 AM UTC

1. Page path field in Component touch ui dialog editor -

 

2. Sling model -

package com.aem.demo.core.models;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;


@Model(adaptables = Resource.class , defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class PagePropertydetailModel {

	@ValueMapValue
	private String pagepath;
	
	@SlingObject
    private ResourceResolver resourceResolver;
	
	private Map<String,Object> pageProperties;
	
	@PostConstruct
	protected void init() {
		
		
		PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
		Page page = pageManager.getPage(pagepath);
		pageProperties = page.getProperties();
		
		
		
		
		
	}

	public Map<String, Object> getPageProperties() {
		return pageProperties;
	}
}

3. Htl code -

Page Information</br>

<div data-sly-use.map="com.aem.demo.core.models.PagePropertydetailModel">

  <sly data-sly-list="${map.pageProperties.keySet.iterator}">

     <li>key = ${item}</li>    
      ${map.pageProperties[item]}


   </sly>

</div>

4. Rendered data as shown below  -

Just shared the idea. I will update logic for Date field like jcr:created and cq:lastModified

 

6 replies

DEBAL_DAS
DEBAL_DASAccepted solution
New Participant
Jul 19, 2022 9:59 AM UTC

1. Page path field in Component touch ui dialog editor -

 

2. Sling model -

package com.aem.demo.core.models;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;


@Model(adaptables = Resource.class , defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class PagePropertydetailModel {

	@ValueMapValue
	private String pagepath;
	
	@SlingObject
    private ResourceResolver resourceResolver;
	
	private Map<String,Object> pageProperties;
	
	@PostConstruct
	protected void init() {
		
		
		PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
		Page page = pageManager.getPage(pagepath);
		pageProperties = page.getProperties();
		
		
		
		
		
	}

	public Map<String, Object> getPageProperties() {
		return pageProperties;
	}
}

3. Htl code -

Page Information</br>

<div data-sly-use.map="com.aem.demo.core.models.PagePropertydetailModel">

  <sly data-sly-list="${map.pageProperties.keySet.iterator}">

     <li>key = ${item}</li>    
      ${map.pageProperties[item]}


   </sly>

</div>

4. Rendered data as shown below  -

Just shared the idea. I will update logic for Date field like jcr:created and cq:lastModified

 

Translate
Level 4
Jul 19, 2022 10:50 AM UTC

Hi @debal_das  Thank you for the help. I just need to elaborate my query actually I'm having an article page with title, description & image. So when I configure that article page path in dialog it should get the corresponding title, description & image(thumbnail image if it's have ) Can I do this with the above code ??

Translate
DEBAL_DAS
New Participant
Jul 19, 2022 11:39 AM UTC

Please correct me after configuring the article page in dialog editor , you want to get the corresponding title, description & image(thumbnail image if it's have ) in dialog editor right?

Translate
Manu_Mathew_
New Participant
Jul 18, 2022 5:14 PM UTC

@tessa_learner1 Yes its possible to do in the sling model.

 

@Model(adaptable=Resource.class)

public class TestClass {

@Self

Resource resource;

@PostConstruct

public void init() {

  PageManager pm = resource.getResourceResolver().adaptTo(PageManager.class);

  Page containingPage = pm.getContainingPage (resource);

  ValueMap pageProperties = containingPage.getProperties();

}

}

 

map the properties to variables and then use it in the HTL.

Translate
Sachin_Arora_
New Participant
Jul 18, 2022 12:21 PM UTC

You can write a sling model for custom component. Using resource resolver you can get resource and adapt to Page. Once you adaptTo Page, you can iterate all properties. Please refer below links for more details.

 

Translate