Customising Teaser V2 Component to Support External URLs (like v1)
Hi everyone,
We originally worked with Teaser v1 for a while now, and only upgraded to v2 to included smart cropping image features. Now I need to extend the Teaser component to support external URLs in the linkURL field, similar to how v1 handled it.
In v2, the getTargetPage() method in TeaserImpl is stricter. It attempts to resolve the linkURL to a Page using PageManager, and if it can't, it falls back to the current page or an action CTA. Here's the current implementation in core AEM:
@9944223
@126844
protected Optional<Page> getTargetPage() {
if (this.targetPage == null) {
String linkURL = resource.getValueMap().get(ImageResource.PN_LINK_URL, String.class);
if (StringUtils.isNotEmpty(linkURL)) {
this.targetPage = Optional.ofNullable(this.resource.getValueMap().get(ImageResource.PN_LINK_URL, String.class))
.map(this.pageManager::getPage).orElse(null);
} else if (actionsEnabled && getActions().size() > 0) {
this.targetPage = getTeaserActions().stream().findFirst()
.flatMap(com.adobe.cq.wcm.core.components.internal.models.v1.TeaserImpl.Action::getCtaPage)
.orElse(null);
} else {
targetPage = currentPage;
}
}
return Optional.ofNullable(this.targetPage);
}
I want to override this behavior so that external URLs like https://www.example.com are accepted as-is, without validation through PageManager.getPage(). The teaser should behave like v1, if it's a valid URL (internal or external), just pass it through.
Thanks in advance for any insights!