Expand my Community achievements bar.

SOLVED

Content fragments setting for filtering non null values

Avatar

Level 3

Hello Community,

 

Is there any setting in content fragments for filtering all non null values?

What i know is we can filter all non null at client side. I was looking for a setting where the server can filter and remove all null fields

 

Please let me know if any one has this done already

 

Thanks

Arun

1 Accepted Solution

Avatar

Correct answer by
Level 6

Hi @arungm20,

There is a way around this but it is not the best approach. If you want to do the filtration on the server side you can use the Content Fragment API and check if the element exist and do a null check for the value.

Resource fragmentResource =resourceResolver.getResource(contentFragmentPath);
ContentFragment contentFragment=fragmentResource.adaptTo(ContentFragment.class);
ContentElement element = contentFragment.getElement(elementName);
if (null != element && null != element.getValue() && null != element.getValue().getValue()) {
//your logic
}

 Create a model with all the fields(data members) as in content Fragment. In the dialog just give a pathfield to select the desired CF. You can make it a multifield incase you need a list of CF's to populate.
You can set the fields of the model depending on the non null values of CF as given in the code snippet and read the same in your htl(sightly).

This is just a way around if you need to filter non null values on server side.

Let me know if it suits your use case.

View solution in original post

6 Replies

Avatar

Level 3

Thanks @Siva_Sogalapalli  we did use this. However, when we have multiple null values the query would need to add filter for all the attributes and hence i was thinking if there is any setting in Content fragments where we can just strip out null values

Avatar

Community Advisor

Then you can try to add field validation at content fragment model itself like make it as required field but may not work if you want to make it as optional field. 

Siva_Sogalapalli_0-1681293912824.png

 

Avatar

Correct answer by
Level 6

Hi @arungm20,

There is a way around this but it is not the best approach. If you want to do the filtration on the server side you can use the Content Fragment API and check if the element exist and do a null check for the value.

Resource fragmentResource =resourceResolver.getResource(contentFragmentPath);
ContentFragment contentFragment=fragmentResource.adaptTo(ContentFragment.class);
ContentElement element = contentFragment.getElement(elementName);
if (null != element && null != element.getValue() && null != element.getValue().getValue()) {
//your logic
}

 Create a model with all the fields(data members) as in content Fragment. In the dialog just give a pathfield to select the desired CF. You can make it a multifield incase you need a list of CF's to populate.
You can set the fields of the model depending on the non null values of CF as given in the code snippet and read the same in your htl(sightly).

This is just a way around if you need to filter non null values on server side.

Let me know if it suits your use case.

Avatar

Level 3

Thanks @Madhur-Madan this approach i will keep in mind and use for other requirements. This seems like any other components and combining the CF