Inclusion of content fragment path along with model json | Community
Skip to main content
thatsmeadarsh
Level 3
March 8, 2021
Solved

Inclusion of content fragment path along with model json

  • March 8, 2021
  • 3 replies
  • 1586 views

Hi All,

As of now AEM OOB model.json expose content fragment elements but not the fragment path. We have a requirement to expose the fragment path along with the model.json as part of our headless CMS requirement. I found that the Exporter contains the fragmentPath variable but not getter for that.

aem-core-wcm-components/ContentFragmentImpl.java at master · adobe/aem-core-wcm-components · GitHub

As this is inside internal package structure no idea on how to override this. What would be the solution to achieve the additional fragmentPath property?

 

Thanks

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SureshDhulipudi

You can extend any core component or customize functionality.

Try like this: 

Step 1: Create an Interface to extend ContentFragment class and add a method to return path

Step 2: Implement the class , use Delegation

 

==== Declare an Interface to ContentFragment Extention Class =========


import com.adobe.cq.wcm.core.components.internal.models.v1.contentfragment;

/**
* An extension to the AEM Core Component ContentFragment
*/
public interface ContentFragmentExtention extends ContentFragment {

default String contentFragmentPath() {
throw new UnsupportedOperationException();
}

}

 

 

Step2:

 

==== Implementation your interface ContentFragmentExtention / Class =========

/**
* An extension to the AEM Core Component ContentFragment
*/
@Model(
adaptables = SlingHttpServletRequest.class,
adapters = { ContentFragmentExtention.class, ContentFragment.class, ComponentExporter.class },
resourceType = ContentFragmentExtentionImpl.RESOURCE_TYPE
)
@3484101(
name = ExporterConstants.SLING_MODEL_EXPORTER_NAME,
extensions = ExporterConstants.SLING_MODEL_EXTENSION
)
public class ContentFragmentExtentionImpl implements ContentFragmentExtention {

public static final String RESOURCE_TYPE = "<path to your proxy ContentFragment path> /apps/project/proxy/componetns/contentfragment";

@1961677 @2434638(type = ResourceSuperType.class)
@Delegate(excludes = DelegationExclusion.class)
private ContentFragment contentFragment;


@ValueMapValue
private String contentFragmentPath; // This is a new property that we are introducing


/**
* implement your method
*
* @2007960 <your path>
*/
@9944223
public String contentFragmentPath() {
<< Logic to get the path >>
return <Path to contentfragment>; //contentFragmentPath
}


}

3 replies

SureshDhulipudi
Community Advisor
SureshDhulipudiCommunity AdvisorAccepted solution
Community Advisor
March 8, 2021

You can extend any core component or customize functionality.

Try like this: 

Step 1: Create an Interface to extend ContentFragment class and add a method to return path

Step 2: Implement the class , use Delegation

 

==== Declare an Interface to ContentFragment Extention Class =========


import com.adobe.cq.wcm.core.components.internal.models.v1.contentfragment;

/**
* An extension to the AEM Core Component ContentFragment
*/
public interface ContentFragmentExtention extends ContentFragment {

default String contentFragmentPath() {
throw new UnsupportedOperationException();
}

}

 

 

Step2:

 

==== Implementation your interface ContentFragmentExtention / Class =========

/**
* An extension to the AEM Core Component ContentFragment
*/
@Model(
adaptables = SlingHttpServletRequest.class,
adapters = { ContentFragmentExtention.class, ContentFragment.class, ComponentExporter.class },
resourceType = ContentFragmentExtentionImpl.RESOURCE_TYPE
)
@3484101(
name = ExporterConstants.SLING_MODEL_EXPORTER_NAME,
extensions = ExporterConstants.SLING_MODEL_EXTENSION
)
public class ContentFragmentExtentionImpl implements ContentFragmentExtention {

public static final String RESOURCE_TYPE = "<path to your proxy ContentFragment path> /apps/project/proxy/componetns/contentfragment";

@1961677 @2434638(type = ResourceSuperType.class)
@Delegate(excludes = DelegationExclusion.class)
private ContentFragment contentFragment;


@ValueMapValue
private String contentFragmentPath; // This is a new property that we are introducing


/**
* implement your method
*
* @2007960 <your path>
*/
@9944223
public String contentFragmentPath() {
<< Logic to get the path >>
return <Path to contentfragment>; //contentFragmentPath
}


}

thatsmeadarsh
Level 3
March 8, 2021
Thanks so much. I'll try to see if it is working. Thanks again 🙂
arunpatidar
Community Advisor
Community Advisor
March 8, 2021

In case if you are looking for a custom solution https://aemlab.blogspot.com/2019/07/get-json-response-of-aem-page.html

Note: I did this as part of PoC, so not a production-ready code.

Arun Patidar
thatsmeadarsh
Level 3
March 8, 2021
Thanks for the response, really appreciate but customizing existing model json is what I am looking for.