技术文章
在SmartEdit中创建新的结构类型
简介
在这篇博客中,我们将在SmartEdit中创建新的结构类型,并将其分配给一个CMS组件。
我们将加强OOTBCMSTimeRestriction使用cronExpression并添加一个向导链接到该组件,该组件将CMS管理器带到第三方站点。
本博客基于SAP Commerce 1811。
定制化(核心部分)
- 在myextension-items.xml中添加cronExpression属性,扩展OOTB CMSTimeRestriction
.
- 更新myextension-locales_en.properties中的本地化
type.CMSTimeRestriction.cronExpression.name = Cron表达式
- 使用cron表达式支持扩展OOTB CMSTimeRestrictionEvaluator
公共类CustomCMSTimeRestrictionEvaluator扩展CMSTimeRestrictionEvaluator{私有静态最终Logger LOG = LogManager.getLogger(CustomCMSTimeRestrictionEvaluator.class);@Override public boolean evaluate(final CMSTimeRestrictionModel timeRestriction, final RestrictionData context) {final boolean isEvaluate = super。评估(timeRestriction、上下文);return isEvaluate && evaluateCronExpression(timeRestriction);}私有boolean evaluateCronExpression(最终CMSTimeRestrictionModel timeRestriction) {if (StringUtils.isBlank(timeRestriction. getcronexpression())){返回true;} Date now = (Date) getSessionService().getAttribute("previewTime");if (now == null) {now = Boolean.TRUE.equals(timererestriction . getusestoretimezone ()) ?getTimeService().getCurrentTime(): new Date();} try {final CronExpression CronExpression = new CronExpression(timeRestriction.getCronExpression());返回cronExpression.isSatisfiedBy(现在);} catch(最后的ParseException e){日志。error("Provided CronExpression {} is not valid.", timeRestriction.getCronExpression(), e); } return false; } }
- 重写myextension-spring.xml中的bean定义
<属性名="sessionService" ref="sessionService" /> <属性名="timeService" ref="timeService" />
我们的核心功能已经完成。现在CMSTimeRestriction也支持cronExpression, CMS Manager也可以基于cron表达式控制组件可见性。
定制(SmartEdit Part)
在CMSTimeRestriction中新创建的属性(cronExpression)将自动在SmartEdit中可见。让我们开始创建新的CMS结构类型->cronExpressionGuide这将有链接,这需要CMS管理到第三方网站(例如。https://www.freeformatter.com/cron-expression-generator-quartz.html).
- 让我们首先创建一个自定义的SmartEdit扩展。请按照这些链接设置和创建一个自定义扩展。trainingsmartedit。
- 一旦创建了新的扩展,转到trainingsmartedit / web /功能/ trainingsmarteditContainer文件夹并创建一个新的包结构/组件/ genericEditor / cronExpressionGuide (这里我们将定义新的结构类型html)。
- 创建cronExpressionGuideTemplate.html内部cronExpressionGuide文件夹如下所示。
生成Cron表达式
- 现在我们需要创建通用编辑器映射服务来映射新的结构类型cronExpressionGuide到一个虚拟字段(CronExpressionGuide).这个虚拟字段可以分配给CMSTimeRestriction。
- 创建新文件夹配置内部genericEditor(/组件/ genericEditor /配置)并添加一个新文件cmsTrainingGenericEditorMappingService.js在config文件夹下。
- 在cmsTrainingGenericEditorMappingService.js中创建新的字段映射
角。module('cmsTrainingGenericEditorConfigurationServiceModule', ['cmsGenericEditorConfigurationServiceModule', 'editorFieldMappingServiceModule']) .run(function(editorFieldMappingService) {// CronExpressionGuide是虚拟字段editorFieldMappingService。addFieldMapping("CronExpressionGuide", null, null, {template: "cronExpressionGuideTemplate.html"});});
- 这里我们用虚拟字段CronExpressionGuide添加了新创建的结构类型的映射。
- 注入编辑器字段映射服务trainingsmarteditcontainerModule.ts文件。
...........@SeModule({imports: ['smarteditServicesModule', 'abAnalyticsToolbarItemModule', 'cmsTrainingGenericEditorConfigurationServiceModule'], ...........
- 最后将此虚拟字段添加到CMSTimeRestriction。在trainingsmartedit-spring.xml中添加以下bean定义
<属性名="cmsStructureType" value="CronExpressionGuide" /> <属性名="cmsStructureType" value="CronExpressionGuide"DefaultComponentTypeAttributeStructure" p:typecode="CMSTimeRestriction" p:qualifier="cronExpressionGuide"> <属性名="populators">
- 在myextension-locales_en.properties中更新本地化
type.CMSTimeRestriction.cronExpressionGuide.name=Cron表达式指南
- 我们也可以这样定义顺序
<属性名="targetListClass" value="java.util. "LinkedList"/> <属性名="sourceList"> <属性名="constrainedBy"> name activeFrom cronExpression
构建步骤-
确保在SmartEdit设置时,假设您已经完成了蚂蚁npminstall。
- 现在运行蚂蚁清洁一切这将编译我们新添加的服务并生成新的trainingsmarteditContainer.js文件。
- 一旦构建成功,启动Hybris服务器并打开SmartEdit。
- 确保在安装时运行以下impex。
{"smartEditLocation":"/trainingsmartedit/trainingsmartedit/js/trainingsmartedit.js","extends": "cmssmartedit"};applications.trainingsmarteditContainer;{"smartEditContainerLocation":"/trainingsmartedit/trainingsmartedit/js/trainingsmarteditContainer.js","extends": "cmssmarteditContainer"}
- 最后测试更改,创建新的CMSTimeRestriction,您将看到带有“生成Cron表达式”链接的Cron表达式指南。
结论
通过这种方式,我们可以创建任何类型的自定义结构类型,并通过引入虚拟字段将其映射到组件。
谢谢
Shreshtt Bhatt
第一个留下评论
你一定是登录评论:评论或回复一篇文章