Expand my Community achievements bar.

SOLVED

Service using Content Fragement

Avatar

Level 2

How to fetch all details from content fragment using services like OSGI services using sling model

 

1 Accepted Solution

Avatar

Correct answer by
Level 9


package com.myorg.core.models;

import java.util.UUID;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Optional;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;


import com.adobe.cq.dam.cfm.ContentFragment;

import static java.util.Objects.nonNull;

@Model(adaptables = { SlingHttpServletRequest.class, Resource.class })
public class ContentFragModel {


@ScriptVariable
private Resource resource;

@ValueMapValue(name = "fragmentPath")
@Deleted Account
private String fragmentPath;

private String title;
private String pageUrl;
private String line1;
private String line2;

@PostConstruct
protected void init() {
if(fragmentPath != null) {
Resource contentRes = resource.getResourceResolver().getResource(fragmentPath);
if (nonNull(contentRes)) {
ContentFragment cf = contentRes.adaptTo(ContentFragment.class);
if (nonNull(cf)) {
title = cf.hasElement("name") ? cf.getElement("name").getContent() : StringUtils.EMPTY;
pageUrl = cf.hasElement("page-url") ? cf.getElement("page-url").getContent() : StringUtils.EMPTY;
line1 = cf.hasElement("line1") ? cf.getElement("line1").getContent() : StringUtils.EMPTY;
line2 = cf.hasElement("line2") ? cf.getElement("line2").getContent() : StringUtils.EMPTY;

}
}
}
}

 

/**
* @Return the title
*/
public String getTitle() {
return title;
}

/**
* @Return the pageUrl
*/
public String getPageUrl() {
return pageUrl;
}

/**
* @Return the line1
*/
public String getLine1() {
return line1;
}

/**
* @Return the line2
*/
public String getLine2() {
return line2;
}


}

View solution in original post

3 Replies

Avatar

Community Advisor

You can write a service with query builder API to fetch the content fragments. You can map the results to Content Fragment model and read the contents.

 

https://aemsimplifiedbynikhil.wordpress.com/2020/10/11/content-fragment-as-java-apis/ - Check this out. 

 

Avatar

Correct answer by
Level 9


package com.myorg.core.models;

import java.util.UUID;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Optional;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;


import com.adobe.cq.dam.cfm.ContentFragment;

import static java.util.Objects.nonNull;

@Model(adaptables = { SlingHttpServletRequest.class, Resource.class })
public class ContentFragModel {


@ScriptVariable
private Resource resource;

@ValueMapValue(name = "fragmentPath")
@Deleted Account
private String fragmentPath;

private String title;
private String pageUrl;
private String line1;
private String line2;

@PostConstruct
protected void init() {
if(fragmentPath != null) {
Resource contentRes = resource.getResourceResolver().getResource(fragmentPath);
if (nonNull(contentRes)) {
ContentFragment cf = contentRes.adaptTo(ContentFragment.class);
if (nonNull(cf)) {
title = cf.hasElement("name") ? cf.getElement("name").getContent() : StringUtils.EMPTY;
pageUrl = cf.hasElement("page-url") ? cf.getElement("page-url").getContent() : StringUtils.EMPTY;
line1 = cf.hasElement("line1") ? cf.getElement("line1").getContent() : StringUtils.EMPTY;
line2 = cf.hasElement("line2") ? cf.getElement("line2").getContent() : StringUtils.EMPTY;

}
}
}
}

 

/**
* @Return the title
*/
public String getTitle() {
return title;
}

/**
* @Return the pageUrl
*/
public String getPageUrl() {
return pageUrl;
}

/**
* @Return the line1
*/
public String getLine1() {
return line1;
}

/**
* @Return the line2
*/
public String getLine2() {
return line2;
}


}