Try our conversational search powered by Generative AI!

How to remove/omit content in ContentFilter or ContentApiModelFilter?

Vote:
 

Hello, i need to ensure that a page is not delivered from the Content DeliVery API if it's not allowed. What is the best way, using a ContentFilter or using a ContentApiModelFilter?

We have a requirement that some articles are only allowed in some countries(legal). So the ArticlePage has a custom property where you can configure it. Now i failed to implement it in ContentFilter since it does not seem to be possible to filter the complete content, just to remove/clear some properties. The documentation mentions it: 

"The downside of IContentFilter is that it is still not possible to remove some default properties of ContentApiModel like ExistingLanguagesMasterLanguage, and so on."

But i'm also unable to implement it in a ContentApiModelFilter (just relevant code):

[ServiceConfiguration(typeof(IContentApiModelFilter), Lifecycle = ServiceInstanceScope.Singleton)]
public class ArticleApiModelFilter : ContentApiModelFilterBase
{
	public override void Filter(ContentApiModel contentApiModel, ConverterContext converterContext)
	{
		IsRelevantArticleResult result = IsRelevantArticle(converterContext.ContentReference);
		if (!result.IsRelevant)
		{
			return;
		}

		if (!String.IsNullOrEmpty(result.Article?.ArticleDetails?.AllowedCountries))
		{
			bool isArticleAllowed = IsArticleAllowed(result.Article!); 
			if (!isArticleAllowed)
			{
                               // contentApiModel.Properties.Clear() does also not work
				foreach (string key in contentApiModel.Properties.Keys)
				{
					contentApiModel.Properties[key] = null;
				}
                                
                                contentApiModel.ExistingLanguages = null;
                                contentApiModel.MasterLanguage = null;
                                contentApiModel.ParentLink = null;
                                contentApiModel.Language = null;
                                contentApiModel.StartPublish = null;
                                contentApiModel.StopPublish = null;
				return;
			}
		}
	}
}

The article is added to a ContentArea and the response still contains it(id=7851):

"mainContentArea": [
        {
            "displayOption": "",
            "contentLink": {
                "id": 7851,
                "workId": 0,
                "guidValue": "9ec626ba-c936-4c39-8266-ee5561219198",
                "expanded": {
                    "contentLink": {
                        "id": 7851,
                        "workId": 0,

So what is the correct way to remove an entire content completely from the api response?

Update: added a feature request.

#314353
Edited, Dec 18, 2023 17:20
Vote:
 

Have you tried implementing a custom version of ContentLoaderService where you override the ShouldContentBeExposed-method tor return false in some cases based on the current content?

Documentation: https://docs.developers.optimizely.com/content-management-system/v1.5.0-content-delivery-api/docs/customizing-content-delivery-api-for-edit-view

#317352
Feb 19, 2024 8:54
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.