How to render the content fragemnet data on page in AEM using sling model
Hi All,
I'm Anil kumar
I have created a content fragment with data fields name, image and biodata. and a component with cFPicker.
I have authored the component with cfPath.Below is my sling model to extract the fragment data on page
but its not working as expected, can any one help me !
package com.gehealthcare.core.internal.models.content.v1;
import com.gehealthcare.core.models.content.SampleGridModel;
import com.gehealthcare.core.models.content.SampleGridItems;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ChildResource;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.List;
@14766979
@Model(adaptables = Resource.class,
adapters = SampleGridModel.class,
resourceType = SampleGirdImpl.RESOURCE_TYPE,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class SampleGirdImpl implements SampleGridModel {
protected static final String RESOURCE_TYPE ="gehealthcare/components/content/samplegrid/v1/samplegrid"; //NOSONAR
@ValueMapValue
private String eyebrow;
@ValueMapValue
private String title;
@ValueMapValue
private String[] fragmentPath;
@ChildResource(name ="gridItems")
public List<SampleGridItems> gridItems = Collections.emptyList();
@SlingObject
private ResourceResolver resourceResolver;
private String fragmentData;
@PostConstruct
protected void init() {
if (fragmentPath != null && fragmentPath.length > 0) {
Resource fragmentResource = resourceResolver.getResource(fragmentPath[0]);
if (fragmentResource != null) {
// Assuming that the Content Fragment has a property named 'data'
fragmentData = fragmentResource.getValueMap().get("data", StringUtils.EMPTY);
}
}
}
@9944223
public String getEyebrow() {
return StringUtils.isNotBlank(eyebrow) ? eyebrow.replaceAll("</?p>", StringUtils.EMPTY) : StringUtils.EMPTY;
}
@9944223
public String getTitle() {
return StringUtils.isNotBlank(title) ? title.replaceAll("</?p>", StringUtils.EMPTY) : StringUtils.EMPTY;
}
public String getFragmentData() {
return fragmentData;
}
}