Skip to Content
Technical Articles

SAP Commerce | Enable Default Value of Boolean Field in SmartEdit

Introduction

Generic Editor  allow us to edit components in the SmartEdit interface. OOTB SmartEdit doesn’t support defaultValue to be display while creating new component.

But OOTB BooleanComponent (AngularJS) has the capability of handling Default Value and expect the below JSON response from CMS Structure API

 {
    cmsStructureType: "Boolean",
    qualifier: "someQualifier",
    i18nKey: 'i18nkeyForSomeQualifier',
    localized: false,
    defaultValue: true
 }

Here we can see that this BooleanComponent accept defaultValue as well and can show toggle button ON/OFF accordingly.

This blog will be based on SAP Commerce 1811.

Problem

Some CMS Components have default value true/false (defined in items.xml) for their Boolean type fields but whenever we create these CMS Component in SmartEdit, we can not see toggle button ON/OFF as per their defaultValue.

For Example, All CMS Component has visible boolean field and by default its True, but we can’t see visible toggle ON by default in SmartEdit whenever we create any component.

Although BooleanComponent support defaultValue but OOTB CMS Structure API doesn’t have defaultValue field defined.

We can modify its metadata by extending ComponentTypeAttributeData DTO with defaultValue.

 

Customization

The customization can be done in any extension but i would recommend to create one custom SmartEdit extension and put all kind of SmartEdit customization inside that extension only.

  • Lets first create one custom SmartEdit extension. Please follow these links to setup & create one custom extension e.g. trainingsmartedit.

https://help.sap.com/viewer/9d346683b0084da2938be8a285c0c27a/1811/en-US/0955af7dd5154a8db28dfce327d8dae5.html

https://help.sap.com/viewer/9d346683b0084da2938be8a285c0c27a/1811/en-US/6d55d5fba206425b9acecda9f2311483.html

https://help.sap.com/viewer/9d346683b0084da2938be8a285c0c27a/1905/en-US/5fd2da27ae50410592b4a8d04af1a02f.html

  • Extend CMS Structure API. Open trainingsmartedit-beans.xml and extend ComponentTypeAttributeData with defaultValue
	<bean class="de.hybris.platform.cmsfacades.data.ComponentTypeAttributeData">
	    <property name="defaultValue" type="Boolean" />
	</bean>
  • Since we added defaultValue only for Boolean type, we need to define one predicate which will allow to populate its value only for Boolean type fields.
  • Create BooleanDefaultValueStructureTypeAttributePredicate in trainingsmartedit -> src folder under relevant package. This will make sure when attribute has default value and its of Boolean type, then only populate defaultValue in Structure API.
public class BooleanDefaultValueStructureTypeAttributePredicate implements Predicate<AttributeDescriptorModel>
{
	@Override
	public boolean test(final AttributeDescriptorModel attributeDescriptor)
	{
		return attributeDescriptor.getDefaultValue() != null && attributeDescriptor.getDefaultValue() instanceof Boolean;
	}
}
  • Create Populator BooleanDefaultValueComponentTypeAttributePopulator to populate defaultValue in ComponentTypeAttributeData
public class BooleanDefaultValueComponentTypeAttributePopulator implements Populator<AttributeDescriptorModel, ComponentTypeAttributeData>
{
	@Override
	public void populate(final AttributeDescriptorModel source, final ComponentTypeAttributeData target) throws ConversionException
	{
		if (source.getDefaultValue() instanceof Boolean)
		{
			target.setDefaultValue((Boolean) source.getDefaultValue());
		}
	}
}
  • Add this populator to cmsAttributePredicatePopulatorListMap. Open trainingsmartedit-sprint.xml and add the below bean definition
	<bean id="booleanDefaultValueComponentTypeAttributePopulator" class="com.hybris.types.populator.BooleanDefaultValueComponentTypeAttributePopulator" />

	<bean depends-on="cmsAttributePredicatePopulatorListMap" parent="mapMergeDirective">
	   <property name="key">
		  <bean class="com.hybris.service.predicate.BooleanDefaultValueStructureTypeAttributePredicate"/>
	   </property>
	   <property name="value">
		  <list>
			<ref bean="booleanDefaultValueComponentTypeAttributePopulator" />
		  </list>
	   </property>
	</bean>
  • Finally, We need to extend ComponentTypeAttributeData from OCC Side as well. We can add this to trainingsmartedit-beans.xml if we don’t have custom occ extension.
	<bean class="de.hybris.platform.cmswebservices.data.ComponentTypeAttributeData">
		<hints>
		    <hint name="wsRelated"/>
		</hints>
		<property name="defaultValue" type="Boolean" />
	</bean>
  • OCC Field level Mapping already defined OOTB in cmswebservices/web/webroot/WEB-INF/config/dto-mapping-spring.xml
	<bean parent="fieldSetLevelMapping">
		<property name="dtoClass"
				  value="de.hybris.platform.cmswebservices.data.ComponentTypeData" />
		<property name="levelMapping">
			<map>
				<entry key="BASIC"
					   value="code,i18nKey,name,attributes" />
				<entry key="DEFAULT"
					   value="code,i18nKey,name,attributes" />
				<entry key="FULL"
					   value="code,i18nKey,name,attributes" />
			</map>
		</property>
	</bean>

 

Build Step

Do ant all and server start. Once Server Started, We can check in SmartEdit by adding any CMS Component, defaultValue should be populated for boolean type fields in CMS Structure API, rest BooleanComponent will do their job.

 {
         "cmsStructureType" : "Boolean",
         "collection" : false,
         "defaultValue" : true,
         "editable" : true,
         "i18nKey" : "type.abstractcmscomponent.visible.name",
         "localized" : false,
         "paged" : false,
         "qualifier" : "visible",
         "required" : false
      }

Conclusion

Thats how we can work for other type fields as well but for other types, we need to extend its angular component with default value capability.

 

/
Be the first to leave a comment
You must be Logged on to comment or reply to a post.