Content fragments setting for filtering non null values | Community
Skip to main content
April 12, 2023
Solved

Content fragments setting for filtering non null values

  • April 12, 2023
  • 2 replies
  • 1266 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Madhur-Madan

Hi @user70744,

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.

2 replies

user70744Author
April 12, 2023

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

user70744Author
April 12, 2023

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. 

 


Agree I have fragments which do not need the values 

Madhur-Madan
Community Advisor
Madhur-MadanCommunity AdvisorAccepted solution
Community Advisor
April 12, 2023

Hi @user70744,

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.

user70744Author
April 13, 2023

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