Expand my Community achievements bar.

SOLVED

How to adapt OOTB ContentFragmentList sling model in another sling model

Avatar

Level 1

Hi,

I am trying to adapt OOTB ContentFragmentList sling model.

I am extending OOTB ContentFragmentList.

Vikash_kumar_18_0-1622232178706.png

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

1 Accepted Solution

Avatar

Correct answer by
Level 10

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());
            }
        }
    }
}

 

 

View solution in original post

3 Replies

Avatar

Correct answer by
Level 10

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());
            }
        }
    }
}

 

 

Avatar

Level 1

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 Vikash_kumar_18_0-1622489592181.png

     

  • I even tried with adapters = {SlingTestModel.class}  but no luck.

Avatar

Level 10

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. 

Vijayalakshmi_S_0-1622555765818.png