Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

How to adapt OOTB ContentFragmentList sling model in another sling model

Vikash_kumar_18
Level 1
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
Vijayalakshmi_S
Correct answer by
Community Advisor
Community Advisor

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
Vijayalakshmi_S
Correct answer by
Community Advisor
Community Advisor

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

Vikash_kumar_18
Level 1
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.
Vijayalakshmi_S
Community Advisor
Community Advisor

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