I have created a RotatingImagesComponent and allocating banners to that component. As per requirement , need to allow maximum 6 banners in RotatingImagesComponent.
Can someone please let me know how to achieve this ? Do I need to create CMS restriction in this case ?
Thanks..
CodePudding user response:
I think what you need in an interceptor Create interceptor that implements PrepareInterceptor
public class RotatingImagesComponentInterceptor implements PrepareInterceptor<RotatingImagesComponentModel> {
....
@Override
public void onPrepare(RotatingImagesComponentModel model, InterceptorContext ctx) throws InterceptorException {
if (CollectionsUtils.isNotEmpty(model.getBanners()) && model.getBanners().size() > 6)
throw new InterceptorException("Only 6 banners are allowed");
}
}
CodePudding user response:
Hi Below are the steps to Restrict Banner Numbers.
1 Extend RotatingImagesComponent to create custom one.
<relation code="SimpleResponsiveBannerComponentToTestRotatingImagesComponent" localized="false">
<deployment table="BannerToTestRotImgRel" typecode="25003"/>
<sourceElement qualifier="rotatingComponent" type="TestRotatingImagesComponent" cardinality="many"/>
<targetElement qualifier="simpleResponsiveBanners" type="SimpleResponsiveBannerComponent" cardinality="many" collectiontype="list" ordered="true"/>
</relation>
<itemtype code="TestRotatingImagesComponent" extends="RotatingImagesComponent" jalo>
<description>Image carousel</description>
</itemtype>
2 Create new controller to set banneers based on your custom logic.
package com.company.teststorefront.controllers.cms;
import static java.util.stream.Collectors.toList;
import de.hybris.platform.acceleratorcms.model.components.SimpleResponsiveBannerComponentModel;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.company.testcore.model.components.TestRotatingImagesComponentModel;
import com.company.teststorefront.controllers.ControllerConstants;
/**
* Controller for the TestRotatingImagesComponent{@link TestRotatingImagesComponentModel}
*/
@Controller("TestRotatingImagesComponentController")
@RequestMapping(value = ControllerConstants.Actions.Cms.TestRotatingImagesComponent)
public class TestRotatingImagesComponentController
extends AbstractAcceleratorCMSComponentController<TestRotatingImagesComponentModel> {
private static final String IMAGE_CAROUSEL_BANNERS_ATTRIBUTE = "imageCarouselBanners";
private static final int BANNERS_MAX_SIZE = 5;
@Override
protected void fillModel(HttpServletRequest request, Model model, TestRotatingImagesComponentModel component) {
//@formatter:off
List<SimpleResponsiveBannerComponentModel> limitedBannerList = component.getSimpleResponsiveBanners().stream()
.limit(BANNERS_MAX_SIZE)
.collect(toList());
//@formatter:on
model.addAttribute(IMAGE_CAROUSEL_BANNERS_ATTRIBUTE, limitedBannerList);
}
}
3 create testRotatingImagesComponent.jsp and have logic to render banners.
<%@ page trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="ycommerce" uri="http://hybris.com/tld/ycommercetags" %>
<%@ taglib prefix="image" tagdir="/WEB-INF/tags/responsive/image"%>
<div data-autoplayspeed = "${component.timeout}">
<c:forEach items="${imageCarouselBanners}" var="banner" varStatus="status">
<c:if test="${ycommerce:evaluateRestrictions(banner)}">
<c:url value="${banner.urlLink}" var="encodedUrl" />
<div >
<a tabindex="-1" href="${encodedUrl}"<c:if test="${banner.urlLink}"> target="_blank"</c:if>>
<image:damImgModel damAsset="${banner.media}" cssClass="carousel-banner__image"/>
</a>
</div>
</c:if>
</c:forEach>
</div>
4 control this variable BANNERS_MAX_SIZE dynamically by adding in properties and use configurationService.