0

I have a scenario in which I need to display a list of String elements in storefront. After investigating, I noticed that Hybris has StringCollection OOTB. However the strings which should be displayed on storefront should be localized.

What is the best aproach for implementing such a scenario ? I know that I can create an ItemType which has only one localized attribute of String Type and then create a relation between this newly created item and the item which will contain the list of Strings.


Edit:

If I use:

<collectiontype code="localizedStringColl" elementtype="localized:java.lang.String"  autocreate="true" generate="true"  type="list" />

I get an error in backoffice while trying to add a new String in the list:

  de.hybris.platform.servicelayer.exceptions.UnknownIdentifierException: No composed type localized:java.lang.String exists
at de.hybris.platform.servicelayer.type.daos.impl.DefaultTypeDao.findComposedTypeByCode(DefaultTypeDao.java:71) ~[coreserver.jar:?]
at de.hybris.platform.servicelayer.type.impl.DefaultTypeService.getComposedTypeForCode(DefaultTypeService.java:114) ~[coreserver.jar:?]
at com.hybris.backoffice.solrsearch.services.impl.DefaultBackofficeFacetSearchConfigService.findSearchConfigForTypeCode(DefaultBackofficeFacetSearchConfigService.java:172) ~[backofficesolrsearchserver.jar:?]
at com.hybris.backoffice.solrsearch.services.impl.DefaultBackofficeFacetSearchConfigService.isSolrSearchConfiguredForType(DefaultBackofficeFacetSearchConfigService.java:122) ~[backofficesolrsearchserver.jar:?]

Hybris Version 6.7

HybrisHelp
  • 5,518
  • 2
  • 27
  • 65
dj_frunza
  • 1,553
  • 3
  • 17
  • 28

2 Answers2

1

How to create Localized String Collection?

You can declare CollectionType with localized:java.lang.String and use it as type while declaring your attribute.

<collectiontype code="localizedStringColl" elementtype="localized:java.lang.String"  autocreate="true" generate="true"  type="list" />

Now you can use it like

            <attribute qualifier="myAttib" type="localizedStringColl" >
                <description>MyAttib</description>
                <persistence type="property" />
            </attribute>

What is the best approach to implementing such a scenario?

First go through this answer, which helps you to understand how data is stored in collectionTypes vs RelationTypes.

As explained in the link, In case of CollectionTypes, comma-separated list of PKs will be stored, which may be resulting in truncation the value and therefore loss of data if your collection data grows up... In this case, better go with the RelationType.

If you know your collection of String is no that big size you can go with collectionType.

HybrisHelp
  • 5,518
  • 2
  • 27
  • 65
1

All the localized types are defined in {extensionName}-items.xml as maps. For example, localized:java.lang.String is defined in core-items.xml

Therefore, the best approach is to create a new maptype:

<maptypes>
    <maptype code="localized:StringCollection" argumenttype="Language" returntype="StringCollection" generate="false"/>
</maptypes>

Now the only thing that remains is to use the localized:StringCollection for the attribute that needs this type:

        <itemtype code="CustomCmsItemComponent" extends="SimpleCMSComponent"
                  autocreate="true" generate="true"
                  jaloclass="com.test.hybris.core.jalo.cms.CustomCmsItemComponent">
            <attributes>
                <attribute qualifier="localizedStringCollectionTest" type="localized:StringCollection">
                    <persistence type="property"/>
                </attribute>
            </attributes>
        </itemtype>

After building and updating the database I noticed that this solution works as expected.

dj_frunza
  • 1,553
  • 3
  • 17
  • 28