Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

extending core components

Avatar

Level 3

Hi,

 

I am extending the core component ImageImpl, So i added the below line to my model

public class ImageServiceImpl extends ImageImpl implements Image

where ImageImpl of type com.adobe.cq.wcm.core.components.internal.models.v2.ImageImpl

 

As soon as i build the code the component will breaks because of extending the ImageImpl.

 

Why am i not allowed to extend the core ImageImpl?

1 Accepted Solution

Avatar

Correct answer by
Employee

Hi @bsr060893,

Please check this blog https://levelup.gitconnected.com/aem-extend-core-component-models-using-resource-type-association-an... that outlines how we can extend core component models using resource type association and delegation. It explains about extending core component ImageImpl from scratch.

 

The core component Image component relies on a class called ImageImpl to implement it’s model. You can use the delegation pattern to override the core component model’s behavior and rely on the core Image interface to provide the methods you need:

 

 

 

import com.adobe.cq.wcm.core.components.models.Image;
import lombok.experimental.Delegate;
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.Via;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import org.apache.sling.models.annotations.via.ResourceSuperType;

@Model(
adaptables = {Resource.class, SlingHttpServletRequest.class},
adapters = Image.class, // Adapts to the CC model interface
resourceType = "demo/components/content/image", // Maps to OUR component, not the CC component
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL // No properties? No problem!
)
public class CustomImage implements Image { // We will be honoring the contract between the HTL and the model implementation

@Self // Indicates that we are resolving the current resource
@Via(type = ResourceSuperType.class) // Resolve not as this model, but as the model of our supertype (ie: CC Image)
@Delegate(excludes = DelegationExclusion.class) // Delegate all our methods to the CC Image except those defined below
private Image delegate;

@ValueMapValue
private boolean useOriginal; // This is a new property that we are introducing

@ValueMapValue
protected String fileReference; // This is the CC Image property that point to the image asset location in the DAM

@Override
public String getSrc() {
// If useOriginal is checked, then serve the raw asset in full resolution, otherwise,
// delegate this method to the CC Image
return useOriginal ? fileReference : delegate.getSrc();
}

private interface DelegationExclusion { // Here we define the methods we want to override
String getSrc(); // Override the method which determines the source of the asset
}
}
view rawCustomImage.java hosted with ❤ by GitHub

 

 

 

Thanks!!

View solution in original post

3 Replies

Avatar

Employee Advisor

Check the AEM error.log file for the error you are getting that will give a trace of the error that is coming up.

 

Also, confirm the AEM Core Component version you are using. If you are using 2.8.0 then there is a bug which has been fixed and similar issue has been discussed in the below link please check once:

 

https://github.com/adobe/aem-core-wcm-components/issues/928

Avatar

Level 3
I am using 2.8.0. will check with the latest. Thank you

Avatar

Correct answer by
Employee

Hi @bsr060893,

Please check this blog https://levelup.gitconnected.com/aem-extend-core-component-models-using-resource-type-association-an... that outlines how we can extend core component models using resource type association and delegation. It explains about extending core component ImageImpl from scratch.

 

The core component Image component relies on a class called ImageImpl to implement it’s model. You can use the delegation pattern to override the core component model’s behavior and rely on the core Image interface to provide the methods you need:

 

 

 

import com.adobe.cq.wcm.core.components.models.Image;
import lombok.experimental.Delegate;
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.Via;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import org.apache.sling.models.annotations.via.ResourceSuperType;

@Model(
adaptables = {Resource.class, SlingHttpServletRequest.class},
adapters = Image.class, // Adapts to the CC model interface
resourceType = "demo/components/content/image", // Maps to OUR component, not the CC component
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL // No properties? No problem!
)
public class CustomImage implements Image { // We will be honoring the contract between the HTL and the model implementation

@Self // Indicates that we are resolving the current resource
@Via(type = ResourceSuperType.class) // Resolve not as this model, but as the model of our supertype (ie: CC Image)
@Delegate(excludes = DelegationExclusion.class) // Delegate all our methods to the CC Image except those defined below
private Image delegate;

@ValueMapValue
private boolean useOriginal; // This is a new property that we are introducing

@ValueMapValue
protected String fileReference; // This is the CC Image property that point to the image asset location in the DAM

@Override
public String getSrc() {
// If useOriginal is checked, then serve the raw asset in full resolution, otherwise,
// delegate this method to the CC Image
return useOriginal ? fileReference : delegate.getSrc();
}

private interface DelegationExclusion { // Here we define the methods we want to override
String getSrc(); // Override the method which determines the source of the asset
}
}
view rawCustomImage.java hosted with ❤ by GitHub

 

 

 

Thanks!!