Enhancing Image Component | Community
Skip to main content
Level 3
November 23, 2022
Solved

Enhancing Image Component

  • November 23, 2022
  • 1 reply
  • 3194 views

Hi all,

I'm enhancing the image component by adding 2 extra fields Tablet image and Mobile image 
I have extended the Image sling models with my CustomImage interface but its not rendering the existing image functionality and my enhancement as well I'm not sure what is missing please help me on the same.

Have added Impl, Interface and HTL.

Interface:

package com.myproject.aem.myproject.division.core.components.models;
import org.osgi.annotation.versioning.ConsumerType;
import com.adobe.cq.wcm.core.components.models.Image;
@ConsumerType
public interface CustomImage extends Image  {
 
    public String getIsInline();
    public String getTabletImage();
    public String getMobileImage();


Thanks and Regards
Hari Prasath V

@arunk @arunpatidar @SantoshSai @LokeshVajrala @Saravanan_Dharmaraj @krati_garg 

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 SantoshSai

Hi @harido ,

I would use Delegation Pattern for Sling Models[0] where logic can be extended by using a Sling delegation pattern. "The Business logic for the core-components is implemented in Sling Models. In case it is necessary to customize the business logic to fulfill project specific requirements the delegation Pattern for Sling Models can be used"

Please refer[1],[2] similar example has been given for Image component.

  • 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 behaviour 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 } }

[0]: https://github.com/adobe/aem-core-wcm-components/wiki/Delegation-Pattern-for-Sling-Models

[1]: https://kiransg.com/2021/11/07/aem-core-component-delegation/

[2]: https://levelup.gitconnected.com/aem-extend-core-component-models-using-resource-type-association-and-delegation-b8855ed281e2

Hope thats helps!

Regards,

Santosh

1 reply

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
November 23, 2022

Hi @harido ,

I would use Delegation Pattern for Sling Models[0] where logic can be extended by using a Sling delegation pattern. "The Business logic for the core-components is implemented in Sling Models. In case it is necessary to customize the business logic to fulfill project specific requirements the delegation Pattern for Sling Models can be used"

Please refer[1],[2] similar example has been given for Image component.

  • 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 behaviour 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 } }

[0]: https://github.com/adobe/aem-core-wcm-components/wiki/Delegation-Pattern-for-Sling-Models

[1]: https://kiransg.com/2021/11/07/aem-core-component-delegation/

[2]: https://levelup.gitconnected.com/aem-extend-core-component-models-using-resource-type-association-and-delegation-b8855ed281e2

Hope thats helps!

Regards,

Santosh

Santosh Sai
HariDoAuthor
Level 3
November 24, 2022

Hi @santoshsai 

yes this helps but I'm not overriding I'm adding 3 new method's(fields) what should i do now i want the core component method's and my 3 new method's as well.

Please help me on this.

Regards
Hari Prasath

SantoshSai
Community Advisor
Community Advisor
November 24, 2022

@harido - That's possible and mentioned in above link - "Delegate OOTB image component and add custom logic on existing methods like getWidth(), getHeight() and getSrcSet() and also add new method getCredit() and getSizes()."

Even if you are adding new methods you have to delegate(it's not overriding) it. Please go through above link to achieve your requirement.

Santosh Sai