But sometimes they need a working example more than docs so below is a sample code that expands the Content Area that may help to fulfill the requirement:
PLEASE NOTED: the expanded level should be limited as it may impact the performance if it is too high.
using System;
using EPiServer.ContentApi.Core.Serialization;
using EPiServer.ContentApi.Core.Serialization.Models;
using Microsoft.AspNetCore.Http;
namespace Foundation.Features.Api
{
public class ExpandContentApiModelFilter : IContentApiModelFilter
{
private readonly IHttpContextAccessor _httpContextAccessor;
private const string ExpandingScopeKey = "ContentDeliveryApi:ExpandingScope";
private const int MaxExpandLevel = 3;
public ExpandContentApiModelFilter(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public Type HandledContentApiModel => typeof(ContentApiModel);
public void Filter(ContentApiModel contentApiModel, ConverterContext converterContext)
{
foreach (var property in contentApiModel.Properties.Values)
{
if (property is ContentAreaPropertyModel contentArea)
{
using (new ExpandingScope(_httpContextAccessor.HttpContext))
{
if (GetExpandingLevel(_httpContextAccessor.HttpContext) > MaxExpandLevel)
{
return;
}
if (contentArea.ExpandedValue is null)
{
contentArea.Expand(converterContext.Language);
}
}
}
}
}
private static int GetExpandingLevel(HttpContext context)
{
if (context.Items.TryGetValue(ExpandingScopeKey, out var value) && value is ExpandingContext expandingContext)
{
return expandingContext.Level;
}
return 0;
}
private class ExpandingContext
{
public int Level { get; set; } = 0;
}
private class ExpandingScope : IDisposable
{
private readonly HttpContext _requestContext;
public ExpandingScope(HttpContext context)
{
_requestContext = context;
if (context.Items.TryGetValue(ExpandingScopeKey, out var value) && value is ExpandingContext expandingContext)
{
expandingContext.Level++;
}
else
{
context.Items[ExpandingScopeKey] = new ExpandingContext();
}
}
public void Dispose()
{
if (_requestContext.Items[ExpandingScopeKey] is ExpandingContext currentContext)
{
if (currentContext.Level > 0)
{
currentContext.Level--;
}
}
else
{
throw new InvalidOperationException();
}
}
}
}
}
Please sign in to leave a comment.