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

Extend Teaser Component using Sling Model Delegate Pattern

Avatar

Avatar
Springboard
Level 3
subrato_kha
Level 3

Likes

10 likes

Total Posts

49 posts

Correct Reply

2 solutions
Top badges earned
Springboard
Establish
Validate 1
Ignite 5
Ignite 3
View profile

Avatar
Springboard
Level 3
subrato_kha
Level 3

Likes

10 likes

Total Posts

49 posts

Correct Reply

2 solutions
Top badges earned
Springboard
Establish
Validate 1
Ignite 5
Ignite 3
View profile
subrato_kha
Level 3

24-11-2020

Hi,

I have been trying to extend Teaser component behaviour using Sling Delegate Pattern. I want to overwrite the linkUrl property behaviour but it is not working as expected. The link on the image is always the one which is configured on the component but I expect it to be overwritten by the static link from the below overwritten method.

 

import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.wcm.core.components.models.ListItem;
import com.adobe.cq.wcm.core.components.models.Teaser;
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.via.ResourceSuperType;

import java.util.List;

@Model(
adaptables = SlingHttpServletRequest.class,
adapters = {Teaser.class, ComponentExporter.class},
resourceType = "myproject/components/content/teaser",
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
public class CustomTeaserImpl implements Teaser {

@Self
@Via(type = ResourceSuperType.class)
private Teaser teaser;

@Override
public String getLinkURL() {
return "/content/subrato/foo";
}

/**
* Checks if the teaser has Call-to-Action elements
*
* @return {@code true} if teaser has CTAs, {@code false} otherwise
*
* @since com.adobe.cq.wcm.core.components.models 12.4.0
*/
@Override
public boolean isActionsEnabled() {
return this.teaser.isActionsEnabled();
}

/**
* Returns the list of Call-to-Action elements
*
* @return the list of CTAs
*
* @since com.adobe.cq.wcm.core.components.models 12.4.0
*/
@Override
public List<ListItem> getActions() {
return this.teaser.getActions();
}

/**
* Returns the image resource for this teaser.
*
* @return the image resource for this teaser or {@code null}
*
* @since com.adobe.cq.wcm.core.components.models 12.4.0
*/
@Override
public Resource getImageResource() {
return this.teaser.getImageResource();
}

/**
* Checks if the link on the image is hidden.
*
* @return {@code true} if link is hidden on the image, {@code false} otherwise
*
* @since com.adobe.cq.wcm.core.components.models 12.4.0
*/
@Override
public boolean isImageLinkHidden() {
return this.teaser.isImageLinkHidden();
}

/**
* Returns this teaser's pretitle, if one was defined.
*
* @return the teaser's pretitle or {@code null}
*
* @since com.adobe.cq.wcm.core.components.models 12.12.0
*/
@Override
public String getPretitle() {
return this.teaser.getPretitle();
}

/**
* Returns this teaser's title, if one was defined.
*
* @return the teaser's title or {@code null}
*
* @since com.adobe.cq.wcm.core.components.models 12.4.0
*/
@Override
public String getTitle() {
return this.teaser.getTitle();
}

/**
* Checks if the link on the title is hidden.
*
* @return {@code true} if link is hidden on the title, {@code false} otherwise
*
* @since com.adobe.cq.wcm.core.components.models 12.4.0
*/
@Override
public boolean isTitleLinkHidden() {
return this.teaser.isTitleLinkHidden();
}

/**
* Returns this teaser's description, if one was defined.
*
* @return the teaser's description or {@code null}
*
* @since com.adobe.cq.wcm.core.components.models 12.4.0
*/
@Override
public String getDescription() {
return this.teaser.getDescription();
}

/**
* Returns the HTML element type (h1-h6) used for the title.
*
* @return the element type
*
* @since com.adobe.cq.wcm.core.components.models 12.4.0
*/
@Override
public String getTitleType() {
return this.teaser.getTitleType();
}

/**
* @since com.adobe.cq.wcm.core.components.models 12.4.0
*/
@Override
public String getExportedType() {
return this.teaser.getExportedType();
}
}

@Theo_Pendle 

Accepted Solutions (0)

Answers (4)

Answers (4)

Avatar

Avatar
Give Back 3
MVP
Manjunath_K
MVP

Likes

139 likes

Total Posts

188 posts

Correct Reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile

Avatar
Give Back 3
MVP
Manjunath_K
MVP

Likes

139 likes

Total Posts

188 posts

Correct Reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile
Manjunath_K
MVP

24-11-2020

Hi @subrato_kha,

The image section on Teaser component will be rendered from the proxy component defined with imageDelegate property on component node. so, adding custom Sling Model for Teaser component using Sling Delegate Pattern & passing static link from overridden method will not work in this case because link on image will be referred from Sling Model object "com.adobe.cq.wcm.core.components.models.Image" (i.e. ${image.link})

 

imageDelegate property on Teaser component:

refer this : https://github.com/adobe/aem-core-wcm-components/blob/master/content/src/content/jcr_root/apps/core/...

teaser1.png

 

 

teaser2.png

 

Hope this helps!

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,357 likes

Total Posts

3,228 posts

Correct Reply

918 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,357 likes

Total Posts

3,228 posts

Correct Reply

918 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

24-11-2020

@Model(adaptables = SlingHttpServletRequest.class, adapters = {Teaser.class,CustomTeaser.class}, resourceType = MyConstants.TEASER_RESOURCE_TYPE, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class CustomTeaser implements Teaser {

@Self
	@Via(type = ResourceSuperType.class)
	private Teaser teaser;

private String newLinkUrl;

@PostConstruct
	protected void init() {
		newLinkUrl = "/content/mypro/lang/somepath";
	}

	public String getLinkURL() {
		return newLinkUrl;
	}
}

Avatar

Avatar
Ignite 1
MVP
SureshDhulipudi
MVP

Likes

163 likes

Total Posts

174 posts

Correct Reply

49 solutions
Top badges earned
Ignite 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile

Avatar
Ignite 1
MVP
SureshDhulipudi
MVP

Likes

163 likes

Total Posts

174 posts

Correct Reply

49 solutions
Top badges earned
Ignite 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile
SureshDhulipudi
MVP

24-11-2020

declare LinkURL ValueMap variable

 

@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)

private String linkURL;

 

 

====

The best practice is to declare interface and then implement:

1) CustomTeaser Interface

 

