How to adapt OOTB ContentFragmentList sling model in another sling model | Community
Skip to main content
May 28, 2021
Solved

How to adapt OOTB ContentFragmentList sling model in another sling model

  • May 28, 2021
  • 3 replies
  • 1815 views

Hi,

I am trying to adapt OOTB ContentFragmentList sling model.

I am extending OOTB ContentFragmentList.

At Line 28, While evaluating the right side, I am getting the expected result as a List of content fragments. But it's throwing below exception while assigning the value to the right side.

Caused by: java.lang.ClassCastException: com.adobe.cq.wcm.core.components.internal.models.v1.contentfragment.ContentFragmentListImpl cannot be cast to com.adobe.cq.wcm.core.components.models.contentfragment.ContentFragmentList

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 Vijayalakshmi_S

Hi @vikash_kumar_18,

I tried reproducing(in AEM 6.5.0, core components version - 2.12.0) what you are trying and I could adapt to ContentFragmentList(CFList) successfully without any issues.

I suggest to cross check the core components version and share below details

  • AEM version and core components version
  • Resource structure of your custom component(inheriting from core CFList) screenshot as authored/available under /content path
  • Complete model class. (Trying using adapters, adapters = {SlingTestModel.class} explicitly, shouldn't be a problem for not having this but just a try)

 

package com.aem.demoproject.core.models; import com.adobe.cq.wcm.core.components.models.contentfragment.ContentFragmentList; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.Self; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.PostConstruct; @Model(adaptables = {SlingHttpServletRequest.class, Resource.class}, adapters = {ContentFragmentListModel.class}, resourceType = ContentFragmentListModel.RESOURCE_TYPE, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class ContentFragmentListModel { public static final String RESOURCE_TYPE = "demoproject/components/content/cfm"; private final Logger LOG = LoggerFactory.getLogger(getClass()); @Self private SlingHttpServletRequest slingRequest; @PostConstruct protected void init() { if (slingRequest != null) { ContentFragmentList cfmList = slingRequest.adaptTo(ContentFragmentList.class); if (cfmList != null) { LOG.info("CF Exported Type={}", cfmList.getExportedType()); // displays demoproject/components/content/cfm correctly LOG.info("CF List size={}", cfmList.getListItems().size()); } } } }

 

 

3 replies

Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
Level 10
May 31, 2021

Hi @vikash_kumar_18,

I tried reproducing(in AEM 6.5.0, core components version - 2.12.0) what you are trying and I could adapt to ContentFragmentList(CFList) successfully without any issues.

I suggest to cross check the core components version and share below details

  • AEM version and core components version
  • Resource structure of your custom component(inheriting from core CFList) screenshot as authored/available under /content path
  • Complete model class. (Trying using adapters, adapters = {SlingTestModel.class} explicitly, shouldn't be a problem for not having this but just a try)

 

package com.aem.demoproject.core.models; import com.adobe.cq.wcm.core.components.models.contentfragment.ContentFragmentList; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.Self; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.PostConstruct; @Model(adaptables = {SlingHttpServletRequest.class, Resource.class}, adapters = {ContentFragmentListModel.class}, resourceType = ContentFragmentListModel.RESOURCE_TYPE, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class ContentFragmentListModel { public static final String RESOURCE_TYPE = "demoproject/components/content/cfm"; private final Logger LOG = LoggerFactory.getLogger(getClass()); @Self private SlingHttpServletRequest slingRequest; @PostConstruct protected void init() { if (slingRequest != null) { ContentFragmentList cfmList = slingRequest.adaptTo(ContentFragmentList.class); if (cfmList != null) { LOG.info("CF Exported Type={}", cfmList.getExportedType()); // displays demoproject/components/content/cfm correctly LOG.info("CF List size={}", cfmList.getListItems().size()); } } } }

 

 

May 31, 2021

Hi @vijayalakshmi_s 

 

I am still getting the same error. Please find the details below - 

  • AEM version - 6.5.7 ( Even I tried with 6.5.0)
  • Core Component version - 2.12.0
  • Custom component structure 

     

  • I even tried with adapters = {SlingTestModel.class}  but no luck.
Vijayalakshmi_S
Level 10
June 1, 2021

Hi @vikash_kumar_18,

Can you try adding the HTL for your component instead of JSP like below. Can remove the dialog if you are not overriding anything. 

Even if you need to override any fields, for now remove the dialog and lets use the dialog inherited from Core -> author the fields and then try building the project.