Search code examples
spring-mvcsap-commerce-cloud

Restrict Banner numbers in CMS Hybris


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..


Solution

  • 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" jaloclass="com.company.testcore.jalo.components.TestRotatingImagesComponent">
    <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 class="owl-carousel js-owl-carousel js-owl-banner carousel-banner" 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 class="carousel__item">
                    <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.