public interface CustomTeaser extends Teaser {

 default String getLinkURL() {

      ///

}

 

----

2) Implement CustomTeaser interface

 

@Model(
adaptables = SlingHttpServletRequest.class,
adapters = {CustomTeaser.class,Teaser.class, ComponentExporter.class},
resourceType = "myproject/components/content/teaser",
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
public class CustomTeaserImpl implements CustomTeaser {


@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)

private String linkURL;

@Override
public String getLinkURL() {
return "/content/subrato/foo";
}
}

Avatar

Avatar
Give Back 3
MVP
Manjunath_K
MVP

Likes

139 likes

Total Posts

188 posts

Correct Reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile

Avatar
Give Back 3
MVP
Manjunath_K
MVP

Likes

139 likes

Total Posts

188 posts

Correct Reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile
Manjunath_K
MVP

24-11-2020

Hi @subrato_kha,

The image section on Teaser component will be rendered from the proxy component defined with imageDelegate property on component node. so, adding custom Sling Model for Teaser component using Sling Delegate Pattern & passing static link from overridden method will not work in this case because link on image will be referred from Sling Model object "com.adobe.cq.wcm.core.components.models.Image"(i.e. ${image.link}).

 

imageDelegate property on Teaser component:

refer this : https://github.com/adobe/aem-core-wcm-components/blob/master/content/src/content/jcr_root/apps/core/...

teaser1.png

 

 

teaser2.png

 

Hope this helps!